Java黑皮书课后题第6章:*6.39(几何:点的位置)编程练习题3.32显示如何测试一个点是否在一个有向直线的左侧、右侧或在直线上,编写一个程序,输入三个点p0p1p2,显示p2是否在直线p0p1
6.39(幾何:點的位置)編程練習題3.32顯示如何測試一個點是否在一個有向直線的左側、右側或在直線上,編寫一個程序,輸入三個點p0p1p2,顯示p2是否在直線p0p1
- 題目
- 題目描述
- 編程練習題3.32(非本題)
- 破題
- 代碼
題目
題目描述
6.39(幾何:點的位置)編程練習題3.32顯示如何測試一個點是否在一個有向直線的左側、右側或在直線上,使用下面的方法頭編寫該方法:
判斷x2y2是否在直線x0y0 x1y1左側
public static boolean leftOfTheLine(double x0, double y0, double x1, double y1, double x2, double y2)
判斷x2y2是否在直線x0y0 x1y1上
public static boolean onTheSameLine(double x0, double y0, double x1, double y1, double x2, double y2)
判斷x2y2是否在線段x0y0 x1y1上
public static boolean onTheLineSegment(double x0, double y0, double x1, double y1, double x2, double y2)
編寫一個程序,輸入三個點p0p1p2,顯示p2是否在直線p0p1的左側、右側、直線上,或者線段上
下面是一些運行示例:
編程練習題3.32(非本題)
import java.util.Scanner;public class Test3_32 {public static void main(String[] args) {// 獲取三個點的x、y坐標值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);// 輸出結果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");} }破題
main方法:接收三個點的橫縱坐標值、分別傳入四個方法并接受返回、輸出結果
leftOfTheLine方法:判斷P2(x2,y2)是否在線P0P1的左側
rightOfTheLine方法:判斷P2(x2,y2)是否在線P0P1的右側
onTheSameLine方法:判斷P2(x2,y2)是否在線P0P1上
onTheLineSegment方法:判斷P2(x2,y2)是否在線段P0P1上
代碼
import java.util.Scanner;public class Test6_39 {public static void main(String[] args) {// 獲取三個點六個值Scanner input = new Scanner(System.in);System.out.print("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();// 傳入四個方法boolean bool1 = leftOfTheLine(x0,y0,x1,y1,x2,y2);boolean bool2 = rightOfTheLine(x0,y0,x1,y1,x2,y2);boolean bool3 = onTheSameLine(x0,y0,x1,y1,x2,y2);boolean bool4 = onTheLineSegment(x0,y0,x1,y1,x2,y2);// 根據不同情況輸出if (bool1){System.out.println("("+x2+","+y2+") is on the left side of the line");System.out.print("\tfrom ("+x0+","+y0+") to ("+x1+","+y1+")");}if (bool2){System.out.println("("+x2+","+y2+") is on the right side of the line");System.out.print("\tfrom ("+x0+","+y0+") to ("+x1+","+y1+")");}if (bool4){System.out.print("("+x2+","+y2+") is on the line segment from (" +x0+","+y0+") to ("+x1 + "," + y1 + ")");return;}if (bool3)System.out.print("("+x2+","+y2+") is on the same line from (" +x0+","+y0+") to ("+x1 + "," + y1 + ")");}// 判斷左側public static boolean leftOfTheLine(double x0, double y0, double x1, double y1, double x2, double y2){double result = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0);if(result > 0){return true;}else {return false;}}// 判斷右側public static boolean rightOfTheLine(double x0, double y0, double x1, double y1, double x2, double y2){double result = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0);if(result < 0){return true;}else {return false;}}// 判斷是否在直線上public static boolean onTheSameLine(double x0, double y0, double x1, double y1, double x2, double y2){double result = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0);if(result == 0){return true;}else {return false;}}// 判斷是否在線段上public static boolean onTheLineSegment(double x0, double y0, double x1, double y1, double x2, double y2){if (onTheSameLine(x0,y0,x1,y1,x2,y2)){if (x0 >= Math.min(x0, x1) && x0 <= Math.max(x0,x1) && y0 >= Math.min(y0,y1) && y0 <= Math.max(y0,y1))return true;return false;}return false;} }總結
以上是生活随笔為你收集整理的Java黑皮书课后题第6章:*6.39(几何:点的位置)编程练习题3.32显示如何测试一个点是否在一个有向直线的左侧、右侧或在直线上,编写一个程序,输入三个点p0p1p2,显示p2是否在直线p0p1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java黑皮书课后题第6章:*6.38(
- 下一篇: Java黑皮书课后题第7章:*7.1(指