Skip to content
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

Add epsilon option to booleanPointOnLine #2051

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions packages/turf-boolean-point-on-line/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getCoord, getCoords } from "@turf/invariant";
* @param {Feature<LineString>} line GeoJSON LineString
* @param {Object} [options={}] Optional parameters
* @param {boolean} [options.ignoreEndVertices=false] whether to ignore the start and end vertices.
* @param {number} [options.precision=10] how many places after the decimal point to consider for the cross product result.
* @returns {boolean} true/false
* @example
* var pt = turf.point([0, 0]);
Expand All @@ -22,6 +23,7 @@ function booleanPointOnLine(
line: Feature<LineString> | LineString,
options: {
ignoreEndVertices?: boolean;
precision?: number;
okcoker marked this conversation as resolved.
Show resolved Hide resolved
} = {}
): boolean {
// Normalize inputs
Expand All @@ -47,7 +49,8 @@ function booleanPointOnLine(
lineCoords[i],
lineCoords[i + 1],
ptCoords,
ignoreBoundary
ignoreBoundary,
typeof options.precision === "undefined" ? 10 : options.precision
)
) {
return true;
Expand All @@ -63,14 +66,16 @@ function booleanPointOnLine(
* @param {Position} lineSegmentEnd coord pair of end of line
* @param {Position} pt coord pair of point to check
* @param {boolean|string} excludeBoundary whether the point is allowed to fall on the line ends.
* @param {number} precision how many places after the decimal point to consider for the cross product result.
* If true which end to ignore.
* @returns {boolean} true/false
*/
function isPointOnLineSegment(
lineSegmentStart: number[],
lineSegmentEnd: number[],
pt: number[],
excludeBoundary: string | boolean
excludeBoundary: string | boolean,
precision: number
): boolean {
const x = pt[0];
const y = pt[1];
Expand All @@ -82,7 +87,11 @@ function isPointOnLineSegment(
const dyc = pt[1] - y1;
const dxl = x2 - x1;
const dyl = y2 - y1;
const cross = dxc * dyl - dyc * dxl;
let cross = dxc * dyl - dyc * dxl;
if (precision) {
okcoker marked this conversation as resolved.
Show resolved Hide resolved
const multiplier = Math.pow(10, precision);
cross = Math.round(cross * multiplier) / multiplier;
}
if (cross !== 0) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"type": "FeatureCollection",
"properties": {
"precision": 0
},
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [-75.25737143565107, 39.99673377198139]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[-75.2580499870244, 40.00180204907801],
[-75.25676601413157, 39.992211720827044]
]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [-75.25737143565107, 39.99673377198139]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[-75.2580499870244, 40.00180204907801],
[-75.25676601413157, 39.992211720827044]
]
}
}
]
}