Path: blob/main/public/games/files/garbage-collector/js/geometry/segment.js
1036 views
/*globals define*/1define([2'object2d',3'utils'4], function( Object2D, Utils ) {5'use strict';67function Segment( x0, y0, x1, y1 ) {8Object2D.call( this, 0, 0 );910this.x0 = x0 || 0;11this.y0 = y0 || 0;12this.x1 = x1 || 0;13this.y1 = y1 || 0;14}1516Segment.prototype = new Object2D();17Segment.prototype.constructor = Segment;1819Segment.prototype.drawPath = function( ctx ) {20ctx.beginPath();21ctx.moveTo( this.x0, this.y0 );22ctx.lineTo( this.x1, this.y1 );23ctx.closePath();24};2526Segment.prototype.drawNormals = function( ctx, options ) {27options = options || {};2829var length = options.length || 10,30lineWidth = options.lineWidth || 2,31stroke = options.stroke || '#0f0';3233ctx.beginPath();3435var x0 = this.x0,36y0 = this.y0,37x1 = this.x1,38y1 = this.y1;3940var mx = 0.5 * ( x0 + x1 ),41my = 0.5 * ( y0 + y1 );4243var normal = Utils.lineNormal( x0, y0, x1, y1 );44if ( !normal ) {45return;46}4748ctx.moveTo( mx, my );49ctx.lineTo( mx + normal.x * length, my + normal.y * length );5051ctx.lineWidth = lineWidth;52ctx.strokeStyle = stroke;53ctx.stroke();54};5556Segment.prototype.random = function() {57return Utils.lerp2d(58this.x0, this.y0,59this.x1, this.y1,60Math.random()61);62};6364Object.defineProperty( Segment.prototype, 'width', {65get: function() {66return Math.abs( this.x1 - this.x0 );67}68});6970Object.defineProperty( Segment.prototype, 'height', {71get: function() {72return Math.abs( this.y1 - this.y0 );73}74});7576return Segment;77});787980