-
Notifications
You must be signed in to change notification settings - Fork 14
/
2d.js
executable file
·131 lines (124 loc) · 3.5 KB
/
2d.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
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
// Reference : http://www.cam.hi-ho.ne.jp/strong_warriors/teacher/chapter0{4,5}.html
function point(x, y){
this.x = x;
this.y = y;
}
function getCrossPoint(x11, y11, x12, y12, x21, y21, x22, y22){ // point
var a1 = y12 - y11;
var b1 = x11 - x12;
var c1 = -1 * a1 * x11 - b1 * y11;
var a2 = y22 - y21;
var b2 = x21 - x22;
var c2 = -1 * a2 * x21 - b2 * y21;
var temp = b1 * a2 - b2 * a1;
if(temp == 0){ // parallel
return false;
}
return new point((c1 * b2 - c2 * b1) / temp, (a1 * c2 - a2 * c1) / temp);
}
function isCross(x11, y11, x12, y12, x21, y21, x22, y22){ // boolean
var temp = getCrossPoint(x11, y11, x12, y12, x21, y21, x22, y22);
if(!temp){ return false; }
if(x11 < x12 && (temp.x < x11 || x12 < temp.x) ||
x11 > x12 && (temp.x < x12 || x11 < temp.x) ||
y11 < y12 && (temp.y < y11 || y12 < temp.y) ||
y11 > y12 && (temp.y < y12 || y11 < temp.y)
){
return false;
}
if(x21 < x22 && (temp.x < x21 || x22 < temp.x) ||
x21 > x22 && (temp.x < x22 || x21 < temp.x) ||
y21 < y22 && (temp.y < y21 || y22 < temp.y) ||
y21 > y22 && (temp.y < y22 || y21 < temp.y)
){
return false;
}
return true;
}
function isCrossBox(x1, y1, x2, y2, bx1, by1, bx2, by2){ // boolean
if(isCross(x1, y1, x2, y2, bx1, by1, bx2, by1)){ return true; }
if(isCross(x1, y1, x2, y2, bx2, by1, bx2, by2)){ return true; }
if(isCross(x1, y1, x2, y2, bx1, by2, bx2, by2)){ return true; }
if(isCross(x1, y1, x2, y2, bx1, by1, bx1, by2)){ return true; }
return false;
}
function isCrossBoxWithOthers(strokesArray, i, bx1, by1, bx2, by2){ // boolean
for(var j = 0; j < strokesArray.length; j++){
if(i == j){ continue; }
switch(strokesArray[j][0]){
case 0:
case 8:
case 9:
break;
case 6:
case 7:
if(isCrossBox(strokesArray[j][7],
strokesArray[j][8],
strokesArray[j][9],
strokesArray[j][10],
bx1, by1, bx2, by2)){
return true;
}
case 2:
case 12:
case 3:
case 4:
if(isCrossBox(strokesArray[j][5],
strokesArray[j][6],
strokesArray[j][7],
strokesArray[j][8],
bx1, by1, bx2, by2)){
return true;
}
default:
if(isCrossBox(strokesArray[j][3],
strokesArray[j][4],
strokesArray[j][5],
strokesArray[j][6],
bx1, by1, bx2, by2)){
return true;
}
}
}
return false;
}
function isCrossWithOthers(strokesArray, i, bx1, by1, bx2, by2){ // boolean
for(var j = 0; j < strokesArray.length; j++){
if(i == j){ continue; }
switch(strokesArray[j][0]){
case 0:
case 8:
case 9:
break;
case 6:
case 7:
if(isCross(strokesArray[j][7],
strokesArray[j][8],
strokesArray[j][9],
strokesArray[j][10],
bx1, by1, bx2, by2)){
return true;
}
case 2:
case 12:
case 3:
case 4:
if(isCross(strokesArray[j][5],
strokesArray[j][6],
strokesArray[j][7],
strokesArray[j][8],
bx1, by1, bx2, by2)){
return true;
}
default:
if(isCross(strokesArray[j][3],
strokesArray[j][4],
strokesArray[j][5],
strokesArray[j][6],
bx1, by1, bx2, by2)){
return true;
}
}
}
return false;
}