英文:
Java geometry problem: perimeter of circular segment
问题
Seq是一个方法,对于给定的线:线的点a和斜率b,返回圆弧段的周长。我使用圆和直线的方程,并解出x,然后轻松获得两个点。但我得到的不是(3.5,1.9)左右,而是(6.528,0.0832000000000006)。
public class Circle {
Point center;
double r;
public double seq(Point a, double b) {
double n = -b * a.getX() + a.getY();
System.out.println("n: " + n);
if (r * r * (b * b + 1) - Math.pow(b * center.getX() - center.getY() + n, 2) > 0) {
double temp = (1 + b * b);
System.out.println("coeficient x^2: " + temp);
double temp1 = 2 * ((-1) * center.getX() + b * n - b * center.getY());
System.out.println("coeficient x: " + temp);
double free_term = center.getX() * center.getX() + n * n - 2 * n * center.getY() + center.getY() * center.getY() - r * r;
System.out.println("free term: " + free_term);
double D = Math.sqrt(temp1 * temp1 - 4 * temp * free_term);
System.out.println(temp1 * temp1 - 4 * temp * free_term);
double x1 = ((-1) * temp1 + Math.sqrt(temp1 * temp1 - 4 * temp * free_term)) / (2 * temp);
double x2 = ((-1) * temp1 - Math.sqrt(temp1 * temp1 - 4 * temp * free_term)) / (2 * temp);
double y1 = b * x1 - b * a.getX() + a.getY();
double y2 = b * x2 - b * a.getX() + a.getY();
System.out.println("(" + x1 + "," + y1 + "), (" + x2 + "," + y2 + ")");
Point A = new Point(x1, y1);
Point B = new Point(x2, y2);
double d = A.dist(B);
System.out.println("d: " + d);
double angle = Math.acos((2 * r * r - d * d) / (2 * r * r));
System.out.println("angle " + angle);
double l = r * Math.PI * angle / Math.toRadians(180);
return l + d;
} else
return 0;
}
}
Main:
Circle k = new Circle();
Point c = new Point(0, 0);
k.center = c;
k.r = 4;
Point a = new Point(0, 4);
System.out.println(k.seq(a, -3.0 / 5.0));
Console:
n: 4.0
coeficient x^2: 1.3599999999999999
coeficient x: -4.8
free term: 0.0
23.04
(6.528,0.0832000000000006), (0.0,4.0)
d: 7.612890793910023
angleNaN
NaN
英文:
Seq is a method which for given line: line's point a and slope b returns perimeter of circular segment. I used the equation of circle and line and solved it for x, and then I easily get two points. But instead of (3.5,1.9) approximately I get (6.528,0.0832000000000006)..
public class Circle {
Point center;
double r;
public double seq(Point a, double b) {
double n = -b*a.getX() + a.getY();
System.out.println("n: "+n);
if (r*r*(b*b+1)-Math.pow(b*center.getX()-center.getY()+n, 2) > 0) {
double temp = (1+b*b);
System.out.println("coeficient x^2: "+temp);
double temp1 = 2*((-1)*center.getX()+b*n-b*center.getY());
System.out.println("coeficient x: "+ temp);
double free_term = center.getX()*center.getX() + n*n -2*n*center.getY() + center.getY()*center.getY() - r*r;
System.out.println("free term: "+free_term);
double D = Math.sqrt(temp1*temp1-4*temp*free_term);
System.out.println(temp1*temp1-4*temp*free_term);
double x1 = ((-1)*temp1+Math.sqrt(temp1*temp1-4*temp*free_term))/2*temp;
double x2 = ((-1)*temp1-Math.sqrt(temp1*temp1-4*temp*free_term))/2*temp;
double y1 = b*x1 - b*a.getX() + a.getY();
double y2 = b*x2 - b*a.getX() + a.getY();
System.out.println("("+x1+","+y1+")"+", ("+x2+","+y2+")");
Point A = new Point(x1,y1);
Point B = new Point(x2,y2);
double d = A.dist(B);
System.out.println("d: "+d);
double angle = Math.acos((2*r*r-d*d)/2*r*r);
System.out.println("angle "+ alfa);
double l = r*Math.PI*angle/Math.toRadians(180);
return l+d;
} else return 0;
}
}
Main:
Circle k = new Circle();
Point c = new Point(0,0);
k.center = c;
k.r = 4;
Point a = new Point(0,4);
System.out.println(k.seq(a, -3.0/5.0));
Console:
n: 4.0
coeficient x^2: 1.3599999999999999
coeficient x: -4.8
free term: 0.0
23.04
(6.528,0.0832000000000006), (0.0,4.0)
d: 7.612890793910023
angleNaN
NaN
答案1
得分: 1
你的问题在于二次方程的实现。你可能会认为 A/2*B
意味着 A/(2*B)
,但实际上它是 (A/2)*B
。所以在除数周围加上括号:
double x1 = ((-1) * temp1 + Math.sqrt(temp1 * temp1 - 4 * temp * free_term)) / (2 * temp);
double x2 = ((-1) * temp1 - Math.sqrt(temp1 * temp1 - 4 * temp * free_term)) / (2 * temp);
英文:
Your problem is in the implementation of the quadratic formula. You might think that A/2*B
means A/(2*B)
but in reality it's (A/2)*B
. So add the parentheses in around the divisor:
double x1 = ((-1)*temp1+Math.sqrt(temp1*temp1-4*temp*free_term))/(2*temp);
double x2 = ((-1)*temp1-Math.sqrt(temp1*temp1-4*temp*free_term))/(2*temp);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论