Skip to content

Commit

Permalink
fix: perpendicular formula and handle straight line (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
muktihari authored Sep 6, 2024
1 parent 964c2c3 commit e119a62
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
18 changes: 9 additions & 9 deletions rdp/rdp.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Package rdp host the implementation of [Ramer–Douglas–Peucker algorithm](https://w.wiki/B6U3) algorithm.
package rdp

import "math"
import (
"math"
)

// Point represents coordinates in 2D plane.
type Point struct {
Expand All @@ -15,6 +17,10 @@ type Point struct {
// Note: The resulting slice is a reslice of given points (it shares the same underlying array) for efficiency.
// It works similar to append, so the input points should not be used after this call, use only the returned value.
func Simplify(points []Point, epsilon float64) []Point {
if epsilon <= 0 {
return points
}

if len(points) <= 2 {
return points
}
Expand All @@ -34,16 +40,10 @@ func Simplify(points []Point, epsilon float64) []Point {
}
}

if maxDist <= epsilon {
if maxDist <= epsilon || index == 0 || index == len(points)-1 {
return append(points[:0], first, last)
}

// Move index to avoids infinite recursive as slice input
// for next operation is never changed if we keep it as is.
if index == 0 || index == len(points) {
index++
}

left, right := points[:index], points[index:]

return append(Simplify(left, epsilon), Simplify(right, epsilon)...)
Expand All @@ -59,7 +59,7 @@ func perpendicularDistance(p, start, end Point) float64 {
// Standard Form: Ax + Bx + C = 0
A := end.Y - start.Y
B := start.X - end.X
C := (end.X * start.Y) - (start.X * end.Y)
C := start.Y*(end.X-start.X) - (end.Y-start.Y)*start.X

// d = | Ax + By + C = 0 | / ✓(A²+B²)
return math.Abs(A*p.X+B*p.Y+C) / math.Sqrt(A*A+B*B)
Expand Down
28 changes: 28 additions & 0 deletions rdp/rdp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ func TestSimplify(t *testing.T) {
points []Point
expect []Point
}{
{
name: "no point is removed",
epsilon: 0,
points: []Point{
{X: 0.0, Y: 0.0},
{X: 0.1, Y: 0.2},
},
expect: []Point{
{X: 0.0, Y: 0.0},
{X: 0.1, Y: 0.2},
},
},
{
name: "one points",
epsilon: 0.1,
Expand Down Expand Up @@ -111,6 +123,22 @@ func TestPerpendicularDistance(t *testing.T) {
d: 6.708, // euclidean((5,6), (2,0))
prec: 1000,
},
{
name: "same as start, distance should be zero",
p: Point{X: 45.897662518545985, Y: 6.801717197522521},
start: Point{X: 45.897662518545985, Y: 6.801717197522521},
end: Point{X: 45.89766209945083, Y: 6.8017197120934725},
d: 0,
prec: 1000,
},
{
name: "same as end, distance should be zero",
p: Point{X: 45.89766209945083, Y: 6.8017197120934725},
start: Point{X: 45.897662518545985, Y: 6.801717197522521},
end: Point{X: 45.89766209945083, Y: 6.8017197120934725},
d: 0,
prec: 1000,
},
}

for i, tc := range tt {
Expand Down

0 comments on commit e119a62

Please sign in to comment.