package uk.me.graphe.client;

import com.google.gwt.user.client.Window;

public class DrawingPolygon {

    int[] mXArray = {0};
    int[] mYArray = {0};
    double mZoom = 1;
    private int mOffsetX, mOffsetY;
    
    /**
     * Set the polygon coordinates
     * @param newXArray
     * @param newYArray
     */
    public void set(int[] newXArray, int[] newYArray, double newZoom, int newOffsetX, int newOffsetY){
        mXArray = newXArray;
        mYArray = newYArray;
        mOffsetX = newOffsetX;
        mOffsetY = newOffsetY;
        mZoom = newZoom;
    }
    
    /**
     * Returns true if point is in polygon
     * @param x
     * @param y
     * @return true if point is in polygon
     */
    public boolean contains(int x, int y){
        x = (int) ((x+mOffsetX)*mZoom);
        y = (int) ((y+mOffsetY)*mZoom);
        Boolean cont = pointInPoly(mXArray, mYArray, x, y);
        return cont;
    }
    
    private boolean pointInPoly(int X[], int Y[], int x, int y){
        int i, j;
        boolean c = false;
        for (i = 0, j = X.length-1; i < X.length; j = i++){
            if (( ((Y[i]<=y) && (y<Y[j])) || ((Y[j]<=y) && (y<Y[i])) ) &&
            (x < (X[j]-X[i]) * (y-Y[i]) / (Y[j]-Y[i]) + X[i]))
                c = !c;
        }
        return c;
    }
     
}

@@ -25,16 +25,47 @@
 ent;%0D%0A%0D%0A
+import java.util.ArrayList;%0D%0A%0D%0A
 import c
@@ -142,60 +142,8 @@
     
-int%5B%5D mXArray = %7B0%7D;%0D%0A    int%5B%5D mYArray = %7B0%7D;%0D%0A    
 doub
@@ -187,24 +187,178 @@
 tX, mOffsetY
+;%0D%0A    private ArrayList%3CInteger%5B%5D%3E polygonXList = new ArrayList%3CInteger%5B%5D%3E();%0D%0A    private ArrayList%3CInteger%5B%5D%3E polygonYList = new ArrayList%3CInteger%5B%5D%3E()
 ;%0D%0A    %0D%0A   
@@ -478,19 +478,23 @@
 oid set(
-i
+I
 nt
+eger
 %5B%5D newXA
@@ -499,19 +499,23 @@
 XArray, 
-i
+I
 nt
+eger
 %5B%5D newYA
@@ -582,18 +582,25 @@
     
-mXArray = 
+polygonXList.add(
 newX
@@ -600,24 +600,25 @@
 dd(newXArray
+)
 ;%0D%0A        m
@@ -620,18 +620,25 @@
     
-mYArray = 
+polygonYList.add(
 newY
@@ -642,16 +642,17 @@
 ewYArray
+)
 ;%0D%0A     
@@ -754,24 +754,242 @@
  %0D%0A    /**%0D%0A
+     * Resets internal polygon coordinates%0D%0A     */%0D%0A    public void clear()%7B%0D%0A        if(polygonXList.size()%3E0)%7B%0D%0A            polygonXList.clear();%0D%0A            polygonYList.clear();%0D%0A        %7D%0D%0A    %7D%0D%0A    %0D%0A    /**%0D%0A
      * Retur
@@ -1143,32 +1143,55 @@
 int x, int y)%7B%0D%0A
+        Boolean cont;%0D%0A
         x = (int
@@ -1260,31 +1260,218 @@
 );%0D%0A        
-Boolean
+Integer%5B%5D cXArray;%0D%0A        Integer%5B%5D cYArray;%0D%0A        for(int i = 0;i%3CpolygonXList.size();i++)%7B%0D%0A            cXArray=polygonXList.get(i);%0D%0A            cYArray=polygonYList.get(i);%0D%0A           
  cont = poin
@@ -1482,17 +1482,17 @@
 oly(
-m
+c
 XArray, 
 mYAr
@@ -1487,17 +1487,17 @@
 XArray, 
-m
+c
 YArray, 
@@ -1516,19 +1516,74 @@
     
-return cont
+    if(cont == true) return true;%0D%0A        %7D%0D%0A        return false
 ;%0D%0A 
@@ -1630,36 +1630,52 @@
 oly(
-i
+I
 nt
+eger
  X%5B%5D, 
-i
+I
 nt
+eger
  Y%5B%5D, 
-i
+I
 nt
+eger
  x, 
-i
+I
 nt
+eger
  y)%7B

Add ability for DrawingPolygon class to contains more than one set of coordinates