more RE: Polygons & Nodal Points...

cabney cabney at cyberpass.net
Wed Jul 25 15:02:15 CDT 2001


~sdpm~
I can't leave this alone (I have a GD cgi script for visualization :-)

Some refactoring of the in_poly subroutine...

On Mon, 16 Jul 2001, cabney wrote:
> ###
> # is a point x,y inside a polygon defined by @poly?
> sub in_poly
> {
> 	my ( $x, $y, @poly ) = @_;
> 	my $c = 0;
> 	my @ladder = slice ( $x, @poly );
> 
> 	while (@ladder)
> 	{
> 		my $rung = shift @ladder;
> 
> 		# special case: we're on the edge
> 		if ( $y == $rung ) {
> 			# trailing edge
> 			$c++ unless $c % 2;
> 		}
> 		# we know we're on-edge/encompassed-by the polygon
> 		last if ( $y <= $rung );
> 
> 		# odd is in, even is out (promote diversity!)
> 		$c++;
> 	}
> 	return ($c % 2) ? "hit!" : "miss!";
> }

###
# is a point x,y inside a polygon defined by @poly?
sub in_poly
{
	my ( $x, $y, @poly ) = @_;
	my $c = 0;

	my @ladder = slice ( $x, @poly );

	while ( (my $rung) = shift @ladder )
	{
		last if ( $y <= $rung && ($c%2) );
		$c++;
	}
	return ($c % 2) ? "hit!" : "miss!";
}

Hmm, would I have to pay my employer for a negative line count in my
productivity assessment?  Maybe I shoulda got it right the first time...

Where can I get one of those buzzers Larry Wall was using to keep him
on a given topic for a limited time?

CA
-- 
There was a time
A wind that blew so young
For this could be the biggest sky
And I could have the faintest idea

~sdpm~

The posting address is: san-diego-pm-list at hfb.pm.org

List requests should be sent to: majordomo at hfb.pm.org

If you ever want to remove yourself from this mailing list,
you can send mail to <majordomo at happyfunball.pm.org> with the following
command in the body of your email message:

    unsubscribe san-diego-pm-list

If you ever need to get in contact with the owner of the list,
(if you have trouble unsubscribing, or have questions about the
list itself) send email to <owner-san-diego-pm-list at happyfunball.pm.org> .
This is the general rule for most mailing lists when you need
to contact a human.




More information about the San-Diego-pm mailing list