如果给出起点,终点和偏移量,则找到圆弧中心点

问题描述

我想找到圆弧的中心点。 我有圆弧的起点,终点和偏移量。

我尝试了以下代码。有了这段代码,我在90%的情况下获得了圆弧的中心点。但是对于某些数据,无法找到中心点。

我对弧图有这个定义

X169290Y2681101I90207J39371*
X267716Y2779527J98426*
X169290Y2877952I98425*
X79082Y2818897J98425*

以上四个圆弧中心坐标的答案都是(4.299940599999999,70.5999858)

这是一个弧形图像,该弧由四个单象限弧组成。

enter image description here

private Coordinate findCenter(Coordinate start,Coordinate end,Coordinate offset) {
        double twoPi = 2 * Math.PI;
        
        if (quadrantMode.equals("single-quadrant")) {
            // The Gerber spec says single quadrant only has one possible center,// and you can detect it based on the angle. But for real files,this
      // seems to work better - there is usually only one option that makes
      // sense for the center (since the distance should be the same
      // from start and end). We select the center with the least error in
      // radius from all the options with a valid sweep angle.
            
            double sqdistDiffMin = Double.MAX_VALUE;
            
            Coordinate center = null;
            
            List<Coordinate> qFactors = new ArrayList<Coordinate>();
            qFactors.add(new Coordinate(1,1));
            qFactors.add(new Coordinate(1,-1));
            qFactors.add(new Coordinate(-1,1));
            qFactors.add(new Coordinate(-1,-1));
            
            for (Coordinate factors : qFactors) {
                Coordinate testCenter = new Coordinate(start.getX() + offset.getX() * factors.getX(),start.getY() + offset.getY() * factors.getY());
                // Find angle from center to start and end points
                double startAngleX = start.getX() - testCenter.getX();
                double startAngleY = start.getY() - testCenter.getY();
                double startAngle = Math.atan2(startAngleY,startAngleX);
                
                double endAngleX = end.getX() - testCenter.getX();
                double endAngleY = end.getY() - testCenter.getY();
                double endAngle = Math.atan2(endAngleY,endAngleX);
                
                // # Clamp angles to 0,2pi
                double theta0 = (startAngle + twoPi) % twoPi;
                double theta1 = (endAngle + twoPi) % twoPi;
                
                // # Determine sweep angle in the current arc direction
                double sweepAngle;
                if (direction.equals("counterclockwise") ) {
                    theta1 += twoPi;
//                  sweepAngle = Math.abs(theta1 - theta0);
                    sweepAngle = Math.abs(theta1 - theta0) % twoPi;
                } else {
                    theta0 += twoPi;
                    sweepAngle = Math.abs(theta0 - theta1) % twoPi;
                }
                
                // # Calculate the radius error
                double sqdistStart = sqdistance(start,testCenter);
                double sqdistEnd = sqdistance(end,testCenter);
                double sqdistDiff = Math.abs(sqdistStart - sqdistEnd);
                
                // Take the option with the lowest radius error from the set of
                // options with a valid sweep angle
                // In some rare cases,the sweep angle is numerically (10**-14) above pi/2
                // So it is safer to compare the angles with some tolerance
                boolean isLowesTradiusError = sqdistDiff < sqdistDiffMin;
                boolean isValidSweepAngle = sweepAngle >= 0 && sweepAngle <= Math.PI / 2.0 + 1e-6;
                if (isLowesTradiusError && isValidSweepAngle) {
                    center = testCenter;
                    sqdistDiffMin = sqdistDiff;
                }
            }
            return center;
        }
        else {
            return new Coordinate(start.getX() + offset.getX(),start.getY() + offset.getY());
        }
    }

public static double sqdistance(Coordinate point1,Coordinate point2) {
    double diff1 = point1.getX() - point2.getX();
    double diff2 = point1.getY() - point2.getY();

    return diff1 * diff1 + diff2 * diff2;
}

弧形绘制的解释图

enter image description here

解决方法

您正在处理由值1e-6引起的准确性问题。我建议您做两件事:

  • 检查将此值替换为零时会发生什么(可能引起问题的副作用吗?)
  • 如果零值不起作用,请尝试用小于1e-6的值代替,例如1e-91e-12,...

但是最重要的是:您需要在修改该值的同时进行一些详尽的测试(您是否有需要涵盖的测试列表+发明了一些实际的测试用例)。