Forum: Ruby java.awt.Polygon equivalent ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Serge S. (Guest)
on 2008-12-04 23:01
Hello all !

Im searching for a java.awt.Polygon alike class in Ruby, any cue ?

I need to define a polygon with a set of latitude,longitude points and
check if other latitude,longitude points are in this polygon after...

Any help appreciated ! Thx

Serge S.
Serge S. (Guest)
on 2008-12-05 19:35
Here is the simple implementation that I made to suit my needs just in
case someone is interested :

  class Point

    attr_accessor :x
    attr_accessor :y

    def initialize(x,y)
      @x = x
      @y = y
    end

  end

  class Polygon

    def initialize
      @polygon = []
    end

    def add_point(point)
      @polygon[@polygon.size] = point
    end

    def inside_polygon?(point)

      counter = 0;

      p1 = @polygon[0]
      i = 1
      @polygon.each do
        p2 = @polygon[i % nombre_points]
        if (point.y > MIN(p1.y,p2.y))
          if (point.y <= MAX(p1.y,p2.y))
            if (point.x <= MAX(p1.x,p2.x))
              if (p1.y != p2.y)
                xinters = (point.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
                if (p1.x == p2.x || p.x <= xinters)
                  counter = counter + 1
                end
              end
            end
          end
        end
        i = i+1
        p1 = p2
      end

      if (counter % 2 == 0)
        return false
      else
        return true
      end

    end

    def nombre_points
      return @polygon.size
    end

    def MIN(a, b)
      if a < b
        return a
      else
        return b
      end
    end

    def MAX(a, b)
      if a > b
        return a
      else
        return b
      end
    end


  end





Serge S. wrote:
> Hello all !
>
> Im searching for a java.awt.Polygon alike class in Ruby, any cue ?
>
> I need to define a polygon with a set of latitude,longitude points and
> check if other latitude,longitude points are in this polygon after...
>
> Any help appreciated ! Thx
>
> Serge S.
This topic is locked and can not be replied to.