-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomework.js
93 lines (67 loc) · 1.68 KB
/
homework.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
var sqrt = Math.sqrt;
var pow = Math.pow;
var random = Math.random;
/* POINT */
var Point = function (x, y) {
this.x = x || 0;
this.y = y || 0;
}
Point.prototype.getDistance = function(point) {
return sqrt(pow(point.y - this.y, 2) + pow(point.x - this.y, 2));
};
Point.prototype.translate = function(dx, dy) {
this.x += dx;
this.y += dy;
return this;
};
/* TRIANGLE */
var Triangle = function (p1, p2, p3) {
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
this.l1 = p2.getDistance(p3);
this.l2 = p3.getDistance(p1);
this.l3 = p1.getDistance(p2);
}
Triangle.prototype.getPerimeter = function() {
return this.l1 + this.l2 + this.l3;
};
Triangle.prototype.getArea = function() {
var p = this.getPerimeter() / 2;
return sqrt(p*(p - this.l1)*(p - this.l2)*(p - this.l3));
};
var randomPoint = function () {
var x1 = random() * 200 - 100;
var y1 = random() * 200 - 100;
return new Point(x1, y1);
};
var randomPoints = function (n) {
var n = n || 1;
var res = new Array(n);
for (var i = 0; i < n ; i += 1) {
res[i] = randomPoint();
}
return res;
}
var points = randomPoints(100);
var sopraLaBisettrice = function (array){
var test = function (point){
return point.y - point.x > 0 ;
}
var result = array.filter (function (item,index,array){
return test(item);
})
return result ;
}
var Line = function (a, b, c){
if (!(this instanceof Line )){
return new Line (a,b,c);
}
this.a = a || 0;
this.b = b || 0;
this.c = c || 0;
}
Point.prototype.distance = function (Line){
var distance = Math.abs((( line.a * this.x ) + (line.b * this.y) + line.c )) / Math.sqrt ((Math.pow(line.a,2) + Math.pow(line.b,2))
return distance;
}