Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/garbage-collector/js/geometry/circle.js
1036 views
1
/*globals define*/
2
define([
3
'object2d',
4
'utils'
5
], function( Object2D, Utils ) {
6
'use strict';
7
8
function Circle( x, y, radius ) {
9
Object2D.call( this, x, y );
10
11
this.radius = radius || 0;
12
}
13
14
Circle.prototype = new Object2D();
15
Circle.prototype.constructor = Circle;
16
17
Circle.prototype.drawPath = function( ctx ) {
18
ctx.beginPath();
19
ctx.arc( 0, 0, this.radius, 0, 2 * Math.PI );
20
ctx.closePath();
21
};
22
23
Circle.prototype.random = function() {
24
// Calculates r in [0, 1) and theta in [0, 2PI).
25
// Note that using sqrt() results in a uniform distribution:
26
// x^2 + y^2 = r^2.
27
var radius = this.radius * Math.sqrt( Math.random() ),
28
theta = 2 * Math.PI * Math.random();
29
30
return {
31
x: radius * Math.cos( theta ),
32
y: radius * Math.sin( theta )
33
};
34
};
35
36
Circle.prototype.contains = function( x, y ) {
37
return Utils.distanceSquared( x, y, this.x, this.y ) <= this.radius * this.radius;
38
};
39
40
Object.defineProperty( Circle.prototype, 'left', {
41
get: function() {
42
return this.x - this.radius;
43
},
44
45
set: function( left ) {
46
this.x = left + this.radius;
47
}
48
});
49
50
Object.defineProperty( Circle.prototype, 'right', {
51
get: function() {
52
return this.x + this.radius;
53
},
54
55
set: function( right ) {
56
this.x = right - this.radius;
57
}
58
});
59
60
Object.defineProperty( Circle.prototype, 'top', {
61
get: function() {
62
return this.y - this.radius;
63
},
64
65
set: function( top ) {
66
this.y = top + this.radius;
67
}
68
});
69
70
Object.defineProperty( Circle.prototype, 'bottom', {
71
get: function() {
72
return this.y + this.radius;
73
},
74
75
set: function( bottom ) {
76
this.y = bottom - this.radius;
77
}
78
});
79
80
Object.defineProperty( Circle.prototype, 'diameter', {
81
get: function() {
82
return this.radius * 2;
83
}
84
});
85
86
Object.defineProperty( Circle.prototype, 'width', {
87
get: function() {
88
return this.diameter;
89
}
90
});
91
92
Object.defineProperty( Circle.prototype, 'height', {
93
get: function() {
94
return this.diameter;
95
}
96
});
97
98
return Circle;
99
});
100
101