Saturday, June 21, 2014

Java Exercise Nr. 42 (Selections)

Suppose a right triangle is placed in a plane as shown below. The right-angle point is placed at (0, 0), and the other two points are placed at (200, 0), and (0, 100). Write a program that prompts the user to enter
a point with x- and y-coordinates and determines whether the point is inside the triangle.

Solution:

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
double x, y;
double slope, b, y1;

System.out.print("Enter a point's x- and y-coordinates:");
x = s.nextDouble();
y = s.nextDouble();

//finding the equation of the hypotenuse
slope = ((double)(0 - 100) / (double)(200 - 0));
b = 100 + slope * 0;

y1 = slope * x + b;
System.out.println(slope);
System.out.println(b);
System.out.println(y1);

        // point is in the triangle if yp <= yx
if(y <= y1) {
System.out.print("The point is in the triangle.");
} else {
System.out.print("The point is not in the triangle.");
}
}

2 comments:

  1. Very nice post.Thanks for sharing your knowledge to us.This is very helpfull programming post.
    Advantages Of DBMS Over File Management System

    ReplyDelete
  2. i got lost at the point where u calculating for the equation for the hypotenuse
    i understand the slope part but the formula for the b is wat am not getting
    bcos b is also used in calculating the value of Y1.
    I will appreciate it if u can clarify.

    ReplyDelete

Author