-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
164 lines (134 loc) · 3.1 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
<canvas id="myCanvas"
width="500px" height="500px"
style="background-color:darkred">
</canvas>
<style>
body{
margin:0;
overflow:hidden;
}
</style>
<script>
const ctx=myCanvas.getContext("2d");
const offset={
x:myCanvas.width/2,
y:myCanvas.height/2
};
const point={x:90,y:120};
const G={x:20,y:50};
ctx.translate(offset.x,offset.y);
update();
document.onmousemove=(event)=>{
point.x=event.x-offset.x;
point.y=event.y-offset.y;
update();
}
function update(){
ctx.clearRect(-offset.x,-offset.y,
myCanvas.width,myCanvas.height);
drawCoordinateSystem();
const {mag,dir}=toPolar(point);
const same=toXY({mag,dir});
const resultAdd=add(point,G);
ctx.beginPath();
ctx.setLineDash([3,3]);
ctx.moveTo(G.x,G.y);
ctx.lineTo(resultAdd.x,resultAdd.y);
ctx.lineTo(point.x,point.y);
ctx.stroke();
ctx.setLineDash([]);
drawArrow({x:0,y:0},resultAdd,"red");
const resultSub=subtract(point,G);
drawArrow({x:0,y:0},resultSub,"red");
drawArrow(G,point,"red");
const scaledSub=scale(
normalize(resultSub),50);
drawArrow({x:0,y:0},scaledSub,"red");
console.log(Math.acos(dot(normalize(G),
normalize(point))));
drawArrow({x:0,y:0},point);
drawArrow({x:0,y:0},G);
}
function drawArrow(tail,tip,color="white",size=20){
const {dir,mag}=toPolar(subtract(tip,tail));
const v1={dir:dir+Math.PI*0.8,mag:size/2};
const p1=toXY(v1);
const t1=add(p1,tip);
const v2={dir:dir-Math.PI*0.8,mag:size/2};
const p2=toXY(v2);
const t2=add(p2,tip);
ctx.beginPath();
ctx.moveTo(tail.x,tail.y);
ctx.lineTo(tip.x,tip.y);
ctx.strokeStyle=color;
ctx.stroke();
ctx.beginPath();
ctx.moveTo(tip.x,tip.y);
ctx.lineTo(t1.x,t1.y);
ctx.lineTo(t2.x,t2.y);
ctx.closePath();
ctx.stroke();
ctx.fillStyle=color;
ctx.fill();
}
function dot(p1,p2){
return p1.x*p2.x+p1.y*p2.y;
}
function normalize(p){
return scale(p,1/magnitude(p));
}
function scale(p,scalar){
return{
x:p.x*scalar,
y:p.y*scalar
};
}
function add(p1,p2){
return{
x:p1.x+p2.x,
y:p1.y+p2.y
};
}
function subtract(p1,p2){
return{
x:p1.x-p2.x,
y:p1.y-p2.y
};
}
function toXY({mag,dir}){
return {
x:Math.cos(dir)*mag,
y:Math.sin(dir)*mag
};
}
function toPolar({x,y}){
return {
dir:direction({x,y}),
mag:magnitude({x,y})
};
}
function direction({x,y}){
return Math.atan2(y,x);
}
function magnitude({x,y}){
return Math.hypot(x,y);
}
function drawPoint(loc,size=10,color="white"){
ctx.beginPath();
ctx.fillStyle=color;
ctx.arc(loc.x,loc.y,size/2,0,Math.PI*2);
ctx.fill();
}
function drawCoordinateSystem(){
ctx.beginPath();
ctx.moveTo(-offset.x,0);
ctx.lineTo(myCanvas.width-offset.x,0);
ctx.moveTo(0,-offset.y);
ctx.lineTo(0,myCanvas.height-offset.y);
ctx.setLineDash([5,4]);
ctx.lineWidth=2;
ctx.strokeStyle="red";
ctx.stroke();
ctx.setLineDash([]);
}
</script>