Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/garbage-collector/js/geometry/rect.js
1036 views
1
/*globals define*/
2
define([
3
'object2d'
4
], function( Object2D ) {
5
'use strict';
6
7
function Rect( x, y, width, height ) {
8
Object2D.call( this, x, y );
9
10
this.width = width || 0;
11
this.height = height || 0;
12
}
13
14
Rect.prototype = new Object2D();
15
Rect.prototype.constructor = Rect;
16
17
Rect.prototype.drawPath = function( ctx ) {
18
ctx.beginPath();
19
ctx.rect( -0.5 * this.width, -0.5 * this.height, this.width, this.height );
20
ctx.closePath();
21
};
22
23
Rect.prototype.random = function() {
24
return {
25
x: this.x + ( Math.random() - 0.5 ) * this.width,
26
y: this.y + ( Math.random() - 0.5 ) * this.height
27
};
28
};
29
30
Rect.prototype.contains = function( x, y ) {
31
var point = this.toLocal( x, y );
32
x = point.x;
33
y = point.y;
34
35
var halfWidth = 0.5 * this.width,
36
halfHeight = 0.5 * this.height;
37
38
return -halfWidth <= x && x <= halfWidth &&
39
-halfHeight <= y && y <= halfHeight;
40
};
41
42
Object.defineProperty( Rect.prototype, 'left', {
43
get: function() {
44
return this.x - 0.5 * this.width;
45
},
46
47
set: function( left ) {
48
this.x = left + 0.5 * this.width;
49
}
50
});
51
52
Object.defineProperty( Rect.prototype, 'right', {
53
get: function() {
54
return this.x + 0.5 * this.width;
55
},
56
57
set: function( right ) {
58
this.x = right - 0.5 * this.width;
59
}
60
});
61
62
Object.defineProperty( Rect.prototype, 'top', {
63
get: function() {
64
return this.y - 0.5 * this.height;
65
},
66
67
set: function( top ) {
68
this.y = top + 0.5 * this.height;
69
}
70
});
71
72
Object.defineProperty( Rect.prototype, 'bottom', {
73
get: function() {
74
return this.y + 0.5 * this.height;
75
},
76
77
set: function( bottom ) {
78
this.y = bottom - 0.5 * this.height;
79
}
80
});
81
82
return Rect;
83
});
84
85