java.awt.Polygon equivalent ?

S

Serge Savoie

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 Savoie
 
S

Serge Savoie

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top