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-distance-calculator

v1.0.0

Published

Calculates the distance from a point to a multi-segmented line.

Downloads

368

Readme

line-distance-calculator

A small collection of methods for calculating the distance from a point to a line.

An ES6 module containing a small collection of methods for calculating the distance between a point and a line (which is optionally either multi-segmented or infinite). Written for my project Nibriboard. Check out the demo!

It does the job as to distance-to-line-segment, but I ended up implemented this before I saw that package - and I wanted to have a go at implementing the algorithm successfully after I failed a few weeks ago.

Installation

You'll need support for ES6 Modules in your project / build tool / browser in order to use this package. Install this with npm: npm install --save line-distance-calculator.

Usage

There are 3 primary methods exported by this module:

point_line_distance_multi(point, line_points)

Calculates the minimum distance between a specified multi-segmented line and a specified point. Example:

import { point_line_distance_multi } from "line-distance-calculator";

let distance_data = point_line_distance_multi(
	{ x: 45, y: 30 },
	[
		{ x: 250, y: 337 },
		{ x: 266, y: 326 },
		{ x: 574, y: 177 },
		{ x: 605, y: 254 },
		{ x: 469, y: 120 },
		{ x: 377, y: 315 },
		{ x: 411, y: 328 },
		{ x: 435, y: 335 },
		{ x: 531, y: 344 }
	]
);
console.log(`The minimum distance is ${distance_data[1]}`);
console.log(`The segment that's closest to the point starts at index ${distance_data[0]}`);

point_line_distance(point, line_start, line_end)

Calculates the minimum distance between a point and an infinite line specified by 2 more points. Example:

import { point_line_distance } from "line-distance-calculator";

let point = { x: 50, y: 82 };
let line_start = { x: 10, y: 52 };
let line_end = { x: 50, y: 18 };

let distance = point_line_distance(point, line_start, line_end);

console.log(`The distance to the line from the point is ${distance}`);

point_infinite_line_distance(point, line_a, line_b)

An implementation from this wikipedia article before I realised that the algorithm was for an infinite line and not a finite one. I've left this in because this implementation does work - it's just not what I wanted myself.

It works identically to point_line_distance demonstrated above. Example:

import { point_infinite_line_distance } from "line-distance-calculator";

let point = { x: 50, y: 82 };
let line_start = { x: 10, y: 52 };
let line_end = { x: 50, y: 18 };

let distance = point_infinite_line_distance(point, line_start, line_end);

console.log(`The distance to the infinite line from the point is ${distance}`);

Contribute

Contributions are welcome! If you find a bug, please open an issue - or even better open a pull request :smiley_cat:

License

This package is licensed under the Mozilla Public License 2.0. The full license text is available here - along with a link to a summary summary on what you can and can't do with it.

If you'd like to do something that's prohibited by the license - please do get in touch! My email address is available on my website.

Sources