Thread perl highlight (25 answers)
Opened by esskar at 2003-08-11 16:32

esskar
 2003-08-11 16:32
#31699 #31699
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use strict;

my @x = (0, 2, 4);
my @y = (0, 2, 0);

my &#36polygon =  { x=>\@x, y=>\@y };
my &#36point1 =  { x=>2, y=>1 };
my &#36point2 =  { x=>5, y=>5 };
my &#36point3 =  { x=>1, y=>1 };

sub min
{
    return &#36_[0] < &#36_[1] ? &#36_[0] : &#36_[1];
}

sub max
{
    return &#36_[0] > &#36_[1] ? &#36_[0] : &#36_[1];
}

sub isInside
{
    # return values: -1 == draussen, 0 == auf Kante , 1 == innerhalb
   my &#36polygon = shift;
   my &#36point = shift;

   my &#36nPPoints = 0;
      
   return -1 if((&#36nPPoints = scalar(@{&#36polygon->{x}})) != scalar(@{&#36polygon->{y}}));   
   return -1 unless &#36nPPoints;
   
   my &#36x = &#36point->{x};
   my &#36y = &#36point->{y};
   my &#36ax = &#36polygon->{x}->[&#36nPPoints-1];
   my &#36ay = &#36polygon->{y}->[&#36nPPoints-1];
   my &#36lx = &#36ax;
   my(&#36bx, &#36by) = (0, 0);
   my(&#36count, &#36ignore, &#36zx) = (0, 0, 0);

   for my &#36i (0..&#36nPPoints-1)
   {
      &#36bx = &#36polygon->{x}->[&#36i];
      &#36by = &#36polygon->{y}->[&#36i];
      
      if(!&#36ignore)
      {
         if(&#36by == &#36y and &#36bx>=&#36x)
         {
            return 0 if &#36bx == &#36x;
            &#36ignore = 1;
         }
         else
         {
            if((&#36ay < &#36y) == (&#36y < &#36by))
            {
               if(&#36x < min(&#36ax, &#36bx)) { &#36count++; }
               elsif(&#36x > max(&#36ax, &#36bx)) {}
               elsif(&#36x < (&#36zx = ((&#36bx-&#36ax)*(&#36y-&#36ay)/(&#36by-&#36ay))+&#36ax)) { &#36count++; }
               elsif(&#36zx == &#36x) { return 0; }               
 
            }
            &#36ax = &#36bx; &#36ay = &#36by;
         }
      }
      else
      {
          if(&#36by == &#36y)
          {
              return 0 if(((&#36lx < &#36x) == (&#36x<&#36bx)) or &#36bx == &#36x);
          }
          else
          {
              &#36count++ if((&#36ay < &#36y) == (&#36y < &#36by));
              &#36ignore = 0;
              &#36ax = &#36bx; &#36ay = &#36by;
          }                    
      }
      &#36lx = &#36bx;
   }    
   
   return (&#36count & 1) ? 1 : -1;
}

print "Point1: ", isInside(&#36polygon, &#36point1),"\n"; 
print "Point2: ", isInside(&#36polygon, &#36point2),"\n";
print "Point3: ", isInside(&#36polygon, &#36point3),"\n";
\n\n

<!--EDIT|esskar|1060617662-->

View full thread perl highlight