Sunday 3 July 2016

Chapter 3 Exercise 34, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*3.34 (Geometry: point on line segment) Programming Exercise 3.32 shows how to test whether a point is on an unbounded line. Revise Programming Exercise 3.32 to test whether a point is on a line segment. Write a program that prompts the user to enter the three points for p0, p1, and p2 and displays whether p2 is on the line segment from p0 to p1. Here are some sample runs:

Enter three points for p0, p1, and p2: 1 1 2.5 2.5 1.5 1.5
(1.5, 1.5) is on the line segment from (1.0, 1.0) to (2.5, 2.5) 

Enter three points for p0, p1, and p2: 1 1 2 2 3.5 3.5
(3.5, 3.5) is not on the line segment from (1.0, 1.0) to (2.0, 2.0) 




import java.util.Scanner;
 
public class ProgrammingEx3_34 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  System.out.println("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();
 
  String p0 = "(" + x0 + ", " + y0 + ")";
  String p1 = "(" + x1 + ", " + y1 + ")";
  String p2 = "(" + x2 + ", " + y2 + ")";
 
  String s = " not ";
 
  // checking if the point is in range
  if (((x2 < x1 && x2 > x0) || (x2 < x0 && x2 > x1))
    && ((y2 < y1 && y2 > y0) || (y2 < y0 && y2 > y1))) {
   double c = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0);
   if (c == 0) {
    s = " ";
   }
  }
 
  System.out.println(p2 + "is" + s + "on the line segment from " + p0
    + " to " + p1);
 }
}

No comments :

Post a Comment