coollearner
coollearner

Reputation: 23

java coding a solution in determining if a point lies on the interior of a polygon(rectangle)

I was reading Solution 3 (2D) in website and tried to translate this into a java code. Please comment if the java is correct. Instead of x and y coordinates, i am using latitude and longitude coordinates (note: loc.getLongitude(),loc.getLatitude() is the set of points being checked if inside the rectangle and the given coordinates (double) are not real coordinates yet.

        double lat0 = 0.2;
        double long0 = 0.3;
        double lat1 = 1.2;
        double long1 = 1.3;
        double lat2 = 1.2;
        double long2 = 1.3;
        double lat3 = 1.2;
        double long3 = 1.3;
        double rel1 = (loc.getLongitude()- long0)*(lat1 - lat0)- ((loc.getLatitude()-lat0)*(long1-long0));
        double rel2 = (loc.getLongitude()- long1)*(lat2 - lat1)- ((loc.getLatitude()-lat1)*(long2-long1));
        double rel3 = (loc.getLongitude()- long2)*(lat3 - lat2)- ((loc.getLatitude()-lat2)*(long3-long2));
        double rel4 = (loc.getLongitude()- long3)*(lat0 - lat3)- ((loc.getLatitude()-lat3)*(long0-long3));

        if (rel1 >= 0 && rel2 >= 0 && rel3 >= 0 && rel4 >= 0 )
        {
            try
            {
            ..........
                            ..........
            }
        else
        {
            .........

        }
        }

Upvotes: 0

Views: 181

Answers (1)

OleGG
OleGG

Reputation: 8657

  1. All rel1..rel4 must be of the same sign, not essentially positive, maybe - all negative.
  2. It will work only for convex polygons.
  3. I don't know if it fair to use 2d algorithm on 3d model (latitudes and longitudes are from ploar coordinates, aren't they?). Yes, in general cases it will give right result, but in some cases you'll need to deal with the situation, when line, connecting two points "on globe" will not be the same, as it is in projection.

Upvotes: 1

Related Questions