-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathLine.js
61 lines (43 loc) · 1.29 KB
/
Line.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
class DiagonalCollisionInfo {
constructor() {
this.collisionPoints = []
this.leftSideOfPlayerCollided = false;
this.rightSideOfPlayerCollided = false;
this.topSideOfPlayerCollided = false;
this.bottomSideOfPlayerCollided = false;
}
}
class Line {
constructor(x1, y1, x2, y2) {
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
this.isHorizontal = y1 === y2;
this.isVertical = x1 === x2;
this.isDiagonal = !(this.isHorizontal || this.isVertical)
this.ensurePointsAreInOrder();
this.midPoint = createVector((x1+x2)/2,(y1+y2)/2);
this.diagonalCollisionInfo = new DiagonalCollisionInfo();
}
Show() {
push();
stroke(255,0,0)
strokeWeight(3)
line(this.x1, this.y1, this.x2, this.y2);
ellipse(this.midPoint.x,this.midPoint.y,10,10)
pop();
}
ensurePointsAreInOrder() {
if(this.isHorizontal || this.isVertical){
if (this.x1>this.x2 || this.y1 > this.y2){
let temp = this.x1;
this.x1 = this.x2;
this.x2 = temp;
temp = this.y1;
this.y1 = this.y2;
this.y2 = temp;
}
}
}
}