因此,我需要編寫一個程序來決定一條線是否攔截一個圓。不需要特定的攔截座標,只需觸摸圓或與其相切即可攔截。如果之前已經問過這個問題,我很抱歉,但是我找不到適合我的問題的東西。也許我看起來不夠堅硬。(java)決定一條線是否攔截一個圓
import java.util.Scanner;
public class LineCircle_Intersection {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double p1x, p2x, p1y, p2y, cx, cy, r;
System.out.print("Enter p1x: ");
p1x = in.nextDouble();
System.out.print("Enter p1y: ");
p1y = in.nextDouble();
System.out.print("Enter p2x: ");
p2x = in.nextDouble();
System.out.print("Enter p2y: ");
p2y = in.nextDouble();
System.out.print("Enter cx: ");
cx = in.nextDouble();
System.out.print("Enter cy: ");
cy = in.nextDouble();
System.out.print("Enter r: ");
r = in.nextDouble();
if ((p1x - cx < r) && (p1y - cy < r))
System.out.println("The line intersects the circle.");
else if ((p2x - cx < r) && (p2y - cy < r))
System.out.println("The line intersects the circle.");
else
System.out.println("The line does not intersect the circle.");
}
}
我一直在使用直線和圓的公式而不是輸入點的替代路線,但它是一種亂七八糟的,我不知道在哪裏,我用它去。
這應該有助於https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line – BevynQ