In: Computer Science
Q4: Write a program that takes as input two opposite corners of a rectangle: (x1,y1) and (x2,y2). Finally, the user is prompted for the coordinates of a third point (x,y). The program should print Boolean value True or False based on whether the point (x,y) lies inside the rectangle. If the point lies on the rectangle, the program should print False.
Program should work irrespective of the order in which the user inputs the opposite coordinates. program user should be able to input coordinates in all possible orders.
- Right upper coordinate first and then left lower coordinate
- Left lower coordinate first and then right upper coordinate
- Left upper coordinate first and then right lower coordinate
- Right lower coordinate first and then left upper coordinate
import java.util.Scanner;
public class RectanglePoint
{
// Driver code
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("1 - Right upper coordinate first
and then left lower coordinate\r\n" +
"2 - Left lower
coordinate first and then right upper coordinate\r\n" +
"3 - Left upper
coordinate first and then right lower coordinate\r\n" +
"4 - Right lower
coordinate first and then left upper coordinate");
System.out.print("\nChoose one option :");
int option = sc.nextInt();
if(option==1 || option==2 || option==3 || option==4 )
{}
else {
System.out.println("Please Enter
Correct Option. Exiting Thank You");
System.exit(0);
}
// a program that takes as input two opposite corners
of a rectangle: (x1,y1) and (x2,y2).
System.out.print("Enter (x1,y1) :");
int x1 = sc.nextInt(), y1 = sc.nextInt();
System.out.print("Enter (x2,y2) :");
int x2 = sc.nextInt(), y2 =
sc.nextInt();
// given point
// the user is prompted for the coordinates of a third
point (x,y)
System.out.print("Enter (x,y) to check inside or not
:");
int x = sc.nextInt(), y = sc.nextInt();
// print Boolean value True or False based on
whether the point (x,y) lies inside the rectangle.
// If the point lies on the rectangle, the program
should print False.
// bottom-left and top-right
// corners of rectangle
if(option==2)
{
if (x > x1 && x < x2 &&
y > y1
&& y < y2)
System.out.println("False");
else
System.out.println("True");
}
if(option==1)
{
if (x > y1 && x < y2 &&
y > x1
&& y < x2)
System.out.println("False");
else
System.out.println("True");
}
if(option==3)
{
if (x < x1 && x > x2 &&
y < y1
&& y > y2)
System.out.println("False");
else
System.out.println("True");
}
if(option==4)
{
if (x < y1 && x > y2 &&
y < x1
&& y > x2)
System.out.println("False");
else
System.out.println("True");
}
}
}