-1

Finding a point-in-a-polygon is a common problem, however, I don't understand why many have made edge cases for the prime meridian, equator and international date line. Don't the negative values for points west of the prime meridian and positive values for points east of the prime meridian take care of any problems?

The function below tries to see if a point exists within a rectangle (i.e. it uses two points to determine the rectangle). It doesn't make any checks for the prime meridian or equator and so far I have yet to find a single edge case that my function fails on. Am I missing something or have many programmers unnecessarily tried to deal with this edge case?

Can anyone find any edge cases where the following function would return true/false when it should have returned vice versa? I'm assuming if someone does find one it'd deal with a point in between the prime meridian, equator or international date line.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct point_s
{
  long latitude;
  long longitude;
} point_t;

bool find_point_on_plane(point_t *, point_t *, point_t *);
int main(int argc, char * argv[])
{
  point_t * london    = (point_t *) malloc(sizeof(point_t));
  point_t * cambridge = (point_t *) malloc(sizeof(point_t));
  point_t * x         = (point_t *) malloc(sizeof(point_t));

  london->latitude  = 51.5074;
  london->longitude = -.1278; 
  cambridge->latitude  = 52.2053;
  cambridge->longitude  = 0.1218;
  x->latitude  = 52.0;
  x->longitude = 0.0;

  find_point_on_plane(london, cambridge, x) ? printf("True.\n") : printf("False.\n"); 
  return 0;
}

bool find_point_on_plane(point_t * lower_left, point_t * upper_right, point_t * x)
{
    if(x->latitude <= upper_right->latitude &&
       x->latitude >= lower_left->latitude  &&
       x->longitude <= upper_right->longitude &&
       x->longitude >= lower_left->longitude)
    {
        return true;
    }
    return false;
}
MrPickles
  • 1,255
  • 1
  • 16
  • 31

1 Answers1

1

Problems arise with a window like this:

enter image description here

  • So would I just make a check whether the longitude is greater than zero or less than zero and handle the case, accordingly? – MrPickles Jun 22 '16 at 10:27
  • First ask yourself how you will represent such a window. –  Jun 22 '16 at 10:28
  • I can't just represent it using two points (lower left and upper right) like I've been doing? – MrPickles Jun 22 '16 at 10:29
  • Mh, how you do you distinguish from the plain rectangle with the same corners ? –  Jun 22 '16 at 10:30
  • Still struggling to find the coordinates that break it. For example, (51, -180) = lower left, (52, 180) = upper right and (51.5, -179) = point_that_lies_in_rect returns true...which is correct. Do you have a point that will break this? – MrPickles Jun 22 '16 at 10:36
  • @MrPickles: following your convention (except that you didn't answer my question), this window accepts all longitudes, so no surprise it returns true. –  Jun 22 '16 at 10:39
  • It would return false if longitude (point_to_be_found) = 181. – MrPickles Jun 22 '16 at 10:41
  • Yes, it looks like the func handles that case correctly unless I'm missing something. – MrPickles Jun 22 '16 at 10:44
  • I ran it through my test case...you've yet to provide a point that breaks the func. – MrPickles Jun 22 '16 at 10:49
  • @MrPickles Good luck. –  Jun 22 '16 at 10:49