in reply to Permutation and comparison loop

It only shows the relationships in one directly, but it works:

use strict; use warnings; <DATA>; # Header my @images = map {chomp; [split ',']} <DATA>; for my $i (0..$#images) { for my $j ($i+1..$#images) { # Test for Min > Max for outside of range. next if $images[$i][1] > $images[$j][2] || $images[$j][1] > $images[$i][2] || $images[$i][3] > $images[$j][4] || $images[$j][3] > $images[$i][4]; print "$images[$i][0] overlaps $images[$j][0]\n"; } } __DATA__ Image number,minlat,maxlat,minlon,maxlon 1,0,30,20,50 2,10,30,70,90 3,70,80,40,50 4,40,70,20,50 5,20,75,40,80

Replies are listed 'Best First'.
Re^2: Permutation and comparison loop
by spacegeologist (Initiate) on Apr 20, 2011 at 23:14 UTC

    Thanks, this works great. The input data I showed you was simplified. My real input data gives the latitude (from -90 to 90) & longitude (0 to 360) for each corner of the image. Now I am trying to adapt the code to these new requirements. The difficult part is that when an image crosses the 0 to 360 longitude line the code can't correctly identify what it overlaps with. I need an elegant solution to this.

      • For the latitudes, if you add 90 to minlat and maxlat, then you remove the negative numbers and the normal comparisons work.

        Provided that your images do not cross the North or South poles. If they do, then things get real weird and you need to move to an alternative coordinates system.

      • For the longitudes, if you add 360 to both coordinates, then you remove the 0 from the picture and the math again works
      #! perl -slw use strict; use enum qw[ IMAGENO MINLAT MAXLAT MINLON MAXLON ]; sub overlapLat { my( $a, $b ) = @_; return if $a->[MINLAT]+90 > $b->[MAXLAT]+90 or $a->[MAXLAT]+90 < $b->[MINLAT]+90; return 1; } sub overlapLon { my( $a, $b ) = @_; return if $a->[MINLON]+360 > $b->[MAXLON]+360 or $a->[MAXLON]+360 < $b->[MINLON]+360; return 1; } ##discard header <DATA>; my @rects = map[ split ',' ], <DATA>; for my $first ( 0 .. $#rects ) { for my $second ( $first + 1 .. $#rects ) { if( overlapLat( @rects[ $first, $second ] ) and overlapLon( @rects[ $first, $second ] ) ) { print "@{$rects[ $first ] } overlaps \n@{ $rects[ $second +] }"; } } } __DATA__ "Image number","minlat","maxlat","minlon","maxlon" ...

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      Why reinventing the wheel?

      Clipping algorithms are the basis of most computer graphics book.

      IMHO Cohen–Sutherland is quite straight forward...

      Nota bene: you need spherical geometry to calculate "lines" (great circles) connecting "corners". But then it's the same principle.

      Latitudes are not the shortest connections between 2 points!!!

      Cheers Rolf

      This is no problem

      All you have to do is be careful how you define your min and max longitude. If an area starts at longitude 350 and is 20 clicks wide going to 10, then your minimum longitude is 350 and max is 10. The same algorithm works that either I and BrowserUk provided

      If your data is defined so that it gives you a range of 350-370, then all you need to is take modulus 360 of your data ($max = $max % 360). Same thing is true if it's -10 to 20 ($min = $min % 360).

      There's no reason to touch latitudes, as they won't ever be outside of the -90 to 90 range.

        There's no reason to touch latitudes, as they won't ever be outside of the -90 to 90 range.

        Satellites that do global imaging tend to fly in polar orbits. So, it possible to imagine an image taken when the sat is directly over the pole, (I think that this is technically infeasible, but they can get pretty damn close), that has coordinates for its four corners:

        (85,45) 90 (85,135) +--------|--------+ | | North | | | Pole | | | / | 0/360 +--------0--------+ 180 | | | | | | | | | +--------|--------+ (85,315) 270 (85,225)

        If that was represented by the OPs minlat/maxlat, minlon/maxlon system, then you get: nnn, 85, 85, 0, 360 which isn't going to play well for overlap detection.

        The simplest fix is to break such transition images into two (or 4) parts:

        nnn-a, 85, 90, 0, 180 nnn-b, 85, 90, 180, 360

        But even then, the distortions that occur at high latitudes screws the math as soon as you start doing any trig, so spherical coordinates become pretty much de rigueur.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.