Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/garbage-collector/js/geometry/segment.js
1036 views
1
/*globals define*/
2
define([
3
'object2d',
4
'utils'
5
], function( Object2D, Utils ) {
6
'use strict';
7
8
function Segment( x0, y0, x1, y1 ) {
9
Object2D.call( this, 0, 0 );
10
11
this.x0 = x0 || 0;
12
this.y0 = y0 || 0;
13
this.x1 = x1 || 0;
14
this.y1 = y1 || 0;
15
}
16
17
Segment.prototype = new Object2D();
18
Segment.prototype.constructor = Segment;
19
20
Segment.prototype.drawPath = function( ctx ) {
21
ctx.beginPath();
22
ctx.moveTo( this.x0, this.y0 );
23
ctx.lineTo( this.x1, this.y1 );
24
ctx.closePath();
25
};
26
27
Segment.prototype.drawNormals = function( ctx, options ) {
28
options = options || {};
29
30
var length = options.length || 10,
31
lineWidth = options.lineWidth || 2,
32
stroke = options.stroke || '#0f0';
33
34
ctx.beginPath();
35
36
var x0 = this.x0,
37
y0 = this.y0,
38
x1 = this.x1,
39
y1 = this.y1;
40
41
var mx = 0.5 * ( x0 + x1 ),
42
my = 0.5 * ( y0 + y1 );
43
44
var normal = Utils.lineNormal( x0, y0, x1, y1 );
45
if ( !normal ) {
46
return;
47
}
48
49
ctx.moveTo( mx, my );
50
ctx.lineTo( mx + normal.x * length, my + normal.y * length );
51
52
ctx.lineWidth = lineWidth;
53
ctx.strokeStyle = stroke;
54
ctx.stroke();
55
};
56
57
Segment.prototype.random = function() {
58
return Utils.lerp2d(
59
this.x0, this.y0,
60
this.x1, this.y1,
61
Math.random()
62
);
63
};
64
65
Object.defineProperty( Segment.prototype, 'width', {
66
get: function() {
67
return Math.abs( this.x1 - this.x0 );
68
}
69
});
70
71
Object.defineProperty( Segment.prototype, 'height', {
72
get: function() {
73
return Math.abs( this.y1 - this.y0 );
74
}
75
});
76
77
return Segment;
78
});
79
80