-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrig2.html
253 lines (216 loc) · 7.06 KB
/
trig2.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<html>
<meta charset="utf-8">
<head>
<script src="https://unpkg.com/mathjs/lib/browser/math.js"></script>
<script>
window.onload = init
function drawLine(x1, y1, x2, y2, lineColor) {
ctx.strokeStyle = lineColor
ctx.beginPath()
ctx.moveTo(x1, y1)
ctx.lineTo(x2, y2)
ctx.stroke()
}
var ctx
var end = 750
var middle = end / 2
var oneSize = 100
var staticColor = 'pink'
var dynamicColor = '#3f3ff5'
function drawCircle(radius) {
ctx.beginPath()
ctx.strokeStyle = staticColor
ctx.arc(middle, middle, radius, -math.PI * 2, 0)
ctx.stroke()
}
function drawStatic() {
var tickSpace = oneSize / 5
var tickSize = 5
ctx.clearRect(0, 0, end, end)
drawLine(middle, 0, middle, end, staticColor)
drawLine(0, middle, end, middle, staticColor)
for (i = -20; i <= 20; i += 1) {
if (i != 0) {
x1 = middle + tickSpace * i
y1 = middle - tickSpace * i
tickStart = middle-tickSize
tickEnd = middle+tickSize
drawLine(x1, tickStart, x1, tickEnd, staticColor)
drawLine(tickStart, y1, tickEnd, y1, staticColor)
if ((tickSpace * i) % oneSize == 0) {
drawLabelAtPos(i/5, 0, 0, 0, x1, middle+15, 'center', 'white')
drawLabelAtPos(i/5, 0, 0, 0, middle + 10, y1+4, 'center', 'white')
}
}
}
drawCircle(oneSize)
}
// x1, y1,
// x2, y2,
// x3, y3
function drawTriangle(middle, x1, y1, x2, y2, x3, y3) {
drawLine(middle + x1, middle + y1, middle + x2, middle + y2, dynamicColor)
drawLine(middle + x2, middle + y2, middle + x3, middle + y3, dynamicColor)
drawLine(middle + x1, middle + y1, middle + x3, middle + y3, dynamicColor)
}
function my_round(number) {
return math.round(number * 1000) / 1000
}
function drawMathLabel(prefix, number, offset) {
textX = middle - 250
ctx.textAlign = 'right'
if (prefix.length > 0) {
ctx.fillText(prefix + '(α) = ', textX, middle - offset)
} else {
ctx.fillText('α = ', textX, middle - offset)
ctx.textAlign = 'left'
ctx.fillText('= ' + getSliderDegree() + '°', textX + 160, middle - offset)
}
ctx.textAlign = 'left'
ctx.fillText(my_round(number), textX, middle - offset)
let scope = {
x: number
}
if (number == 'NaN') {
pis = 'NaN'
} else {
pis = math.evaluate("x/pi", scope)
}
ctx.fillText(' = π*' + my_round(pis), textX + 60, middle - offset)
}
function drawLabelAtPos(text, x, y, rotation, x_offset, y_offset, textAlign, color) {
ctx.save()
ctx.textAlign = textAlign
ctx.fillStyle = color
ctx.translate(x, y)
ctx.rotate(rotation)
ctx.fillText(text, x_offset, y_offset)
ctx.stroke()
ctx.restore()
}
function drawTrig() {
ctx.putImageData(staticPicture, 0, 0)
var alpha_degree = getSliderDegree()
var alpha = alpha_degree * (math.pi / 180)
var cos_alpha = math.cos(alpha)
var sin_alpha = math.sin(alpha)
var tan_alpha = math.tan(alpha)
var cot_alpha = math.cot(alpha)
var csc_alpha = math.csc(alpha)
var sec_alpha = math.sec(alpha)
if (math.equal((alpha_degree + 90) % 180, 0)) {
tan_alpha = "NaN"
sec_alpha = "NaN"
}
if (math.equal(alpha_degree % 180, 0)) {
console.log('sin is 0')
cot_alpha = "NaN"
csc_alpha = "NaN"
}
x2 = cos_alpha * oneSize
y2 = sin_alpha * -oneSize
var csc_y = -csc_alpha * oneSize + middle
var sec_x = sec_alpha * oneSize + middle
drawTriangle(middle, 0, 0, x2, y2, x2, 0)
drawLine(middle, csc_y, middle + x2, middle + y2, 'green')
drawLine(middle + x2, middle + y2, sec_x, middle, 'yellow')
drawLine(middle, middle + y2, middle + x2, middle + y2, 'green')
ctx.beginPath()
ctx.strokeStyle = dynamicColor
ctx.arc(middle, middle, oneSize / 4, -alpha, 0)
ctx.stroke()
ctx.fillStyle = 'green'
ctx.font = '20px sans-serif'
drawMathLabel("", alpha, 360)
drawMathLabel("sin", sin_alpha, 330)
drawMathLabel("cos", cos_alpha, 300)
drawMathLabel("tan", tan_alpha, 270)
drawMathLabel("cot", cot_alpha, 240)
drawMathLabel("csc", csc_alpha, 210)
drawMathLabel("sec", sec_alpha, 180)
if ((alpha - (math.pi / 4)) % (math.pi / 2) > 0.37) {
csc_label_x = middle - 35
} else {
csc_label_x = middle - 10
}
if (alpha % (math.pi / 2) > 0.23) {
sec_label_y = middle + 20
} else {
sec_label_y = middle + 45
}
ctx.font = '15px sans-serif'
drawLabelAtPos("sin", 0, 0, 0, middle - 10, middle + y2 + 4, 'right', 'green')
drawLabelAtPos("cos", 0, 0, 0, middle + x2, middle + 20, 'right', dynamicColor)
drawLabelAtPos("csc", 0, 0, 0, csc_label_x, csc_y, 'right', 'green')
drawLabelAtPos("sec", 0, 0, 0, sec_x, sec_label_y, 'left', dynamicColor)
drawLabelAtPos("tan", middle + x2, middle + y2, -alpha + (math.pi / 2), 10, -10, 'left', 'yellow')
drawLabelAtPos("cot", middle + x2, middle + y2, -alpha + (math.pi / 2), -10, -10, 'right')
}
var staticPicture
function init() {
var canvas = document.getElementById('drawBoard')
ctx = canvas.getContext('2d')
drawStatic()
staticPicture = ctx.getImageData(0, 0, canvas.width, canvas.height)
setupSlider()
drawTrig()
}
function getSliderDegree() {
var slider = document.getElementById('alpha-slider')
return slider.value / 10
}
function setupSlider() {
var slider = document.getElementById('alpha-slider')
slider.oninput = function () {
var rad = getSliderDegree() * (math.PI / 180)
drawTrig()
}
}
function addAngle(increment) {
var slider = document.getElementById('alpha-slider')
var slider_max = Number(slider.max)
var newVal = Number(slider.value) + Number(increment)
if (newVal < 0) {
newVal += slider_max
}
if (newVal > slider_max) {
newVal -= slider_max
}
slider.value = newVal
drawTrig()
}
</script>
<style>
body {
background-color: #19191d;
color: khaki
}
button {
border: none;
padding: 1px;
cursor: pointer;
/* display: grid; */
float: left;
border: 1px solid
}
button:hover {
background: rgb(120, 121, 194)
}
#alpha-slider {
width: 10cm
}
</style>
</head>
<body data-rssl="1">
<canvas id="drawBoard" width="750" height="750"></canvas>
<div>
<button onclick="addAngle(100)">+10°</button>
<button onclick="addAngle(-100)">-10°</button>
<button onclick="addAngle(50)">+5°</button>
<button onclick="addAngle(-50)">-5°</button>
<button onclick="addAngle(1)">+0.1°</button>
<button onclick="addAngle(-1)">-0.1°</button>
<input type="range" min="0" max="3600" value="450" class="slider" id="alpha-slider">
</div>
</body>
</html>