java 圆的交点_java – 圆线交点

public static ArrayList getCircleLineIntersectionPoint(IntPoint pointA, IntPoint pointB, IntPoint center, int radius) {

// returns a list of intersection points between a line which passes through given points,

// pointA and pointB, and a circle described by given radius and center coordinate

double disc, A, B, C, slope, c;

double x1, x2, y1, y2;

IntPoint point1, point2;

ArrayList intersections = new ArrayList();

try{

slope = Util.calculateSlope(pointA, pointB);

}catch (UndefinedSlopeException e){

C = Math.pow(center.y, 2) + Math.pow(pointB.x, 2) - 2 * pointB.x * center.x + Math.pow(center.x, 2) - Math.pow(radius, 2);

B = -2 * center.y;

A = 1;

disc = Math.pow(B, 2) - 4 * 1 * C;

if (disc < 0){

return intersections;

}

else{

y1 = (-B + Math.sqrt(disc)) / (2 * A);

y2 = (-B - Math.sqrt(disc)) / (2 * A);

x1 = pointB.x;

x2 = pointB.x;

}

point1 = new IntPoint((int)x1, (int)y1);

point2 = new IntPoint((int)x2, (int)y2);

if (Util.euclideanDistance(pointA, point2) > Util.euclideanDistance(pointA, point1)){

intersections.add(point1);

}

else{

intersections.add(point2);

}

return intersections;

}

if (slope == 0){

C = Math.pow(center.x, 2) + Math.pow(center.y, 2) + Math.pow(pointB.y, 2) - 2 * pointB.y * center.y - Math.pow(radius, 2);

B = -2 * center.x;

A = 1;

disc = Math.pow(B, 2) - 4 * 1 * C;

if (disc < 0){

return intersections;

}

else{

x1 = (-B + Math.sqrt(disc)) / (2*A);

x2 = (-B - Math.sqrt(disc)) / (2*A);

y1 = pointB.y;

y2 = pointB.y;

}

}

else{

c = slope * pointA.x + pointA.y;

B = (2 * center.x + 2 * center.y * slope + 2 * c * slope);

A = 1 + Math.pow(slope, 2);

C = (Math.pow(center.x, 2) + Math.pow(c, 2) + 2 * center.y * c + Math.pow(center.y, 2) - Math.pow(radius, 2));

disc = Math.pow(B, 2) - (4 * A * C);

if (disc < 0){

return intersections;

}

else{

x1 = (-B + Math.sqrt(disc)) / (2 * A);

x2 = (-B - Math.sqrt(disc)) / (2 * A);

y1 = slope * x1 - c;

y2 = slope * x2 - c;

}

}

point1 = new IntPoint((int)x1, (int)y1);

point2 = new IntPoint((int)x2, (int)y2);

if (Util.euclideanDistance(pointA, point2) > Util.euclideanDistance(pointA, point1)){

//if (Util.angleBetween(pointA, pointB, point1) < Math.PI/2){

intersections.add(point1);

//}

}

else{

//if (Util.angleBetween(pointA, pointB, point1) < Math.PI/2){

intersections.add(point2);

//}

}

return intersections;

}

我使用上面的算法来测试圆和线之间的交集.它有时很好,但在其他时候失败.该代码表示​​从圆和线方程(x-a)^(y-b)^ 2 = r ^ 2和y = mx-mx1 y1同时求解x得到的方程.有没有人知道我在数学或其他地方出错了?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值