Path: blob/main/public/games/files/garbage-collector/js/geometry/circle.js
1036 views
/*globals define*/1define([2'object2d',3'utils'4], function( Object2D, Utils ) {5'use strict';67function Circle( x, y, radius ) {8Object2D.call( this, x, y );910this.radius = radius || 0;11}1213Circle.prototype = new Object2D();14Circle.prototype.constructor = Circle;1516Circle.prototype.drawPath = function( ctx ) {17ctx.beginPath();18ctx.arc( 0, 0, this.radius, 0, 2 * Math.PI );19ctx.closePath();20};2122Circle.prototype.random = function() {23// Calculates r in [0, 1) and theta in [0, 2PI).24// Note that using sqrt() results in a uniform distribution:25// x^2 + y^2 = r^2.26var radius = this.radius * Math.sqrt( Math.random() ),27theta = 2 * Math.PI * Math.random();2829return {30x: radius * Math.cos( theta ),31y: radius * Math.sin( theta )32};33};3435Circle.prototype.contains = function( x, y ) {36return Utils.distanceSquared( x, y, this.x, this.y ) <= this.radius * this.radius;37};3839Object.defineProperty( Circle.prototype, 'left', {40get: function() {41return this.x - this.radius;42},4344set: function( left ) {45this.x = left + this.radius;46}47});4849Object.defineProperty( Circle.prototype, 'right', {50get: function() {51return this.x + this.radius;52},5354set: function( right ) {55this.x = right - this.radius;56}57});5859Object.defineProperty( Circle.prototype, 'top', {60get: function() {61return this.y - this.radius;62},6364set: function( top ) {65this.y = top + this.radius;66}67});6869Object.defineProperty( Circle.prototype, 'bottom', {70get: function() {71return this.y + this.radius;72},7374set: function( bottom ) {75this.y = bottom - this.radius;76}77});7879Object.defineProperty( Circle.prototype, 'diameter', {80get: function() {81return this.radius * 2;82}83});8485Object.defineProperty( Circle.prototype, 'width', {86get: function() {87return this.diameter;88}89});9091Object.defineProperty( Circle.prototype, 'height', {92get: function() {93return this.diameter;94}95});9697return Circle;98});99100101