Search code examples
javageometrylatitude-longitude

Java - Determine if latitude and longitude are in a set of coordinates which form a rectangle


I've got five coordinates to form as it were a rectangle (x1,y1),(x2,y2),(x3,y3),(x4,y4),(x1,y1). I put the first four points as the edges of the rectangle on a graph, but I don't know what to do with the fifth set of coordinates which are basically the exact ones as the set of first coordinates.
Then, I've got the lat and long coordinates as (x, y) to check if they're within the above rectangle. I thought of using something similar to the code below, but I'm confused of what to do with the fifth coordinate. This won't form a rectangle but also it's the exact same thing as the first set!

public class Main {
   //main method
   public static void main (String[] args) {

       //declared the variables and initialized the point values
      int x1 = 1;
      int y1 = 2;
      int x2 = 4;
      int y2 = 2;
      int x3 = 1;
      int y3 = 5;
      int x4 = 4;
      int y4 = 5;
      
      int x5 = 1; //not used in creating rectangle boundary
      int y5 = 2; //not used in creating rectangle boundary

      //given point to be checked
      int x = 2;
      int y = 2;

      //calling the user defined method to check if point lies inside rectangle
      if (checkPoint(x1,y1, x2,y2, x3,y3, x4,y4, x,y))
         System.out.print("Yes, given point lies inside rectangle");
      else
         System.out.print("No, given point does not lie inside rectangle");
   }

   //user defined method to find area of triangle
   static float triangleArea(int x1, int y1, int x2, int y2, int x3, int y3) {
      return (float)Math.abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0);
   }

   //user defined method to check whether point lies inside rectangle or not
   static boolean checkPoint(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, int x, int y) {

      //area of rectangle ABCD
      float ABCD = (x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2))/2 + (x1*(y4-y3) + x4*(y3-y1) + x3*(y1-y4))/2;

      //find area of triangle PAB
      float PAB = triangleArea(x, y, x1, y1, x2, y2);

      //find area of triangle PBC
      float PBC = triangleArea(x, y, x2, y2, x3, y3);

      //find area of triangle PCD
      float PCD = triangleArea(x, y, x3, y3, x4, y4);

      // find area of triangle PAD
      float PAD = triangleArea(x, y, x1, y1, x4, y4);

      //check if area of rectangle is
      //equal to areas formed by four triangles
      if(ABCD == PAB + PBC + PCD + PAD)
      //then return true
         return true;
      else
         //else return false
         return false;
   }
}

The above code doesn't find the lat and long (x, y) within the specified rectangle though, so not sure if it's because I'm not using the fifth coordinate or something. Any assistance would be much appreciated. Cheers!


Solution

  • C+----+D
     |    |
     |    |
     |    |
     |    |
    A+-P--+B
    
    A = (x1, y1) = (1, 2);
    B = (x2, y2) = (2, 4);
    C = (x3, y3) = (1, 5);
    D = (x4, y4) = (4, 5);
    
    

    Problem 1: Your formula for rectangle area is wrong.

    For example data you should get 9.0, but you get 8.0

    float ABCD = 2.0 * triangleArea(x1, y1, x2, y2, x3, y3);
    

    Problem 2: You sum wrong triangles.

    You sum PAB + PBC + PCD + PAD

    You should be summing PAB + PAC + PCD + PBD

    Note that for example data you the result happens to identical

    Problem 3: rounding errors

    See What's wrong with using == to compare floats in Java?

    See Is floating point math broken?

    Problem 4: Better algortihms exist

    Your rectangle happens to have sides parallel to the axes.