# java.awt.Polygon equivalent?

**URL:** <https://www.ruby-forum.com/t/java-awt-polygon-equivalent/154893>\
**Category:** Ruby\
**Created:** [December 4, 2008, 10:01pm UTC](https://www.ruby-forum.com/t/java-awt-polygon-equivalent/154893 "2008-12-04T22:01:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![seurdge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@seurdge](https://www.ruby-forum.com/u/seurdge)\
**Post date:** [December 4, 2008, 10:01pm UTC](https://www.ruby-forum.com/t/java-awt-polygon-equivalent/154893/1 "2008-12-04T22:01:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![seurdge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@seurdge](https://www.ruby-forum.com/u/seurdge)\
**Post date:** [December 5, 2008, 6:35pm UTC](https://www.ruby-forum.com/t/java-awt-polygon-equivalent/154893/2 "2008-12-05T18:35:58Z")

</div>

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.
