Java黑皮书课后题第3章:*3.32(几何:点的位置)给定一个从点p0(x0,y0)到p1(x1,y1)的有向线段,可以用以下公式判定定点p2(x2, y2)是在线段的左侧、右侧,或者在该线段上
*3.32(幾何:點(diǎn)的位置)給定一個(gè)從點(diǎn)p0(x0,y0)到p1(x1,y1)的有向線段,可以用以下公式判定定點(diǎn)p2(x2, y2)是在線段的左側(cè)、右側(cè),或者在該線段上
- 題目
- 題目概述
- 運(yùn)行示例
- 代碼
題目
題目概述
*3.32(幾何:點(diǎn)的位置)給定一個(gè)從點(diǎn)p0(x0,y0)到p1(x1,y1)的有向線段,可以用以下公式判定定點(diǎn)p2(x2, y2)是在線段的左側(cè)、右側(cè),或者在該線段上
公式:(x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0)
以上結(jié)果>0則p2在線段左側(cè),=0則在線段上,<0則在線段右側(cè)
編寫程序,提示用戶輸入三個(gè)點(diǎn)p0 p1 p2,顯示p2在線段p0p1的左側(cè)右側(cè)還是線段上
運(yùn)行示例
Enter three points for p0, p1, and p2: 4.4 2 6.5 9.5 -5 4
p2 is on the left side of the line
Enter three points for p0, p1, and p2: 1 1 5 5 2 2
p2 is on the same line
Enter three points for p0, p1, and p2: 3.4 2 6.5 9.5 5 2.5
p2 is on the right side of the line
代碼
import java.util.Scanner;public class Test3_32 {public static void main(String[] args) {// 獲取三個(gè)點(diǎn)的x、y坐標(biāo)值Scanner input = new Scanner(System.in);System.out.println("Enter three points for p0, p1, and p2: ");double x0 = input.nextDouble(), y0 = input.nextDouble();double x1 = input.nextDouble(), y1 = input.nextDouble();double x2 = input.nextDouble(), y2 = input.nextDouble();// 公式判斷double result = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0);// 輸出結(jié)果if(result > 0)System.out.println("p2 is on the left side of the line");else if(result == 0)System.out.println("p2 is on the same line");elseSystem.out.println("p2 is on the right side of the line");} }總結(jié)
以上是生活随笔為你收集整理的Java黑皮书课后题第3章:*3.32(几何:点的位置)给定一个从点p0(x0,y0)到p1(x1,y1)的有向线段,可以用以下公式判定定点p2(x2, y2)是在线段的左侧、右侧,或者在该线段上的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java黑皮书课后题第3章:*3.30(
- 下一篇: Java黑皮书课后题第3章:*3.33(