cga.js
Version:
Xtor Compute Geometry Algorithm Libary 计算几何算法库
51 lines (42 loc) • 1.08 kB
text/typescript
import { v3, Vec3 } from '../../math/Vec3';
import { DistanceResult, IntersectResult } from './result';
export class Segment extends Array {
center: Vec3;
extentDirection: Vec3;
extentSqr: number;
extent: number;
direction: Vec3;
normal: Vec3 | undefined;
/**
* 线段
* @param {Point|Vec3} p0
* @param {Point|Vec3} p1
*/
constructor(_p0: Vec3 = v3(), _p1: Vec3 = v3()) {
super();
Object.setPrototypeOf(this, Segment.prototype);
this.push(_p0, _p1);
this.center = _p0.clone()
.add(_p1)
.multiplyScalar(0.5);
this.extentDirection = _p1.clone().sub(_p0);
this.extentSqr = this.extentDirection.lengthSq();
this.extent = Math.sqrt(this.extentSqr);
this.direction = this.extentDirection.clone().normalize();
}
get p0() {
return this[0];
}
set p0(v: Vec3) {
this[0].copy(v);
}
get p1() {
return this[1];
}
set p1(v: Vec3) {
this[1].copy(v);
}
}
export function segment(p0: Vec3, p1: Vec3) {
return new Segment(p0, p1);
}