*3.32 (Geometry: point position) Given a directed line from point p0(x0, y0) to p1(x1,
y1), you can use the following condition to decide whether a point p2(x2, y2) is
on the left of the line, on the right, or on the same line (see Figure 3.11):
Enter three points for p0, p1, and p2: 4.4 2 6.5 9.5 -5 4
(-5.0, 4.0) is on the left side of the line from (4.4, 2.0) to (6.5, 9.5)
Enter three points for p0, p1, and p2: 1 1 5 5 2 2
(2.0, 2.0) is on the line from (1.0, 1.0) to (5.0, 5.0)
Enter three points for p0, p1, and p2: 3.4 2 6.5 9.5 5 2.5
(5.0, 2.5) is on the right side of the line from (3.4, 2.0) to (6.5, 9.5)
Enter three points for p0, p1, and p2: 4.4 2 6.5 9.5 -5 4
(-5.0, 4.0) is on the left side of the line from (4.4, 2.0) to (6.5, 9.5)
Enter three points for p0, p1, and p2: 1 1 5 5 2 2
(2.0, 2.0) is on the line from (1.0, 1.0) to (5.0, 5.0)
Enter three points for p0, p1, and p2: 3.4 2 6.5 9.5 5 2.5
(5.0, 2.5) is on the right side of the line from (3.4, 2.0) to (6.5, 9.5)
import java.util.Scanner; public class ProgrammingEx3_32 { 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 + ")"; double c = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0); if (c > 0) { System.out.println(p2 + " is on the left side of the line from " + p0 + " to " + p1); } else if (c == 0) { System.out.println(p2 + " is on the line from " + p0 + " to " + p1); } else { System.out.println(p2 + " is on the right side of the line from " + p0 + " to " + p1); } } }
No comments :
Post a Comment