The problem 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. Here is a java examples run:
Enter a point's x- and y-coordinates: 100.5 25.5
The point is in the triangle
Breaking it down private static final double SLOPE = - 0.5 D ;
public static void main ( String [] strings ) {
Scanner input = new Scanner ( System . in );
System . out . print ( "Enter a point's x- and y-coordinates: " );
double x = input . nextDouble ();
double y = input . nextDouble ();
input . close ();
double y1 = calculateY1 ( x , y );
if (( x > 200.0 D ) || ( x < 0.0 D ) || ( y > 100.0 D ) || ( y < 0.0 D )) {
System . out . println ( "The point is not in the triangle." );
} else {
if (( y1 <= 100.0 D )) {
System . out . println ( "The point is in the triangle" );
} else {
System . out . println ( "The point is not in the triangle." );
}
}
}
private static double calculateY1 ( double x , double y ) {
double y1 = y + - x * SLOPE ;
return y1 ;
}
Output Enter a point's x- and y-coordinates: 100.5 50.5
The point is not in the triangle.
Geometry points in triangle posted by Justin Musgrove on 09 April 2016
Tagged: java, java-exercises-beginner, intro-to-java-10th-edition, and ch3
Share on: Facebook Google+