-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLine.ts
257 lines (193 loc) · 6.71 KB
/
Line.ts
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//////////
// LINE //
//////////
/*
A line in two dimensions, from one point to another
*/
import { Material } from './Material.js'
import { RayHit } from './RayHit.js'
import { Vec2 } from './Vec2.js'
import { between } from './util.js'
export class Line {
p1: Vec2 = new Vec2(0, 0); // first point
p2: Vec2 = new Vec2(0, 0); // second point
material: Material = new Material( 0, 0, 0 );
constructor( x1: number, y1: number,
x2: number, y2: number ) {
this.p1.setValues( x1, y1 );
this.p2.setValues( x2, y2 );
}
copy(): Line {
let line = new Line( this.p1.x, this.p1.y, this.p2.x, this.p2.y );
if ( !this.material ) line.material = null;
else line.material = this.material.copy();
return line;
}
static fromPoints ( p1: Vec2, p2: Vec2 ) {
let line = new Line( 0, 0, 0, 0 );
line.p1 = p1;
line.p2 = p2;
return line;
}
toString(): string {
return '<(' + this.p1.x + ',' + this.p1.y + '),(' + this.p2.x + ',' + this.p2.y + ')>';
}
getDirection(): Vec2 {
return this.p2.minus( this.p1 );
}
/*
intersects()
find the point of intersection with another line
line: the other line
infinite: whether the other line is unbounded
returns: the intersection point, or null
*/
intersects( line: Line, infinite: boolean=false ): Vec2 | null {
/*
// bounding box filter
if ( !infinite ) {
if ( line.p1.x > this.p1.x && line.p1.x > this.p2.x &&
line.p2.x > this.p1.x && line.p2.x > this.p2.x ) return null;
if ( line.p1.x < this.p1.x && line.p1.x < this.p2.x &&
line.p2.x < this.p1.x && line.p2.x < this.p2.x ) return null;
if ( line.p1.y > this.p1.y && line.p1.y > this.p2.y &&
line.p2.y > this.p1.y && line.p2.y > this.p2.y ) return null;
if ( line.p1.y < this.p1.y && line.p1.y < this.p2.y &&
line.p2.y < this.p1.y && line.p2.y < this.p2.y ) return null;
}
// cross product filter
let v: Array<number>;
let side1, side2: number;
// points of line on same side of this
if ( !infinite ) {
v = [
0, 0,
this.p2.x - this.p1.x, this.p2.y - this.p1.y,
line.p1.x - this.p1.x, line.p1.y - this.p1.y,
line.p2.x - this.p1.x, line.p2.y - this.p1.y
];
side1 = v[2] * v[5] - v[4] * v[3];
side2 = v[2] * v[7] - v[6] * v[3];
if ( side1 < -1 && side2 < -1 ) return null;
if ( side1 > 1 && side2 > 1 ) return null;
}
// points of this on same side of line
v = [
this.p1.x - line.p1.x, this.p1.y - line.p1.y,
this.p2.x - line.p1.x, this.p2.y - line.p1.y,
0, 0,
line.p2.x - line.p1.x, line.p2.y - line.p1.y
];
side1 = v[6] * v[1] - v[0] * v[7];
side2 = v[6] * v[3] - v[2] * v[7];
if ( side1 < -1 && side2 < -1 ) return null;
if ( side1 > 1 && side2 > 1 ) return null;
*/
// find intersection
let result = null;
//let dx_this = Math.abs( this.p2.x - this.p1.x ); // EDIT
//let dx_line = Math.abs( line.p2.x - line.p1.x ); // EDIT
let dx_this = this.p2.x - this.p1.x;
if ( dx_this < 0 ) dx_this *= -1;
let dx_line = line.p2.x - line.p1.x;
if ( dx_line < 0 ) dx_line *= -1;
if ( dx_this < 0.01 && dx_line < 0.01 ) {
// both vertical, do nothing, return null
} else if ( dx_this < 0.01 ) {
let slope2 = ( line.p2.y - line.p1.y ) / ( line.p2.x - line.p1.x );
let yInt2 = line.p1.y - ( line.p1.x - this.p1.x ) * slope2;
if ( between( yInt2, this.p1.y, this.p2.y ) &&
( between( this.p1.x, line.p1.x, line.p2.x ) || infinite ) ) {
//( between( yInt2, line.p1.y, line.p2.y ) || infinite ) ) { // not necessary?
result = new Vec2( this.p1.x, yInt2 );
}
} else if ( dx_line < 0.01 ) {
let slope1 = ( this.p2.y - this.p1.y ) / ( this.p2.x - this.p1.x );
let yInt1 = this.p1.y - ( this.p1.x - line.p1.x ) * slope1;
if ( ( between( yInt1, line.p1.y, line.p2.y ) || infinite ) &&
between( line.p1.x, this.p1.x, this.p2.x ) ) {
result = new Vec2( line.p1.x, yInt1 );
}
} else {
let slope1 = ( this.p2.y - this.p1.y ) / ( this.p2.x - this.p1.x );
let slope2 = ( line.p2.y - line.p1.y ) / ( line.p2.x - line.p1.x );
if ( slope1 != slope2 ) {
let yInt2 = line.p1.y - this.p1.y - (line.p1.x - this.p1.x) * slope2; // use this.p1 as origin
let intX = yInt2 / ( slope1 - slope2 ) + this.p1.x; // yInt1 is 0 with this.p1 as origin
let intY = ( intX - this.p1.x ) * slope2 + yInt2 + this.p1.y; // y = mx + b
if ( between( intX, this.p1.x, this.p2.x ) &&
( between( intX, line.p1.x, line.p2.x ) || infinite ) &&
between( intY, this.p1.y, this.p2.y ) &&
( between( intY, line.p1.y, line.p2.y ) || infinite ) ) {
result = new Vec2( intX, intY );
}
}
}
return result;
}
/*
rayIntersect()
this: a ray from p1 to p2
otherLine: a flat surface to reflect off of
returns: a RayHit object with the hit location and surface normal
*/
rayIntersect( otherLine: Line ): RayHit | null {
let intersection = this.intersects( otherLine );
if ( intersection === null ) {
return null;
}
// Rotate the line 90 degrees to get a normal vector
let normal = new Vec2( otherLine.p1.y - otherLine.p2.y,
otherLine.p2.x - otherLine.p1.x ).normalize();
// Flip the normal if it's pointing in the same general direction as our ray
if ( this.p1.minus( intersection ).dot( normal ) < 0 ) {
normal.flip();
}
return new RayHit( intersection, normal, otherLine.material );
}
// check whether any of the points are on different sides of the line
/*
if a is the vector from line.p1 to the point,
and the "line" is the infinite line drawn from the unit segment b:
|b| = 1
sin(theta) = opp / hyp = distance_to_line / |a|
a cross b = |a| * |b| * sin(theta)
= |a| * 1 * distance_to_line / |a|
= distance_to_line
*/
whichSide( points: Array<Vec2> ): Array<number> {
let sides: Array<number> = [];
let v1 = this.p2.minus( this.p1 ).normalize();
for ( let i = 0; i < points.length; i++ ) {
let distFromLine = v1.cross( points[i].minus( this.p1 ) );
if ( distFromLine < -0.01 ) {
sides.push( -1 );
} else if ( distFromLine > 0.01 ) {
sides.push ( 1 );
} else {
sides.push( 0 );
}
}
return sides;
}
sortAlong( points: Array<Vec2> ): void {
if ( Math.abs( this.p2.x - this.p1.x ) < 0.01 ) {
points.sort( ( a, b ) => ( a.y - b.y ) / ( this.p2.y - this.p1.y ) );
} else {
points.sort( ( a, b ) => ( a.x - b.x ) / ( this.p2.x - this.p1.x ) );
}
}
sortAlongFunc( a: Vec2, b: Vec2 ): number {
if ( Math.abs( this.p2.x - this.p1.x ) < 0.01 ) {
return ( a.y - b.y ) / ( this.p2.y - this.p1.y );
} else {
return ( a.x - b.x ) / ( this.p2.x - this.p1.x );
}
}
draw( context: CanvasRenderingContext2D ): void {
context.beginPath();
context.moveTo( this.p1.x, this.p1.y );
context.lineTo( this.p2.x, this.p2.y );
context.stroke();
}
}