1
- Engine . Geometry . Line = class Line {
2
- // x1 - The first point's x value
3
- // y1 - The first point's y value
4
- // x1 - The second point's x value
5
- // y2 - The second point's y value
6
- constructor ( x1 , y1 , x2 , y2 ) {
7
- this . x1 = Utils . trim ( x1 , 9 ) ;
8
- this . y1 = Utils . trim ( y1 , 9 ) ;
9
- this . x2 = Utils . trim ( x2 , 9 ) ;
10
- this . y2 = Utils . trim ( y2 , 9 ) ;
11
- }
12
-
1
+ Engine . Geometry . Line = class Line extends Utils . proxy ( CPP . Geometry . Line ) {
13
2
// Draws the line on the given context
14
3
draw ( context ) {
15
4
context . moveTo ( this . x1 , this . y1 ) ;
16
5
context . lineTo ( this . x2 , this . y2 ) ;
17
6
}
18
7
19
- // Gets the matching x value for a given y value
20
- getX ( y ) {
21
- let x = Utils . trim ( ( ( ( y - this . y1 ) * ( this . x2 - this . x1 ) ) / ( this . y2 - this . y1 ) ) + this . x1 , 9 ) ;
22
- if ( isNaN ( x ) || Utils . isBetween ( x , this . x1 , this . x2 ) ) return x ;
23
- }
24
-
25
- // Gets the matching y value for a given x value
26
- getY ( x ) {
27
- let y = Utils . trim ( ( ( ( x - this . x1 ) * ( this . y2 - this . y1 ) ) / ( this . x2 - this . x1 ) ) + this . y1 , 9 ) ;
28
- if ( isNaN ( y ) || Utils . isBetween ( y , this . y1 , this . y2 ) ) return y ;
29
- }
30
-
31
- // Returns if line has given point
32
- hasPoint ( x , y ) {
33
- if ( ! this . boundsHavePoint ( x , y ) ) return false ;
34
- let m = Utils . trim ( ( this . y2 - this . y1 ) / ( this . x2 - this . x1 ) , 9 ) ;
35
- return ( y - this . y1 ) / ( x - this . x1 ) == m ;
36
- }
37
-
38
- // Returns if given point is contained by the bounds aka cage of line
39
- boundsHavePoint ( x , y ) {
40
- return Utils . isBetween ( x , this . x1 , this . x2 ) &&
41
- Utils . isBetween ( y , this . y1 , this . y2 ) ;
42
- }
43
-
44
8
getIntersection ( shape ) {
45
9
if ( shape instanceof Engine . Geometry . Line )
46
10
return this . getLineIntersection ( shape ) ;
@@ -50,23 +14,6 @@ Engine.Geometry.Line = class Line {
50
14
return this . getPolygonIntersection ( shape ) ;
51
15
}
52
16
53
- // line - line intersection method
54
- getLineIntersection ( line ) {
55
- // Escape if lines are parallel
56
- if ( ! ( ( ( this . x1 - this . x2 ) * ( line . y1 - line . y2 ) ) - ( ( this . y1 - this . y2 ) * ( line . x1 - line . x2 ) ) ) ) return ;
57
-
58
- // Intersection point formula
59
- let x = Utils . trim ( ( ( ( ( this . x1 * this . y2 ) - ( this . y1 * this . x2 ) ) * ( line . x1 - line . x2 ) ) - ( ( this . x1 - this . x2 ) * ( ( line . x1 * line . y2 ) - ( line . y1 * line . x2 ) ) ) ) /
60
- ( ( ( this . x1 - this . x2 ) * ( line . y1 - line . y2 ) ) - ( ( this . y1 - this . y2 ) * ( line . x1 - line . x2 ) ) ) , 9 ) ;
61
- let y = Utils . trim ( ( ( ( ( this . x1 * this . y2 ) - ( this . y1 * this . x2 ) ) * ( line . y1 - line . y2 ) ) - ( ( this . y1 - this . y2 ) * ( ( line . x1 * line . y2 ) - ( line . y1 * line . x2 ) ) ) ) /
62
- ( ( ( this . x1 - this . x2 ) * ( line . y1 - line . y2 ) ) - ( ( this . y1 - this . y2 ) * ( line . x1 - line . x2 ) ) ) , 9 ) ;
63
-
64
- if ( Utils . isBetween ( x , this . x1 , this . x2 ) && Utils . isBetween ( x , line . x1 , line . x2 ) &&
65
- Utils . isBetween ( y , this . y1 , this . y2 ) && Utils . isBetween ( y , line . y1 , line . y2 ) ) {
66
- return { x, y } ;
67
- }
68
- }
69
-
70
17
// line - circle intersection method
71
18
getCircleIntersection ( circle ) {
72
19
return circle . getLineIntersection ( this ) ;
0 commit comments