How to determine if one 0-360 longitude range encompasses another

I'm attempting create sectors around the earths equator using longitudes from 0-360. I dont want a user to be able to create a sector that completely encompasses another sector.

Example

Sector 1: minLon = 100 maxLong = 150

If i attempt to create another sector with a min of 100 and max of 151 I want an error.

Similarly it should be able to cross the bounds of 360 So attempting to create the following sector should also cause an error minLon = 350 maxLon = 349

This spans 359 degree's and encompasses the original sector from 100-150.

Other examples that should cause a conflict are Sector 1 minLon = 150 maxLong = 200

Sector 2 minLon = 150 maxLong = 201

I've attempted to convert the degree's from the 0-360 range to -180 to 180 but still ran into issues determining when ranges encompassed another.

I'm not concerned with over lapping sectors just sectors that completely encompass another.

I'm currently working in java but any help in another language or generalizing the solution would be appreciated.

**** Update with More Context and Examples ***** Assume Sector 1 and 2 are defined of which both are valid because neither sector completely encompasses the other (MinLon, MaxLon) Sector 1 (100.0 , 200.0)

Sector 2 (100.1 , 200.1)

The following sector would be invalid and rejected as it resides completely within Sector 1

Sector 3
(100.1 , 200.0)

The following sector is valid with respects to sector 2 as it does not fully encompass it. However, it is invalid and would be rejected as it would completely encompass Sector 1.

Sector 4 (300 , 200)

The following sector would be Valid as its outside Sector 1 and 2 and does not encompass 1 or 2 thus accepted Sector 5 (200.0 , 100.0)

1 answer

  • answered 2020-07-29 17:40 J.D. Luke

    Here's the easiest thing that comes immediately to mind. I am assuming you're not allowed to have a sector that is larger than 360 degrees. If that's the case, it means anytime you have an ending number smaller than the starting number you know it's actually 360 larger than that for the purpose of calculating size (if you needed to account for more than one revolution you'd have to calculate for some multiple of 360).

    start = 350, end = 355, size = (355+(0*360)) - 350 = 5
    start = 359, end = 4, size = (4+(1*360))-359 = 5
    

    Once you have start and size the rest should be straightforward.