-
Notifications
You must be signed in to change notification settings - Fork 0
/
Punto.js
159 lines (93 loc) · 2.57 KB
/
Punto.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
var Punto = function (x , y) {
this.x = x || 0;
this.y = y || 0;
}
Punto.prototype.getDistanceFromPoint = function (p) {
return Math.sqrt(Math.pow((p.x - this.x),2)+Math.pow((p.y - this.y),2));
};
var Triangolo = function (p1 , p2 , p3) {
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
}
Triangolo.prototype.getPerimetro = function () {
return this.p1.getDistanza(p2) + this.p1.getDistanza(p3) + this.p2.getDistanza(p3);
};
Triangolo.prototype.getArea = function () {
var p = this.getPerimetro() / 2;
var a = this.p1.getDistanza(this.p2);
var b = this.p1.getDistanza(this.p3);
var c = this.p2.getDistanza(this.p3);
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
Punto.prototype.translate = function (dx,dy) {
this.x += dx;
this.y += dy;
return this;
}
var rand = function (minim,maxim) {
return minim + Math.random() * (maxim - minim);
}
var RandomPoint = function () {
var p = new Punto(rand(-100,100),rand(-100,100));
return p ;
}
var generaNpunti = function (n) {
var res = [];
for (var a = 0; a < n ; a++) {
res.push(RandomPoint());
}
return res;
}
var bisettrice = function (p) {
return p.y - p.x > 0;
}
var planFilter = function (punti, f) {
var pts = punti || [];
var res = [];
for(var i = 0; i < punti.length; i++){
if( f(punti[i]) )
res.push(punti[i]);
}
return res;
}
Punto.prototype.membership = function (f) {
if (f(this)>0) {
return 1;
}
else if (f(this)<0) {
return -1;
}
return 0;
}
pow = Math.pow;
sqrt = Math.sqrt;
abs = Math.abs;
Punto.prototype.getDistanceFromLine = function (line) {
return abs(line.a*this.x + line.b * this.y + line.c)/sqrt((pow(line.a,2) + pow(line.b,2)));
}
Punto.prototype.getDistance = function (x) {
if (x istanceof Punto) {
return this.getDistanceFromPoint(x);
}
if (x istanceof Line) {
return this.getDistanceFromLine(x);
}
throw new Error('x is not a Point nor a Line');
};
Triangolo.prototype.above = function (linea) {
var ar = [this.p1, this.p2, this.p3];
return ar.every(function(el, ind, ar) { return linea.getDistanceFromPoint(el) > 0; });
}
Triangolo.prototype.below = function (linea) {
var ar = [this.p1, this.p2, this.p3];
return ar.every(function(el, ind, ar) { return linea.getDistanceFromPoint(el) < 0; });
}
Triangolo.prototype.intersect = function (linea) {
return !this.above(linea) && !this.below(linea);
}
var Quad = function (p1, p2, p3, p4) {
this.points = [p1, p2, p3, p4];
}
Quad.prototype.above = Triangolo.prototype.above;
Quad.prototype.below = Triangolo.prototype.below;