Path: blob/main/public/games/files/garbage-collector/js/geometry/rect.js
1036 views
/*globals define*/1define([2'object2d'3], function( Object2D ) {4'use strict';56function Rect( x, y, width, height ) {7Object2D.call( this, x, y );89this.width = width || 0;10this.height = height || 0;11}1213Rect.prototype = new Object2D();14Rect.prototype.constructor = Rect;1516Rect.prototype.drawPath = function( ctx ) {17ctx.beginPath();18ctx.rect( -0.5 * this.width, -0.5 * this.height, this.width, this.height );19ctx.closePath();20};2122Rect.prototype.random = function() {23return {24x: this.x + ( Math.random() - 0.5 ) * this.width,25y: this.y + ( Math.random() - 0.5 ) * this.height26};27};2829Rect.prototype.contains = function( x, y ) {30var point = this.toLocal( x, y );31x = point.x;32y = point.y;3334var halfWidth = 0.5 * this.width,35halfHeight = 0.5 * this.height;3637return -halfWidth <= x && x <= halfWidth &&38-halfHeight <= y && y <= halfHeight;39};4041Object.defineProperty( Rect.prototype, 'left', {42get: function() {43return this.x - 0.5 * this.width;44},4546set: function( left ) {47this.x = left + 0.5 * this.width;48}49});5051Object.defineProperty( Rect.prototype, 'right', {52get: function() {53return this.x + 0.5 * this.width;54},5556set: function( right ) {57this.x = right - 0.5 * this.width;58}59});6061Object.defineProperty( Rect.prototype, 'top', {62get: function() {63return this.y - 0.5 * this.height;64},6566set: function( top ) {67this.y = top + 0.5 * this.height;68}69});7071Object.defineProperty( Rect.prototype, 'bottom', {72get: function() {73return this.y + 0.5 * this.height;74},7576set: function( bottom ) {77this.y = bottom - 0.5 * this.height;78}79});8081return Rect;82});838485