-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
209 lines (197 loc) · 4.96 KB
/
index.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
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
'use strict';
const Vector2d = require('geometry-vector2d');
// Line intersects
/*
p1, p2 belong to first line
p3, p4 belong to second line
*/
function pointOfLinesIntersect(p1, p2, p3, p4) {
if(p1.y == p2.y) {
if(p3.y == p4.y) {
return undefined;
}
if(p3.x == p4.x) {
return Vector2d.getVectorXY(p3.x, p1.y);
}
let x = getXInLine(p3, p4, p1.y);
if(x == undefined) {
return undefined;
}
return Vector2d.getVectorXY(x, p1.y);
}
if(p3.y == p4.y) {
if(p1.x == p2.x) {
return Vector2d.getVectorXY(p1.x, p3.y);
}
let x = getXInLine(p1, p2, p3.y);
if(x == undefined) {
return undefined;
}
return Vector2d.getVectorXY(x, p3.y);
}
if(p1.x == p2.x) {
if(p3.x == p4.x) {
return undefined;
}
let y = getYInLine(p3, p4, p1.x);
if(y == undefined) {
return undefined;
}
return Vector2d.getVectorXY(p1.x, y);
}
if(p3.x == p4.x) {
let y = getYInLine(p1, p2, p3.x);
if(y == undefined) {
return undefined;
}
return Vector2d.getVectorXY(p3.x, y);
}
let p2xp1x = p2.x - p1.x,
p4yp3y = p4.y - p3.y,
p4xp3x = p4.x - p3.x,
p2yp1y = p2.y - p1.y,
p3xp1x = p3.x - p1.x;
let a = p2xp1x * p4yp3y * p1.y - p4xp3x * p2yp1y * p3.y +
p2yp1y * p4yp3y * p3xp1x,
b = p2xp1x * p4yp3y - p4xp3x * p2yp1y;
if(b == 0) {
return undefined;
}
let y = a / b;
let x = getXInLine(p1, p2, y);
if(x == undefined) {
return undefined;
}
return Vector2d.getVectorXY(x, y);
}
function getXInLine(p1, p2, y) {
if(p1.y != p2.y) {
return p1.x + (p2.x - p1.x) * (y - p1.y) / (p2.y - p1.y);
}
return undefined;
}
function getYInLine(p1, p2, x) {
if(p1.x != p2.x) {
return p1.y + (p2.y - p1.y) * (x - p1.x) / (p2.x - p1.x);
}
return undefined;
}
/*
point - point that belongs to line (begin, direction)
begin, direction belong to ray (starts in begin directed to direction)
return true if belongs otherwise false
*/
function pointBelongsToRay(point, begin, direction) {
if(begin.y == direction.y) {
if(begin.x == direction.x) {
return false;
}
if(begin.x < direction.x) {
if(point.x < begin.x) {
return false;
}
}
if(point.x > begin.x) {
return false;
}
}
if(begin.x == direction.x) {
if(begin.y < direction.y) {
if(point.y < begin.y) {
return false;
}
}
if(point.y > begin.y) {
return false;
}
}
let quarterOfPoint = Vector2d.getQuarterAccording(point, begin),
quarterOfDirection = Vector2d.getQuarterAccording(direction, begin);
if(quarterOfPoint != quarterOfDirection) {
return false;
}
return true;
}
/*
point - point that belongs to line (begin, end)
begin, end belong to segment (starts in begin and ends in end)
return true if belongs otherwise false
*/
function pointBelongsToSegment(point, begin, end) {
return pointBelongsToRay(point, begin, end) && pointBelongsToRay(point, end, begin);
}
/*
p1, p2 belong line
begin, direction belong to ray (starts in begin directed to direction)
*/
function pointOfLineAndRayIntersect(p1, p2, begin, direction) {
let point = pointOfLinesIntersect(p1, p2, begin, direction);
if(point != undefined) {
if(pointBelongsToRay(point, begin, direction) == false) {
point = undefined;
}
}
return point;
}
/*
begin1, direction1 belong to ray (starts in begin1 directed to direction1)
begin2, direction2 belong to ray (starts in begin2 directed to direction2)
*/
function pointOfRaysIntersect(begin1, direction1, begin2, direction2) {
let point = pointOfLineAndRayIntersect(begin1, direction1, begin2, direction2);
if(point != undefined) {
if(pointBelongsToRay(point, begin1, direction1) == false) {
point = undefined;
}
}
return point;
}
/*
p1, p2 belong to line
begin, end belong to segment
*/
function pointOfLineAndSegmentIntersect(p1, p2, begin, end) {
let point = pointOfLinesIntersect(p1, p2, begin, end);
if(point != undefined) {
if(pointBelongsToSegment(point, begin, end) == false) {
point = undefined;
}
}
return point;
}
/*
begin1, direction1 belong to ray (starts in begin1 directed to direction1)
begin2, end2 belong segment
*/
function pointOfRayAndSegmentIntersect(begin1, direction1, begin2, end2) {
let point = pointOfLineAndSegmentIntersect(begin1, direction1, begin2, end2);
if(point != undefined) {
if(pointBelongsToRay(point, begin1, direction1) == false ) {
point = undefined;
}
}
return point;
}
/*
begin1, end1 belong to first segment
begin2, end2 belong to second segment
*/
function pointOfSegmentsIntersect(begin1, end1, begin2, end2) {
let point = pointOfLineAndSegmentIntersect(begin1, end1, begin2, end2);
if(point != undefined) {
if(pointBelongsToSegment(point, begin1, end1) == false ) {
point = undefined;
}
}
return point;
}
module.exports = {
pointOfLinesIntersect: pointOfLinesIntersect,
pointOfLineAndRayIntersect: pointOfLineAndRayIntersect,
pointOfRaysIntersect: pointOfRaysIntersect,
pointOfLineAndSegmentIntersect: pointOfLineAndSegmentIntersect,
pointOfRayAndSegmentIntersect: pointOfRayAndSegmentIntersect,
pointOfSegmentsIntersect: pointOfSegmentsIntersect,
getXInLine: getXInLine,
getYInLine: getYInLine
};