-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpoligon.js
78 lines (74 loc) · 1.91 KB
/
poligon.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
module.exports = Poligon;
var Retangle = require("./retangle.js");
var Point = require("./point.js");
function Poligon() {
this.points = [];
}
Poligon.prototype = {
set x(value) {
this.changed = true;
this._x = value;
},
get x() {return this._x},
set y(value) {
this.changed = true;
this._y = value;
},
get y() {return this._y},
angle: 0,
//points: [],
resize: function(val) {
for(var i = 0; i < this.points.length; i++) {
this.points[i].x *= val;
this.points[i].y *= val;
}
},
rotate: function(ang) {
this.changed = true;
this.angle += ang;
for(var i = 0; i < this.points.length; i++) {
var tmp_vector = this.points[i].vector;
tmp_vector.ang += ang;
this.points[i] = tmp_vector.point;
}
},
add_vertice: function(x, y) {
this.changed = true;
var tmp_p = new Point(x, y);
this.points.push(tmp_p);
},
retangle_definition: function() {
if(! this.changed) return this.last_return;
var ret = new Retangle();
var min_x = this._element_factory.screen.width;
var max_x = 0;
var min_y = this._element_factory.screen.height;
var max_y = 0;
for(var i = 0; i < this.points.length; i++) {
if(this.points[i].x > max_x) max_x = this.points[i].x;
if(this.points[i].x < min_x) min_x = this.points[i].x;
if(this.points[i].y > max_y) max_y = this.points[i].y;
if(this.points[i].y < min_y) min_y = this.points[i].y;
}
ret.Ax = this.x + min_x;
ret.Ay = this.y + min_y;
ret.Bx = this.x + max_x;
ret.By = this.y + min_y;
ret.Cx = this.x + max_x;
ret.Cy = this.y + max_y;
ret.Dx = this.x + min_x;
ret.Dy = this.y + max_y;
this.last_return = ret;
this.changed = false;
return ret;
},
draw: function(screen) {
screen.ctx.moveTo(this.points[0].x, this.points[0].y);
for(var i = 1; i < this.points.length; i++) {
screen.ctx.lineTo(this.points[i].x, this.points[i].y);
}
if(!this.solid){
screen.ctx.lineTo(this.points[0].x, this.points[0].y);
}
}
};