-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
172 lines (159 loc) · 2.74 KB
/
Copy pathtest.html
File metadata and controls
172 lines (159 loc) · 2.74 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
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
<canvas id="can" width= "500" height="500" style="border:1px solid black;"></canvas>
<br>
<input type="text" name="fname">
<script>
var alldraw = true;
var uidcounter = 0;
var kaa, kab;
function getUid()
{
uidcounter++;
return uidcounter;
}
function point(x,y)
{
this.x = x;
this.y = y;
}
function line(x1,y1,x2,y2)
{
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
this.m = function()
{
xd = this.x2-this.x1;
yd = this.y2-this.y1;
return yd/xd;
};
this.off = function ()
{
var ma = this.m();
if (ma == Infinity || ma == -Infinity)
return this.x1;
return this.y2 - this.x2* ma;
};
this.draw = function()
{
this.ctx.beginPath();
this.ctx.moveTo(this.x1,500-this.y1);
this.ctx.lineTo(this.x2,500-this.y2);
this.ctx.stroke();
};
this.inter = function (line)
{
var a = (line.off()-this.off());
var b = (this.m()-line.m());
var x = a/b;
/*if (b == Infinity || b == -Infinity)
x = a;*/
if(b == Infinity || b == -Infinity)
x = this.x2;
var p = new point(x,line.m()*x+line.off());
if(p.y == -Infinity || p.y == Infinity)
{
x = line.x2;
var p = new point(x,this.m()*x+this.off());
// p.y = this.m()*x+this.off()
}
kaa = p.x;
kab = line.m();
p.m1 = this.m();
p.m2 = line.m();
return p;
};
}
function circle(x,y,r)
{
this.x = x;
this.y = y;
this.r = r;
this.vx = 0;
this.vy = 0;
this.draw = function()
{
this.ctx.beginPath();
this.ctx.arc(this.x,500-this.y,this.r,0,2*Math.PI);
this.ctx.stroke();
};
this.move = function(objs)
{
this.x += this.vx;
this.y += this.vy;
};
}
var c = document.getElementById("can");
window.addEventListener("keydown", asdd,true);
function asdd(e)
{
document.title = e.keyCode;
if ( e.keyCode == 37)
{
a.x1 --;
}
if ( e.keyCode == 38)
{
a.y1 ++;
}
if ( e.keyCode == 39)
{
a.x1 ++;
}
if ( e.keyCode == 40)
{
a.y1 --;
}
if ( e.keyCode == 65)
{
a.x2 --;
}
if ( e.keyCode == 87)
{
a.y2 ++;
}
if ( e.keyCode == 68)
{
a.x2 ++;
}
if ( e.keyCode == 83)
{
a.y2 --;
}
}
ctx = c.getContext("2d");
ctx.font = '10pt Calibri';
line.prototype.ctx = ctx;
circle.prototype.ctx = ctx;
setInterval(frame,20);
a = new line(50,100,50,50);
b = new line(50,50,100,100);
c = new circle(10,10,5);
b.vx = 1;
function frame()
{
//clear
//c.width = c.width;
ctx.fillStyle="white";
ctx.fillRect(0,0,500,500);
ctx.fillStyle="black";
//end clear
//update
//c.move();
p = a.inter(b);
//end update
//draw
a.draw();
b.draw();
new circle(a.x1,a.y1,3).draw();
new circle(p.x,p.y,5).draw();
/*if(p.m1 < 0)
new line(p.x,p.y,p.x+10,p.y+(-(p.m2-p.m1))*10).draw();
else
new line(p.x,p.y,p.x-10,p.y-(-(p.m2-p.m1))*10).draw();
*/
ctx.fillText(p.m1,10,20);
ctx.fillText(p.m2,10,30);
//end draw
}
</script>