Skip to content

Commit 4948867

Browse files
committed
Step 7.15: Extend CPP line class
1 parent 9728863 commit 4948867

File tree

1 file changed

+1
-54
lines changed
  • resources/scripts/engine/geometry

1 file changed

+1
-54
lines changed

resources/scripts/engine/geometry/line.js

+1-54
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,10 @@
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) {
132
// Draws the line on the given context
143
draw(context) {
154
context.moveTo(this.x1, this.y1);
165
context.lineTo(this.x2, this.y2);
176
}
187

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-
448
getIntersection(shape) {
459
if (shape instanceof Engine.Geometry.Line)
4610
return this.getLineIntersection(shape);
@@ -50,23 +14,6 @@ Engine.Geometry.Line = class Line {
5014
return this.getPolygonIntersection(shape);
5115
}
5216

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-
7017
// line - circle intersection method
7118
getCircleIntersection(circle) {
7219
return circle.getLineIntersection(this);

0 commit comments

Comments
 (0)