-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlineintersectiontest.js
103 lines (84 loc) · 2.06 KB
/
lineintersectiontest.js
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
var width = 800;
var height = 600;
function getCanvas() {
return document.getElementById('canvas');
}
window.addEventListener('load', function() {
var canvas = getCanvas();
if(canvas && canvas.getContext) {
var context = canvas.getContext('2d');
if (context) {
context.fillStyle = '#fff';
context.fillRect(0, 0, width, height);
}
}
canvas.addEventListener('mousemove', mouseMove, false);
canvas.addEventListener('mousedown', mouseDown, false);
}, false);
function getEventXCoord(ev){
if(ev.layerX){ //Firefox
return ev.layerX;
}
return ev.offsetX; //Opera
}
function getEventYCoord(ev){
if(ev.layerY){ //Firefox
return ev.layerY;
}
return ev.offsetY; //Opera
}
function mouseDown(ev){
var canvas = getCanvas();
context = canvas.getContext('2d');
debug = 1;
}
function getLine2(){
return new Segment(new Vector(0, 100), new Vector(150, 50));
}
var debug = 1;
function mouseMove(ev) {
if(debug == 1){
//alert('t3');
}
var canvas = getCanvas();
context = canvas.getContext('2d');
context.fillStyle = '#fff';
context.fillRect(0, 0, width, height);
context.fillStyle = '#f00'; //red
if(debug == 1){
//alert('t3.5');
}
context.strokeStyle = '#000'; //green
context.lineWidth = 4;
//context.beginPath();
//alert('t3.75');
//context.moveTo(0, 0);
//alert('t3.95');
var x = getEventXCoord(ev);
var y = getEventYCoord(ev);
//context.lineTo(x, y);
if(debug == 1){
// alert('t4');
}
//context.closePath();
//alert('t5');
//context.fill();
//alert('t6');
//context.stroke();
//alert('t7');
//alert('b1');
// var v = new Vector(0, 0);
var seg1 = new Segment(new Vector(0, 0), new Vector(x, y));
if(debug == 1){
//alert('b2');
debug = 0;
}
seg1.draw(getCanvas());
var seg2 = new Segment(new Vector(0, 100), new Vector(100, 50));
seg2.draw(getCanvas());
var intersectionPoint = new Vector(0, 0);
if(intersect(seg1, seg2, intersectionPoint) == INTERSECT){
intersectionPoint.color = '#f00';
intersectionPoint.draw(getCanvas());
}
};