-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
200 lines (182 loc) · 7.26 KB
/
index.html
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<title>Vector Calculator</title>
<link href="https://cdn.jsdelivr.net/npm/bootswatch@5.0.2/dist/darkly/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
<style> <!-- "all: unset" is to remove properties inherited from bootstrap -->
input::-webkit-inner-spin-button,input::-webkit-outer-spin-button {
-webkit-appearance: none;
margin: 0
}
input[type=number] {
-moz-appearance: textfield
}
.clockface {
all: unset;
width: 50%;
padding-top: 50%;
margin-left:auto;
margin-right:auto;
transform: translateY(0);
border-style: solid;
border-radius: 50%
}
.needle {
position: absolute;
top: 50%;
width: 50%;
transform-origin: 100%;
height: 6px
}
</style>
</head>
<body style="padding-top: 72px">
<nav class="navbar navbar-expand-lg fixed-top navbar-dark bg-dark">
<div class="container-fluid">
<a class="navbar-brand" href="#">Vector Calculator</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<button type="button" class="btn btn-outline" data-bs-toggle="modal" data-bs-target="#formula">Formula</button>
</li>
</ul>
<a class="btn btn-outline-success" href="https://github.com/beans42/vec-calc" role="button">Source</a>
</div>
</div>
</nav>
<div class="modal fade" id="formula" tabindex="-1" aria-labelledby="formulaLabel" aria-hidden="true">
<div class="modal-dialog modal-fullscreen-md-down">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="formulaLabel">How It Works</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p>First, both vectors are converted to cartesian form using the following technique:</p>
<pre><code>x = magnitude * cos(angle)<br>y = magnitude * sin(angle)</code></pre>
<p>Second, the vectors are summed up coordinate-wise.</p>
<pre><code>result vector = (x1 + x2 , y1 + y2)</code></pre>
<p>Lastly, the vector is converted back into magnitude/direction form.</p>
<pre><code>magnitude = sqrt(x<sup>2</sup> + y<sup>2</sup>)<br>angle = <a href="https://en.wikipedia.org/wiki/Atan2">atan2</a>(y, x)</code></pre>
<p>A JavaScript implementation can be found at <a href="">line 167</a> of this page.</p>
</div>
</div>
</div>
</div>
<div class="container">
<div>
<label>First Vector</label>
<div class="row">
<div class="col-sm">
<input type="number" inputmode="decimal" class="form-control" id="vec_one_mag" oninput="handle()" placeholder="Magnitude" aria-label="Magnitude">
</div>
<div class="col-sm">
<input type="number" inputmode="decimal" class="form-control" id="vec_one_dir" oninput="handle()" placeholder="Direction (degrees)" aria-label="Direction (degrees)">
</div>
</div>
</div>
<div>
<label>Second Vector</label>
<div class="row">
<div class="col-sm">
<input type="number" inputmode="decimal" class="form-control" id="vec_two_mag" oninput="handle()" placeholder="Magnitude" aria-label="Magnitude">
</div>
<div class="col-sm">
<input type="number" inputmode="decimal" class="form-control" id="vec_two_dir" oninput="handle()" placeholder="Direction (degrees)" aria-label="Direction (degrees)">
</div>
</div>
</div>
<div hidden id="result_div">
<label>Result Vector</label>
<div class="row">
<div class="col">
<input type="text" readonly class="form-control" id="vec_res_mag">
</div>
<div class="col">
<input type="text" readonly class="form-control" id="vec_res_dir">
</div>
</div>
<label>Diagram</label>
<div class="row">
<div class="clockface">
<div class="needle"></div>
<div class="needle"></div>
<div class="needle"></div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script>
<script>
const holder_div = document.getElementById("result_div");
const needles = holder_div.querySelectorAll("div.needle");
const text_boxes = [
document.getElementById("vec_one_mag"),
document.getElementById("vec_one_dir"),
document.getElementById("vec_two_mag"),
document.getElementById("vec_two_dir"),
document.getElementById("vec_res_mag"),
document.getElementById("vec_res_dir"),
];
function handle() {
for (let i = 0; i < 4; ++i) {
if (Number.isNaN(+text_boxes[i].value) || text_boxes[i].value === "") {
holder_div.hidden = true;
return;
}
}
const a_vec = [ +text_boxes[0].value, +text_boxes[1].value ];
const b_vec = [ +text_boxes[2].value, +text_boxes[3].value ];
const res_vec = vec_add(a_vec, b_vec);
text_boxes[4].value = res_vec[0];
text_boxes[5].value = res_vec[1] + "°";
let vec_arr = [
{ v: a_vec, res: false },
{ v: b_vec, res: false },
{ v: res_vec, res: true }
];
vec_arr.sort((a, b) => {
return b.v[0] - a.v[0];
});
vec_arr.forEach((val, idx, arr) => {
const elem = needles[idx];
const mag_proportion = 100 - +(val.v[0] / arr[0].v[0] * 100).toFixed(1);
const color = val.res ? "#FF0000" : "#FFF";
elem.style.transform = "rotate("+(val.v[1]+90)+"deg)";
elem.style.background = "linear-gradient(to right,rgba(255,0,0,0) 0,rgba(255,0,0,0) "+mag_proportion+"%,"+color+" "+mag_proportion+"%,"+color+" 100%)";
});
holder_div.hidden = false;
}
function vec_add(a_vec, b_vec) { //vec format: [ magnitude, angle in degrees ]
const d2r = x => x * Math.PI / 180; //helper functions for conversion between radians and degrees
const r2d = x => x * 180 / Math.PI;
const cos = x => Math.cos(d2r(x)); //wrap standard math functions (use radians) to use degrees
const sin = x => Math.sin(d2r(x));
const a_cart = [ //cartesian format: [ x coordinate, y coordinate ]
a_vec[0] * cos(a_vec[1]),
a_vec[0] * sin(a_vec[1])
];
const b_cart = [
b_vec[0] * cos(b_vec[1]),
b_vec[0] * sin(b_vec[1])
];
const sum_cart = [
a_cart[0] + b_cart[0],
a_cart[1] + b_cart[1]
];
const sum_vec = [
Math.sqrt((sum_cart[0] * sum_cart[0]) + (sum_cart[1] * sum_cart[1])), //magnitude
r2d(Math.atan2(sum_cart[1], sum_cart[0]))
];
if (sum_vec[1] < 0) //if negative angle (bad), make positive by adding 360°
sum_vec[1] += 360
return [ +sum_vec[0].toFixed(2), +sum_vec[1].toFixed(2) ]; //toFixed() returns string and the '+' converts back to Number
}
</script>
</body>
</html>