Understanding Epsilon
Relevant source files
The following files were used as context for generating this wiki page:
In the geo-path-reducer library, Epsilon is a critical threshold parameter used by the Douglas-Peucker algorithm to simplify curves. It defines the maximum allowable deviation (distance) between the original path and the simplified version. By adjusting this value, developers can control the trade-off between the precision of the path and the number of points retained.
Sources: README.md:1-10, src/index.js:1-5
The Role of Epsilon in Douglas-Peucker
The geoPathReducer function implements the Douglas-Peucker algorithm, where Epsilon serves as the decision boundary for recursive subdivision. The algorithm identifies the point with the maximum perpendicular distance (dmax) from the line segment connecting the path's start and end points. If this dmax exceeds the Epsilon value, the path is split at that point, and the process repeats.
Sources: src/index.js:4-15
Decision Logic Flow
The following diagram illustrates how Epsilon influences the path simplification process:
The logic checks if any point deviates too far from the simplified line segment based on the user-defined Epsilon.
Sources: src/index.js:14-25
Parameter Specifications
Epsilon is passed as the second argument to the main reducer function. While the README documentation describes it in terms of "degrees," the underlying implementation calculates distances in kilometers using the Haversine formula, though the logic remains consistent: it represents the distance threshold for point removal.
| Parameter | Type | Description |
|---|---|---|
points | Array | A collection of objects containing lat and lng properties. |
epsilon | Number | The maximum distance threshold. |
Sources: README.md:28-33, src/index.js:3
Technical Implementation and Distance Calculation
The impact of Epsilon is determined by two primary helper modules that calculate spatial distances:
Perpendicular Distance Calculation
The perpendicular-distance.js module calculates the distance from a point to the line segment (start-end) using the semi-perimeter and area of the triangle formed by the three points.
Sources: src/perpendicular-distance.js:3-12
Great-Circle Distance
The distance.js module provides the actual numeric values for these calculations using the Earth's radius (6371 km) and the Haversine formula. This ensures that the Epsilon comparison is grounded in physical distance.
Sources: src/distance.js:3-16
// Example usage of Epsilon in the reducer
const epsilon = 0.0001;
const simplifiedPoints = geoPathReducer(points, epsilon);
Sources: README.md:19-21
Conclusion
Epsilon is the primary configuration variable in geo-path-reducer that governs the aggressiveness of the simplification. A smaller Epsilon results in a path that closely follows the original data with more points, while a larger Epsilon produces a highly simplified path with fewer points, suitable for low-resolution map visualizations or reducing data payload sizes.
Sources: README.md:5-10, src/index.js:14-25