Commit 92eb9b71 authored by Peter Čonga's avatar Peter Čonga
Browse files

Distance span using relative distances

parent 8410844e
Loading
Loading
Loading
Loading
+14 −20
Original line number Diff line number Diff line
@@ -366,30 +366,24 @@ public class BatchCuttingPlanesAction extends ControlPanelAction<BatchCuttingPla
                            .filter(Objects::nonNull)
                            .collect(Collectors.toList());
                        
                        Point2d furthestPoint = nearestNeighbors.stream()
                            .max(Comparator.comparingDouble(point -> Math.sqrt(Math.pow((point.x - avgPoint.x), 2) + Math.pow((point.y - avgPoint.y), 2))))
                            .orElse(null);
                        
                        Point2d otherFurthestPoint = nearestNeighbors.stream()
                            .filter(point -> point != furthestPoint) // Skip the furthest point
                            .max(Comparator.comparingDouble(point -> Math.sqrt(Math.pow((point.x - furthestPoint.x), 2) + Math.pow((point.y - furthestPoint.y), 2))))
                            .orElse(null);
                        
                        
                        Line2D normal = avgCurve.getNormals().get(curCounter);
                        double furthestNearestNeighborDist = Math.sqrt(Math.pow((furthestPoint.x - avgPoint.x), 2) + Math.pow((furthestPoint.y - avgPoint.y), 2));
                        double otherFurthestNearestNeighborDist = Math.sqrt(Math.pow((furthestPoint.x - otherFurthestPoint.x), 2) + Math.pow((furthestPoint.y - otherFurthestPoint.y), 2)) - furthestNearestNeighborDist;
                        
                        double dotProductFurthest = normal.getX2() * (furthestPoint.x - avgPoint.x) + normal.getY2() * (furthestPoint.y - avgPoint.y);
                        double maxDistance = Double.MIN_VALUE;
                        double minDistance = Double.MAX_VALUE;
                        for (Point2d p : nearestNeighbors) {
                            double dx = p.x - avgPoint.x;
                            double dy = p.y - avgPoint.y;
                            double relativeDistance = dx * normal.getX2() + dy * normal.getY2();
                            
                        double distanceInNormalDirection = dotProductFurthest > 0 ? furthestNearestNeighborDist : otherFurthestNearestNeighborDist;
                        double distanceInOppositeDirection = dotProductFurthest > 0 ? otherFurthestNearestNeighborDist : furthestNearestNeighborDist;
                            maxDistance = relativeDistance > maxDistance ? relativeDistance : maxDistance;
                            minDistance = relativeDistance < minDistance ? relativeDistance : minDistance;
                        }
                        
                        normal.setLine(
                            normal.getX1() - (normal.getX2() * distanceInOppositeDirection),
                            normal.getY1() - (normal.getY2() * distanceInOppositeDirection),
                            normal.getX1() + (normal.getX2() * distanceInNormalDirection),
                            normal.getY1() + (normal.getY2() * distanceInNormalDirection)
                            normal.getX1() + (normal.getX2() * minDistance),
                            normal.getY1() + (normal.getY2() * minDistance),
                            normal.getX1() + (normal.getX2() * maxDistance),
                            normal.getY1() + (normal.getY2() * maxDistance)
                        );

                        avgCurve.setNormalAt(curCounter, normal);