Skip to content

Commit cd229b4

Browse files
committed
Step 5.3: Create 'Line' class
1 parent 567efd9 commit cd229b4

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
13+
// Draws the line on the given context
14+
draw(context) {
15+
context.moveTo(this.x1, this.y1);
16+
context.lineTo(this.x2, this.y2);
17+
}
18+
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+
getIntersection(shape) {
45+
if (shape instanceof Engine.Geometry.Line)
46+
return this.getLineIntersection(shape);
47+
}
48+
49+
// line - line intersection method
50+
getLineIntersection(line) {
51+
// Escape if lines are parallel
52+
if (!(((this.x1 - this.x2) * (line.y1 - line.y2)) - ((this.y1 - this.y2) * (line.x1 - line.x2)))) return;
53+
54+
// Intersection point formula
55+
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)))) /
56+
(((this.x1 - this.x2) * (line.y1 - line.y2)) - ((this.y1 - this.y2) * (line.x1 - line.x2))), 9);
57+
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)))) /
58+
(((this.x1 - this.x2) * (line.y1 - line.y2)) - ((this.y1 - this.y2) * (line.x1 - line.x2))), 9);
59+
60+
if (Utils.isBetween(x, this.x1, this.x2) && Utils.isBetween(x, line.x1, line.x2) &&
61+
Utils.isBetween(y, this.y1, this.y2) && Utils.isBetween(y, line.y1, line.y2)) {
62+
return { x, y };
63+
}
64+
}
65+
};

views/game.html

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<!-- Scripts -->
1111
<script type="text/javascript" src="/scripts/utils.js"></script>
1212
<script type="text/javascript" src="/scripts/namespaces.js"></script>
13+
<script type="text/javascript" src="/scripts/engine/geometry/line.js"></script>
1314
<script type="text/javascript" src="/scripts/engine/restorable.js"></script>
1415
<script type="text/javascript" src="/scripts/engine/font.js"></script>
1516
<script type="text/javascript" src="/scripts/engine/sprite.js"></script>

0 commit comments

Comments
 (0)