CGI-Imagemap

 view release on metacpan or  search on metacpan

lib/CGI/Imagemap.pm  view on Meta::CPAN

  # Ellipse equation: (x-$cx)^2/major^2 + (y-$cy)^2/minor^2 = 1
  # If the point ($x,$y) plugged into the equation is > 1 =>
  # pt is outside, if <=1, pt. is inside..
  return (($x-$cx)**2/$major**2 + ($y-$cy)**2/$minor**2)<=1;
}

# return true if point is in circle
sub _circle {
  my($self, $x, $y, $target) = @_;
  my($cx,$cy,$ex,$ey) = $target =~ m/(\d+),(\d+)\s+(\d+),(\d+)/;

  my($distanceP,$distanceE);

  # compare squares of distance from center of edgepoint and given point

  $distanceP = ($cx - $x)**2 + ($cy - $y)**2;
  $distanceE = ($cx - $ex)**2 + ($cy - $ey)**2;

  return ($distanceP <= $distanceE);
}

# return true if point is in given polygon
sub _poly {
  my($self, $x, $y, $target) = @_;
  my($pn, @px, @py) = 0;
  my($i,$intersections,$dy,$dx,$b,$m,$x1,$y1,$x2,$y2);

  # We'll treat the test point as the origin, so translate each
  # point in the polygon appropriately
  while($target =~ s/\s*(\d+),(\d+),?//) {
    $px[$pn] = $1 - $x;
    $py[$pn] = $2 - $y;
    $pn++;
  }

  # A polygon with less than 3 points is an error
  return 0 if $pn < 3;

  # Close the polygon
  $px[$pn] = $px[0];
  $py[$pn] = $py[0];

  # Now count the number of line segments in the polygon that intersect
  # the left side of the X axis.  If it's an odd number we are inside the
  # polygon.

  # Assume no intersection
  $intersections=0;

  for($i = 0; $i < $pn; $i++) {
    $x1 = $px[$i  ]; $y1 = $py[$i  ];
    $x2 = $px[$i+1]; $y2 = $py[$i+1];

    # Line is completely to the right of the Y axis
    next if( ($x1>0) && ($x2>0) );

    # Line doesn't intersect the X axis at all
    next if( (($y1<=>0)==($y2<=>0)) && (($y1!=0)&&($y2!=0)) );

    # Special case.. if the Y on the bottom=0, we ignore this intersection
    # (otherwise a line endpoint counts as 2 hits instead of 1)
    if( $y2>$y1 ){
      next if $y2==0;
    }
    elsif( $y1>$y2 ){
      next if $y1==0;
    }
    else {
      # Horizontal span overlaying the X axis.  Consider it an intersection 
      # iff. it extends into the left side of the X axis
      $intersections++ if ( ($x1 < 0) || ($x2 < 0) );
      next;
    }

    # We know line must intersect the X axis, so see where
    $dx = $x2 - $x1;

    # Special case.. if a vertical line, it intersects
    unless ( $dx ) {
      $intersections++;
      next;
    }

    $dy = $y2 - $y1;
    $m = $dy / $dx;
    $b = $y2 - $m * $x2;
    next if ( ( (0 - $b) / $m ) > 0 );

    $intersections++;
  }

  # If there were an odd number of intersections to the left of the origin
  # (the clicked-on point) then it is within the polygon
  return ($intersections % 2);
}

1;

__END__

=pod

=head1 NAME

CGI::Imagemap - interpret NCSA imagemaps for CGI programs

=head1 SYNOPSIS

  use CGI::Imagemap;
 
  $map = new CGI::Imagemap;

  $map->addmap(-file=>"image.map");
  #OR
  $map->addmap(@map);

  eval { $action = $map->action($x,$y) };
  #Check $@ for errors

=head1 DESCRIPTION



( run in 3.017 seconds using v1.01-cache-2.11-cpan-140bd7fdf52 )