70 lines
1.3 KiB
JavaScript
70 lines
1.3 KiB
JavaScript
import { Point } from "./Point.js";
|
|
import { renderCube } from "../../3D/cube.js"
|
|
|
|
class Segment
|
|
{
|
|
/**
|
|
* @param {Point} start
|
|
* @param {Point} stop
|
|
*/
|
|
constructor(start, stop)
|
|
{
|
|
/**
|
|
* @type {Point}
|
|
*/
|
|
this.start = start === undefined ? new Point() : start;
|
|
|
|
/**
|
|
* @type {Point}
|
|
*/
|
|
this.stop = stop === undefined ? new Point() : stop;
|
|
|
|
}
|
|
|
|
angle()
|
|
{
|
|
let x = this.start.x - this.stop.x,
|
|
y = this.start.y - this.stop.y;
|
|
|
|
return Math.atan2(y, x);
|
|
}
|
|
|
|
len()
|
|
{
|
|
let x = this.start.x - this.stop.x,
|
|
y = this.start.y - this.stop.y;
|
|
|
|
return (x ** 2 + y ** 2) ** (1 / 2);
|
|
}
|
|
|
|
/**
|
|
* @param {CanvasRenderingContext2D} ctx
|
|
*/
|
|
draw(ctx)
|
|
{
|
|
if(ctx instanceof CanvasRenderingContext2D)
|
|
{
|
|
ctx.moveTo(this.start.x, this.start.y);
|
|
ctx.lineTo(this.stop.x, this.stop.y);
|
|
}
|
|
else if(ctx instanceof WebGLRenderingContext)
|
|
{
|
|
renderCube(ctx, this.start.x, -3, this.start.y, 0, 0.5, 0.5, 0.5);
|
|
}
|
|
else
|
|
{
|
|
alert('Unknown rendering context type');
|
|
}
|
|
}
|
|
|
|
from_json(data)
|
|
{
|
|
this.start = this.start.from_json(data.start);
|
|
this.stop = this.stop.from_json(data.stop);
|
|
|
|
return this;
|
|
}
|
|
}
|
|
|
|
export { Segment }
|