-1

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!

Lesiak
  • 22,088
  • 2
  • 41
  • 65
Sammy65
  • 627
  • 2
  • 12
  • 28
  • "*What to do with it?*" How about simply ignoring it? – Hovercraft Full Of Eels Aug 17 '23 at 18:13
  • 1
    Why don't you use an array of the `Point` class instead of using x1, y1, x2...? – Grinding For Reputation Aug 17 '23 at 18:16
  • @HovercraftFullOfEels, okay ignoring them is fine, but my equation still doesn't seem to work. – Sammy65 Aug 17 '23 at 18:17
  • @GrindingForReputation yeah, it could do with some refactoring but still trying to wrap my haed around the logic for this. – Sammy65 Aug 17 '23 at 18:22
  • One reason that your equations don't work is because you're ignoring the fact that you're dealing with floating-point numbers. There is no true sense of `==` with floating point numbers, given how computers represent them. Side note that I would never use `float` and would always prefer `double` due to improved accuracy. – Hovercraft Full Of Eels Aug 17 '23 at 18:46
  • The numerical stability could be a problem, but there are more serious ones: 1. you calculate the rectangle area incorrectly - I expect 9, your formula gets 8. 2. you sum areas of the wrong triangles - draw a picture and label the vertices (for this particular input you get correct answer, but it is only an accident). More generally - your rectangle has sides parallel to axes - is is a given in your problem? If so, much simpler and numerically stable algorithm exists. – Lesiak Aug 17 '23 at 18:54
  • @HovercraftFullOfEels Voted to reopen, as the solution is wildly wrong, and the problem is not caused by floating point rounding errors. – Lesiak Aug 17 '23 at 19:04
  • @Lesiak: I'll re-open the question. – Hovercraft Full Of Eels Aug 17 '23 at 19:06
  • I just noticed the question was closed without any answer or explanation as to why? It's won't help to take an action without any form of explanation to help the person who asked the question for next ones, would it? – Sammy65 Aug 17 '23 at 19:12
  • *Why* do you have 5 sets of points? Did this problem come from a source outside of your control? (Perhaps this is a homework assignment, or part of one?) In plotting a curve that consists of sequence of `n` line segments, one may be provided with `n + 1` points. Disregarding the possibility of backtracking or the curve intersecting itself, it is a closed curve if and only if the first point is the same as the last point. It might be expected that something like `(x1 == x5 && y1 ==y5)` is part of the test to determine if you have a rectangle or not. – Old Dog Programmer Aug 17 '23 at 19:33
  • 3
    You use the terms "latitude" and "longitude" which confuses the problem since these apply to spherical coordinates - if you intend on measuring on the spherical plane then it requires different formula altogether. – Computable Aug 17 '23 at 19:35
  • Since the points represent latitude and longitude, do the edges of the rectangle run "parallel" to the longitude and latitude lines? Or, do you have to test for the possibilities of obliquely oriented rectangles? And would (0,0) -- (0,90) -- (87, 90) -- (87, 0) -- (0, 0) qualify as a rectangle? – Old Dog Programmer Aug 17 '23 at 19:39
  • @OldDogProgrammer, thanks for your comments! The `(x1 == x5 && y1 == y5)` bit makes a lot of sense to me! Yes, the data is out of my control at the minute. As for the latitude and longitude usage those are for the x and y coordinates provided. – Sammy65 Aug 17 '23 at 19:53
  • 1
    As @Computable suggested, I did take use of "latitude and longitude" to imply that geographic coordinates, not Cartesian coordinates, were being referenced. In my "would ... qualify as a rectangle" question, the order is (latitude, longitude). – Old Dog Programmer Aug 18 '23 at 14:27

1 Answers1

3
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.

Lesiak
  • 22,088
  • 2
  • 41
  • 65
  • Thanks for your answer here, it has made me rethink my approach. I'll start off by first getting the result you've mentioned, then take it from there. I'll revert back with an updated code once I've done those changes. Thanks! – Sammy65 Aug 17 '23 at 19:56
  • 1
    @Sammy65, Changing the code in the question to incorporate changes suggested in answers isn't good practice here, since that can change the question. It might be better to ask a new question. If you do ask a new question, you can reference this question in the new. You can reference the new question by adding a comment to this question to let followers know of the new question. – Old Dog Programmer Aug 17 '23 at 20:07