-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
73 lines (51 loc) · 1.57 KB
/
test.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
62
63
64
65
66
67
68
69
70
var Point = function (x, y) {
this.x = x || 0;
this.y = y || 0;
}
var Line = function (a, b, c) {
if (! (this instanceof Line)){
return new Line(a, b, c);
} //evitare che l'utente debba per forza usare new
this.a = a;
this.b = b;
this.c = c;
}
Point.prototype.getDistanceFromLine = function(line){
return ((line.a*this.x) + (line.b*this.y) + (line.c)) / (Math.sqrt(Math.pow(line.a)) + (Math.pow(line.b)));
}
Point.prototype.getDistance = function (x) {
if (x instanceof Point){
//return this.getDistanceFromPoint(f);
}
if (x instanceof Line){
return this.getDistanceFromLine(x);
}
throw new Error('x is not a Point nor a Line');
}
var Triangle = function (p1, p2, p3) {
// this.p1 = p1;
// this.p2 = p2;
// this.p3 = p3;
this.points = [p1, p2, p3];
// this.l1 = p2.getDistance(p3);
// this.l2 = p3.getDistance(p1);
// this.l3 = p1.getDistance(p2);
}
Triangle.prototype.above = function(line){
return this.points.every(function(point) {
return point.getDistance(line) > 0;})
};
Triangle.prototype.below = function(line){
//return ((this.p1.getDistance(line) < 0) && (this.p2.getDistance(line) < 0) && (this.p3.getDistance(line) < 0));
return this.points.every(function(point) {
return point.getDistance(line) < 0;})
};
Triangle.prototype.intercect = function(line){
return (! (this.below(line) && (this.above(line)));
};
var Quad = function (p1, p2, p3, p4){
this.points = [p1, p2, p3, p4];
};
Quad.prototype.above = Triangle.prototype.above;
Quad.prototype.below = Triangle.prototype.below;
Quad.prototype.intercect = Triangle.prototype.intercect;