Skip to content

Commit

Permalink
Use an iterative version of Dougle Reumer for freeform selection.
Browse files Browse the repository at this point in the history
  • Loading branch information
tinevez committed Oct 28, 2024
1 parent 712eec2 commit c7ecea2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/main/java/org/mastodon/ui/util/RamerDouglasPeucker.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ public static void simplifyPath( final List< Point > points, final double epsilo
simplifySection( points, 0, points.size() - 1, epsilon );
}

/**
* Determines if a point should be added to the specified polygon, based on
* the Ramer-Douglas-Peucker criterion (perpendicular distance from the last
* segment).
*/
public static boolean shouldAddPoint( final List< Point > points, final Point newPoint, final double epsilon )
{
if ( points.size() < 2 )
return true;

// Get the last two points
final Point lastPoint = points.get( points.size() - 1 );
final Point secondLastPoint = points.get( points.size() - 2 );

final double distance = perpendicularDistance( newPoint, secondLastPoint, lastPoint );

return distance > epsilon;
}

private static void simplifySection( final List< Point > points, final int start, final int end, final double epsilon )
{
if ( end <= start + 1 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ public void drag( final int x, final int y )
{
if ( isDrawing )
{
polygon.add( new Point( x, y ) );
RamerDouglasPeucker.simplifyPath( polygon, 0.1 );
final Point p = new Point( x, y );
if ( RamerDouglasPeucker.shouldAddPoint( polygon, p, 0.1 ) )
polygon.add( p );
panel.repaint();
}
}
Expand Down

0 comments on commit c7ecea2

Please sign in to comment.