-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw05.js
110 lines (72 loc) · 2.11 KB
/
hw05.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
/* PUNTO */
function Point(x,y) {
var x;
var y;
this.getX = function(){return x;};
this.getY = function(){return y;};
this.setX = function(coordinataX){this.x = coordinataX;};
this.setY = function(coordinataY){this.y = coordinataY;};
this.setX(x);
this.setY(y);
};
Point.prototype.getDistance = function (p2){
var ascissa = Math.abs(this.getX() - p2.getX());
var ordinata = Math.abs(this.getY() - p2.getY());
var distance = Math.sqrt(Math.pow(ascissa,2) + Math.pow(ordinata,2));
return distance;
};
/* Traslazione del punto */
Point.prototype.translate = function(dx,dy) {
this.setY(this.getY() + dy);
this.setX(this.getX() + dx);
};
/* Random Point */
var randomPoint = function(){
var random = Math.random;
var x = (random()*200) - 100;
var y = (random()*200) - 100;
return new Point(x,y);
};
/* Random Points */
var generatePoints = function(lenght) {
var lenght = lenght || 5;
var array = [];
var i = 0;
while(i<lenght){
array.push(randomPoint());
i++;
}
return array;
};
/* TRIANGOLO */
function Triangle(vert1, vert2, vert3) {
var vertex1 = vert1;
var vertex2 = vert2;
var vertex3 = vert3;
this.getVertex1 = function(){return vertex1;};
this.getVertex2 = function(){return vertex2;};
this.getVertex3 = function(){return vertex3;};
};
Triangle.prototype.getPerimeter = function (){
var dist12 = this.getVertex1().getDistance(this.getVertex2());
var dist13 = this.getVertex1().getDistance(this.getVertex3());
var dist23 = this.getVertex2().getDistance(this.getVertex3());
return (dist12 + dist13 + dist23);
};
Triangle.prototype.getArea = function (){
var dist12 = this.getVertex1().getDistance(this.getVertex2());
var dist13 = this.getVertex1().getDistance(this.getVertex3());
var dist23 = this.getVertex2().getDistance(this.getVertex3());
var semPer = this.getPerimeter()/2;
return Math.sqrt(semPer*(semPer - dist12)*(semPer - dist13)*(semPer - dist23));
};
// *** PROVE DI TEST ***
var p1 = new Point(1,1);
var p2 = new Point(5,1);
var p3 = new Point(3,3);
var tri = new Triangle(p1,p2,p3);
p1.getX();
p1.getY();
p1.getDistance(p2);
tri.getArea();
tri.getPerimeter();