npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

line-segment-distance

v1.0.1

Published

a methods to compute line to line, line to segment and segment to segment in 3D space

Downloads

7

Readme

line-segment-distance

The method used in this library is from http://geomalgorithms.com/a07-_distance.html, go check out the website if you want to leearn more about geometry mathematic.

npm install line-segment-distance

import {
    Line,
    Segment,
    dist3D_Line_to_Line,
    dist3D_Line_to_Segment,
    dist3D_Segment_to_Segment
} from 'line-segment-distance';

dist3D_Line_to_Line

Return the minimum distance between LineA and LineB in 3D space, and the scale factor form each line's origin position

Parameters

  • L1: Line
  • L2: Line
type Line = {
    origin: vec3 | number[],
    dir: vec3 | number[],
}

example:

const result = dist3D_Line_to_Line(L1, L2);

return

{
    distance: number,
    l1_scale: number,
    l2_scale: number,
}

Examples

import {
    dist3D_Line_to_Line,
    Line,
} from 'line-segment-distance';
import { vec3 } from 'gl-matrix';

const lineA:Line = {
    origin: [0, 0, 0],
    dir: [1, 0, 1],
};

const lineB:Line = {
    origin: [32, 0, 0],
    dir: [-1, 1, 1],
};

const result = dist3D_Line_to_Line(lineA, lineB);
const distance = result.distance;
if (distance < 1e-6) {
    // lineA intersects the lineB.
}
const pointOnLineA = vec3.scaleAndAdd(vec3.create(), lineA.origin, lineA.dir, result.l1_scale);

dist3D_Line_to_Segment

Return the minimum distance between LineA and SegmentB in 3D space, and the scale factor form lineA's origin position and the Segment's start psotion;

Notice: The distance between segments and lines may not be the same as the distance between their extended lines. The closest points on the extended infinite line may be outside the range of the segment or ray which is a restricted subset of the line.

Parameters

  • L1: Line
  • S2: Segment
type Line = {
    origin: vec3 | number[],
    dir: vec3 | number[],
}
type Segment = {
    start: vec3 | number[],
    end: vec3 | number[],
}

example:

const result = dist3D_Line_to_Segment(L1, S2);

return

{
    distance: number,
    l1_scale: number,
    s2_scale: number,
}

Examples

import {
    dist3D_Line_to_Segment,
    Line,
    Segment,
} from 'line-segment-distance';
import { vec3 } from 'gl-matrix';

const lineA:Line = {
    origin: [0, 0, 0],
    dir: [1, 0, 1],
};

const segmentB:Segment = {
    start: [4, 8, 0],
    end: [19, 8, 0],
};

const result = dist3D_Line_to_Segment(lineA, segmentB);
const distance = result.distance;
if (distance < 1e-6) {
    // segmentB intersects the lineA.
}
const pointOnLineA = vec3.scaleAndAdd(vec3.create(), lineA.origin, lineA.dir, result.l1_scale);

const segemntB_dir = vec3.normalize(vec3.create(), vec3.sub(vec3.create(), segmentB.end, segmentB.start));

const pointOnSegmentB = vec3.scaleAndAdd(vec3.create(), segmentB.start, segemntB_dir, result.s2_scale);

dist3D_Segment_to_Segment

Return the minimum distance between SegmentA and SegmentB in 3D space, and the scale factor form each segment's start position.

Notice: Like LineToSegment distance, the distance between two segments may not be the same as the distance between their extended lines. The closest points on the extended infinite line may be outside the range of the segment which is a restricted subset of the line.

Parameters

  • S1: Segment
  • S2: Segment
type Segment = {
    start: vec3 | number[],
    end: vec3 | number[],
}

example:

const result = dist3D_Segment_to_Segment(S1, S2);

return

{
    distance: number,
    s1_scale: number,
    s2_scale: number,
}

Examples

import {
    dist3D_Segment_to_Segment,
    Segment,
} from 'line-segment-distance';
import { vec3 } from 'gl-matrix';

const segmentA:Segment = {
    start: [0, 0, 5],
    end: [0, 0, 20],
};

const segmentB:Segment = {
    start: [4, 8, 0],
    end: [19, 8, 0],
};

const result = dist3D_Segment_to_Segment(segmentA, segmentB);
const distance = result.distance;
if (distance < 1e-6) {
    // segmentB intersects the segmentA.
}

const segemntA_dir = vec3.normalize(vec3.create(), vec3.sub(vec3.create(), segmentA.end, segmentA.start));

const pointOnSegmentA = vec3.scaleAndAdd(vec3.create(), segmentA.start, segemntA_dir, result.s2_scale);