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:
; let distance_data = ;console;console;
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:
; let point = x: 50 y: 82 ;let line_start = x: 10 y: 52 ;let line_end = x: 50 y: 18 ; let distance = ; console;
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:
; let point = x: 50 y: 82 ;let line_start = x: 10 y: 52 ;let line_end = x: 50 y: 18 ; let distance = ; console;
Contribute
Contributions are welcome! If you find a bug, please open an issue - or even better open a pull request 😺
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
- Lines and Distance of a Point to a Line on Geometry Algorithms Home by Dan Sunday