Skip to content

Commit

Permalink
Update parameter to epsilon
Browse files Browse the repository at this point in the history
This also preserves backwards compatibility
  • Loading branch information
okcoker committed Mar 13, 2021
1 parent a3f616f commit 77e6af7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
21 changes: 11 additions & 10 deletions packages/turf-boolean-point-on-line/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +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.
* @param {number} [options.epsilon] Fractional number to compare with the cross product result. Useful for dealing with floating points such as lng/lat points
* @returns {boolean} true/false
* @example
* var pt = turf.point([0, 0]);
Expand All @@ -23,7 +23,7 @@ function booleanPointOnLine(
line: Feature<LineString> | LineString,
options: {
ignoreEndVertices?: boolean;
precision?: number;
epsilon?: number;
} = {}
): boolean {
// Normalize inputs
Expand All @@ -50,7 +50,7 @@ function booleanPointOnLine(
lineCoords[i + 1],
ptCoords,
ignoreBoundary,
typeof options.precision === "undefined" ? 10 : options.precision
typeof options.epsilon === "undefined" ? null : options.epsilon
)
) {
return true;
Expand All @@ -60,13 +60,14 @@ function booleanPointOnLine(
}

// See http://stackoverflow.com/a/4833823/1979085
// See https://stackoverflow.com/a/328122/1048847
/**
* @private
* @param {Position} lineSegmentStart coord pair of start of line
* @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.
* @param {number} epsilon Fractional number to compare with the cross product result. Useful for dealing with floating points such as lng/lat points
* If true which end to ignore.
* @returns {boolean} true/false
*/
Expand All @@ -75,7 +76,7 @@ function isPointOnLineSegment(
lineSegmentEnd: number[],
pt: number[],
excludeBoundary: string | boolean,
precision: number
epsilon: number | null
): boolean {
const x = pt[0];
const y = pt[1];
Expand All @@ -88,11 +89,11 @@ function isPointOnLineSegment(
const dxl = x2 - x1;
const dyl = y2 - y1;
let cross = dxc * dyl - dyc * dxl;
if (precision) {
const multiplier = Math.pow(10, precision);
cross = Math.round(cross * multiplier) / multiplier;
}
if (cross !== 0) {
if (epsilon !== null) {
if (Math.abs(cross) > epsilon) {
return false;
}
} else if (cross !== 0) {
return false;
}
if (!excludeBoundary) {
Expand Down
2 changes: 1 addition & 1 deletion packages/turf-boolean-point-on-line/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test("turf-boolean-point-on-line", (t) => {
.forEach((filepath) => {
const name = path.parse(filepath).name;
const geojson = load.sync(filepath);
const options = geojson.propeties;
const options = geojson.properties;
const feature1 = geojson.features[0];
const feature2 = geojson.features[1];
const result = pointOnLine(feature1, feature2, options);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "FeatureCollection",
"properties": {
"precision": 0
"epsilon": 10e-18
},
"features": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"type": "FeatureCollection",
"properties": {
"epsilon": 10e-17
},
"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]
]
}
}
]
}

0 comments on commit 77e6af7

Please sign in to comment.