-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPoint.js
44 lines (38 loc) · 975 Bytes
/
Point.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
function Point(x, y, name){
this.name = name;
this.x = x;
this.y = y;
this.edge = null;
}
Point.prototype.distance = function(point){
return(Math.sqrt( this.distance2(point) ));
};
Point.prototype.distance2 = function(point){
return(sqr(point.x-this.x) + sqr(point.y-this.y));
};
Point.prototype.setEdge = function(e){
this.edge = e;
};
Point.prototype.toHTML = function(){
let edge = this.edge ? "<br/>"+this.edge.getName() : ''
return `<b>p${this.name}</b><br/>(${this.x}, ${this.y})${edge}`;
};
Point.prototype.toJSON = function(){
return JSON.stringify({
name: "p"+this.name,
x: this.x,
y: this.y
});
var string = `{"name":"p${this.name}", "x":${this.x}, "y":${this.y}}`;
return JSON.parse(string);
};
Point.prototype.getName = function(){
return "p"+this.name;
};
Point.prototype.moveTo = function(destination){
this.x = destination.x;
this.y = destination.y;
}
function sqr(x){
return x*x;
}