package btools.server; import java.io.BufferedReader; import java.io.IOException; import java.util.ArrayList; import java.util.StringTokenizer; public class Polygon { private int[] ax; private int[] ay; private int minx = Integer.MAX_VALUE; private int miny = Integer.MAX_VALUE; private int maxx = Integer.MIN_VALUE; private int maxy = Integer.MIN_VALUE; public Polygon( BufferedReader br ) throws IOException { ArrayList lines = new ArrayList(); for(;;) { String line = br.readLine(); if ( line == null || "END".equals( line ) ) { break; } lines.add( line ); } int n = lines.size(); ax = new int[n]; ay = new int[n]; for( int i=0; i maxx ) maxx = x; if ( y > maxy ) maxy = y; } } public boolean isInPolygon( long id ) { int x = (int) ( id >> 32 ); int y = (int) ( id & 0xffffffff ); if ( x < minx || x > maxx || y < miny || y > maxy ) { return false; } int n = ax.length-1; // these are closed polygons boolean inside = false; int j = n - 1; for (int i = 0 ;i < n ; j = i++) { if ( (ay[i] > y) != (ay[j] > y) ) { long v = ax[j] - ax[i]; v *= y - ay[i]; v /= ay[j] - ay[i]; if ( x <= v + ax[i]) { inside = !inside; } } } return inside; } public boolean isInBoundingBox( long id ) { int x = (int) ( id >> 32 ); int y = (int) ( id & 0xffffffff ); return x >= minx && x <= maxx && y >= miny && y <= maxy; } }