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的左侧、右侧、直线上,或者线段上
下面是一些运行示例:

Enter three points for p0, p1, and p2: 1 1 2 2 1 1.5
(1.0,1.5) is on the left side of the linefrom (1.0,1.0) to (2.0,2.0)
Enter three points for p0, p1, and p2: 1 1 2 2 1 -1
(1.0,-1.0) is on the right side of the linefrom (1.0,1.0) to (2.0,2.0)
Enter three points for p0, p1, and p2: 1 1 2 2 1.5 1.5
(1.5,1.5) is on the line segment from (1.0,1.0) to (2.0,2.0)
Enter three points for p0, p1, and p2: 1 1 2 2 3 3
(3.0,3.0) is on the line segment from (1.0,1.0) to (2.0,2.0)

编程练习题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;}
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部