Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a program to check whether a point is inside or outside the house below.
- * The point is given as a pair of floating-point numbers, separated by a space.
- * Your program should print "Inside" or "Outside". */
- import java.util.Locale;
- import java.util.Scanner;
- public class _09_PointInsideTheHouse {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Locale.setDefault(Locale.ROOT);
- Scanner scan = new Scanner(System.in);
- System.out
- .println("Enter 2 Real numbers for coordinates of your Point (x, y) :");
- double pointX = scan.nextDouble();
- double pointY = scan.nextDouble();
- boolean pointIsInsideTheFloor = checkThePointIsInsideInTheHouseFloor(
- pointX, pointY);
- if (!pointIsInsideTheFloor) {
- boolean pointIsInsideTheRoof = checkTheGivenPointIsInsideInTheRoof(
- pointX, pointY);
- String result = pointIsInsideTheRoof ? "Inside" : "Outside";
- System.out.printf("That point is %s of the House!%n", result);
- } else {
- System.out.println("That point is Inside of the House!");
- }
- }
- private static boolean checkThePointIsInsideInTheHouseFloor(double pX,
- double pY) {
- double minX = 12.5, maxX = 17.5;
- double minY = 8.5, maxY = 13.5;
- boolean pointInsideLeft = pX >= minX && pX <= maxX && pY >= minY
- && pY <= maxY;
- minX = 20;
- maxX = 22.5;
- boolean pointInsideRight = pX >= minX && pX <= maxX && pY >= minY
- && pY <= maxY;
- return (pointInsideLeft || pointInsideRight);
- }
- private static boolean checkTheGivenPointIsInsideInTheRoof(double pX,
- double pY) {
- if (pX == 17.5 && pY == 3.5) {
- return true;
- }
- // Now will using defined class Point:
- Point givenPoint = new Point(pX, pY);
- Point[] trianglePoints = { new Point(12.5, 8.5), new Point(17.5, 3.5),
- new Point(22.5, 8.5) };
- for (int i = 0; i < trianglePoints.length; i++) {
- Point pointA = trianglePoints[i % 3];
- Point pointB = trianglePoints[(i + 1) % 3];
- boolean valueIsNegative = checkTheValueOrientationOfGivenPointAndOneSideOfTriangle(
- givenPoint, pointA, pointB);
- if (valueIsNegative) {
- return false;
- }
- }
- return true;
- }
- private static boolean checkTheValueOrientationOfGivenPointAndOneSideOfTriangle(
- Point givenPoint, Point p1, Point p2) {
- double result = ((Point.minusX(p2, p1) * (Point.minusY(givenPoint, p1))) - (Point
- .minusY(p2, p1) * Point.minusX(givenPoint, p1)));
- return result < 0 ? true : false;
- }
- }
- // User defining class Point
- public class Point {
- private double x, y;
- public Point(double x, double y) {
- this.x = x;
- this.y = y;
- }
- public double getX() {
- return x;
- }
- public void setX(double x) {
- this.x = x;
- }
- public double getY() {
- return y;
- }
- public void setY(double y) {
- this.y = y;
- }
- public static double minusX(Point p1, Point p2) {
- return p1.x - p2.x;
- }
- public static double minusY(Point p1, Point p2) {
- return p1.y - p2.y;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement