java.awt.Polygon equivalent?


#1

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.


#2

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.