-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.html
More file actions
135 lines (107 loc) · 3.59 KB
/
convert.html
File metadata and controls
135 lines (107 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<link rel="icon" href="images/icon.ico">
<script src="p5/p5.js"></script>
<title>convert</title>
</head>
<body>
<div class="graphic" id="baseConvertGraphic"></div>
<div class="pageButton">
<form id="baseConvertForm" action="/action_page.php">
<input type="text" id="expr" value="horizontal"><br>
<input type="number" min="2" max="35" class="numInput" step="1" id="from" value=23>
<input type="number" min="2" max="35" class="numInput" step="1" id="to" value=25>
</form>
<h1 id="convert" onclick="baseConvert()">go</h1>
</div>
<div class="backButton"><a href="index.html">back</a></div>
<script>
var charToDigit = {
"0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8,
"9": 9, "a": 10, "b": 11, "c": 12, "d": 13, "e": 14, "f": 15, "g": 16,
"h": 17, "i": 18, "j": 19, "k": 20, "l": 21, "m": 22, "n": 23, "o": 24,
"p": 25, "q": 26, "r": 27, "s": 28, "t": 29, "u": 30, "v": 31, "w": 32,
"x": 33, "y": 34, "z": 35
};
var w, h;
function setup() {
if (windowWidth > 550) {
w = 0.8 * windowWidth
} else {
w = 0.95 * windowWidth
};
h = 0.82 * windowHeight;
var myCanvas = createCanvas(w, h);
myCanvas.parent("baseConvertGraphic");
};
function baseConvert() {
var inputs = document.getElementById("baseConvertForm");
// convert each inputted characeter into its corresponding digit
var word = inputs.elements[0].value;
word = word.toLowerCase().replace(/[^a-z]+/g, '');
var chars = word.split("").reverse();
var charVals = [];
for (var i = 0; i < chars.length; i++) {
charVals[i] = charToDigit[chars[i]];
};
// convert from oldbase to newbase
var oldbase = inputs.elements[1].value;
var newbase = inputs.elements[2].value;
// convert the word digits from oldbase to base 10 (sum powers of oldbase)
var baseTenVal = (Array.from(
charVals,
(x, i) => (x * Math.pow(oldbase, i))
)).reduce(add, 0);
// convert the base ten value to newbase
var newBaseNum = baseTenVal.toString(newbase);
// get an array of the value corresponding to each digit in the newbase number
var newChars = newBaseNum.split("");
var newCharVals = [];
for (var i = 0; i < chars.length; i++) {
newCharVals[i] = charToDigit[newChars[i]];
};
// draw rectangles corresponding to newbase digits
drawDigitRectangles(newCharVals)
};
function add(accumulator, a) {
return accumulator + a
};
function drawDigitRectangles(digits) {
clear();
var len = digits.length;
var maxWidth = 0.9 * w;
var hh = (0.75 * h) / len;
for (let i = 0; i < len; i++) {
for (let j = 0; j < digits[i]; j++) {
var ww = maxWidth / (digits[i]);
noStroke();
// alternate fill colors
if ((j % 2) == 0) {
if ((i % 2) == 0) {
fill("black")
} else {
fill("skyblue")
};
} else {
if ((i % 2) == 0) {
fill("skyblue")
} else {
fill("black")
};
};
// leave 0 rows blank
if (digits[i] > 0) {
rect(j * ww, i * hh, ww, hh);
} else {
i++
}
}
}
};
</script>
</body>
</html>