6.39 (Geometry: point position) Programming Exercise 3.32 shows how to test whet
ID: 3718776 • Letter: 6
Question
6.39 (Geometry: point position) Programming Exercise 3.32 shows how to test whether a point is on the left side of a directed line, on the right, or on the same line. Write the methods with the following headers: Return true if point (x2, y2) is on the left side of the * directed line from (x0, y0) to (x1, yl) */ public static boolean leftOfTheLine (double x0, double y0, double xl, double yl, double x2, double y2) Return true if int (x2, y2) is on the same line from (x0, y0) to (x1, yl) */ public static boolean onTheSameLine (double x0, double yo, double xl, double yl, double x2, double y2) Return true if point (x2, y2) is on the line segment from (x0, y0) to (x1, y1) */ public static boolean onTheLineSegment (double x0, double yo, double xl, double yl, double x2, double y2) Write a program that prompts the user to enter the three points for p0, pl, and p2 and displays whether p2 is on the left of the line from p0 to pl, right, the same line, or on the line segment. Here are some sample runs: Enter three points for p0, pl, and p2: 1 1221.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 pO, pl, and p2: 11 22 3 3 Enter (3.0, 3.0) is on the same line from (1.0, 1.0) to (2.0, 2.0) Enter three points for po, pl, and p2: 1 1 2 2 1 1.5 Eter (1.0, 1.5) is on the left side of the line from (1.0, 1.0) to (2.0, 2.0) Enter three points for p0, pl, and p2: 11 221 -1 (1.0, -1.0) is on the right side of the line from (1.0, 1.0) to (2.0, 2.0)Explanation / Answer
The coding is done based on your requirement and it is given below clearly wt=ith comments in the code
Java code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package testpackage;
/**
*
* @author miracle
*/
import java.util.Scanner;
/**
*
* (Geometry: point position)
* Programming Exercise 3.32 shows how to test whether a point is
* on the left side of a directed line, on the right, or on the same line.
* Write the methods with the following headers:
*
* Write a program that prompts the user to enter the three points
* for p0, p1, and p2 and displays whether p2 is on the left of the line
* from p0 to p1, right, the same line, or on the line segment.
*
*/
public class Exerc39 {
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();
double y0 = input.nextDouble();
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();
input.close();
if (rightOfTheLine(x0,y0,x1,y1,x2,y2)) {
System.out.println("(" + x2 + ", " + y2 + ") is on the right side of the line from "+
"(" + x0 + ", " + y0 + ")" + " to (" + x1 + ", " + y1 + ")");
} else if (leftOfTheLine(x0, y0, x1, y1, x2, y2)) {
System.out.println("(" + x2 + ", " + y2 + ") is on the left side of the line from "+
"(" + x0 + ", " + y0 + ")" + " to (" + x1 + ", " + y1 + ")");
}else if (onTheLineSegment(x0,y0,x1,y1,x2,y2)) {
System.out.println("(" + x2 + ", " + y2 + ") is on the line segment from "+
"(" + x0 + ", " + y0 + ")" + " to (" + x1 + ", " + y1 + ")");
} else if (onTheSameLine(x0,y0,x1,y1,x2,y2)) {
System.out.println("(" + x2 + ", " + y2 + ") is on the same line from "+
"(" + x0 + ", " + y0 + ")" + " to (" + x1 + ", " + y1 + ")");
}
}
/** Return true if point (x2, y2) is on the left side of the
* directed line from (x0, y0) to (x1, y1) */
public static boolean leftOfTheLine(double x0, double y0, double x1, double y1, double x2, double y2){
return (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0) > 0;
}
/** Return true if point (x2, y2) is on the same
* line from (x0, y0) to (x1, y1) */
public static boolean onTheSameLine(double x0, double y0, double x1, double y1, double x2, double y2) {
return (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0) == 0;
}
/** Return true if point (x2, y2) is on the
* line segment from (x0, y0) to (x1, y1) */
public static boolean onTheLineSegment(double x0, double y0, double x1, double y1, double x2, double y2) {
double position = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0);
return position <= 0.0000000001 && ((x0 <= x2 && x2 <= x1) || (x0 >= x2 && x2 >= x1));
}
/** Return true if point (x2, y2) is on the right side of the
* directed line from (x0, y0) to (x1, y1) */
public static boolean rightOfTheLine(double x0, double y0, double x1, double y1, double x2, double y2){
return (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0) < 0;
}
}
Hope This Helps, if you have any doubts Please comment i will get back to you, thank you and please thumbs up
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.