如何解决如果给出起点,终点和偏移量,则找到圆弧中心点
我想找到圆弧的中心点。 我有圆弧的起点,终点和偏移量。
我尝试了以下代码。有了这段代码,我在90%的情况下获得了圆弧的中心点。但是对于某些数据,无法找到中心点。
我对弧图有这个定义
X169290Y2681101I90207J39371*
X267716Y2779527J98426*
X169290Y2877952I98425*
X79082Y2818897J98425*
以上四个圆弧中心坐标的答案都是(4.299940599999999,70.5999858)
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;
}
解决方法
您正在处理由值1e-6
引起的准确性问题。我建议您做两件事:
- 检查将此值替换为零时会发生什么(可能引起问题的副作用吗?)
- 如果零值不起作用,请尝试用小于
1e-6
的值代替,例如1e-9
,1e-12
,...
但是最重要的是:您需要在修改该值的同时进行一些详尽的测试(您是否有需要涵盖的测试列表+发明了一些实际的测试用例)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。