Skip to content

Function intersectLine() created #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/math/Line3.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,65 @@ Object.assign( Line3.prototype, {

return line.start.equals( this.start ) && line.end.equals( this.end );

},

intersectLine: function (line, target) {

const eps = 1e-9;

let p1 = this.start;
let p2 = this.end;
let p3 = line.start;
let p4 = line.end;

let p43 = { x: p4.x - p3.x, y: p4.y - p3.y, z: p4.z - p3.z };
if (Math.abs(p43.x) < eps && Math.abs(p43.y) < eps && Math.abs(p43.z) < eps)
return false;

let p21 = { x: p2.x - p1.x, y: p2.y - p1.y, z: p2.z - p1.z };
if (Math.abs(p21.x) < eps && Math.abs(p21.y) < eps && Math.abs(p21.z) < eps)
return false;

let p13 = { x: p1.x - p3.x, y: p1.y - p3.y, z: p1.z - p3.z };

let d1343 = p13.x * p43.x + p13.y * p43.y + p13.z * p43.z;
let d4321 = p43.x * p21.x + p43.y * p21.y + p43.z * p21.z;
let d1321 = p13.x * p21.x + p13.y * p21.y + p13.z * p21.z;
let d4343 = p43.x * p43.x + p43.y * p43.y + p43.z * p43.z;
let d2121 = p21.x * p21.x + p21.y * p21.y + p21.z * p21.z;

let denom = d2121 * d4343 - d4321 * d4321;

if (Math.abs(denom) < eps)
return false;

let numer = d1343 * d4321 - d1321 * d4343;

let mua = numer / denom;
let mub = (d1343 + d4321 * mua) / d4343;

let pa = new THREE.Vector3();
let pb = new THREE.Vector3();

pa.x = p1.x + mua * p21.x;
pa.y = p1.y + mua * p21.y;
pa.z = p1.z + mua * p21.z;

pb.x = p3.x + mub * p43.x;
pb.y = p3.y + mub * p43.y;
pb.z = p3.z + mub * p43.z;

if( target == undefined )
target = new Line3(pa, pb);
else
target.set(pa, pb);

let distLa = this.closestPointToPointParameter(pa);
let distLb = line.closestPointToPointParameter(pb);

let linesIntersect = distLa >= 0 && distLa <= 1 && distLb >= 0 && distLb <= 1;

return linesIntersect;
}

} );
Expand Down