import java.io.PrintStream;
import java.text.DecimalFormat;
//多边形算法
public class PolygonAreaJudge {
private Polygon polygon;
private int rate = 1000000;
private double maxX = 0.0D;
private double minX = 0.0D;
private double maxY = 0.0D;
private double minY = 0.0D;
//创建多边形 多边形坐标点
public PolygonAreaJudge(String axisStr) {
this.rate = RuleUtil.rate;
createPolygon(axisStr);
}
private boolean createPolygon(String axisStr) {
if (StringUtils.isEmpty(axisStr)) {
return false;
}
this.polygon = new Polygon();
String[] axisInfo = axisStr.split("\\;");
String currAxis = "";
for (int cnt = 0; cnt < axisInfo.length; cnt++) {
currAxis = axisInfo[cnt];
if (!StringUtils.isEmpty(currAxis)) {
int pos = currAxis.indexOf(",");
if (pos != -1) {
int y;
int x ;
try {
//十进制转换
DecimalFormat decimalFormat = new DecimalFormat("#########");
double dX = Double.parseDouble(axisInfo[cnt].substring(0, pos).trim()) * this.rate;
String sX = decimalFormat.format(dX);
if (sX.indexOf(".") != -1) {
sX = sX.substring(0, sX.indexOf("."));
}
x = Integer.parseInt(sX);
double dY = Double.parseDouble(axisInfo[cnt].substring(pos + 1, currAxis.length()).trim())
* this.rate;
String sY = decimalFormat.format(dY);
if (sY.indexOf(".") != -1) {
sY = sY.substring(0, sY.indexOf("."));
}
y = Integer.parseInt(sY);
} catch (NumberFormatException e) {
continue;
}
this.polygon.addPoint(x, y);
}
}
}
return true;
}
//创建边界
private boolean creatBounds() {
Rectangle rectangle = this.polygon.getBounds();
this.maxX = rectangle.getMaxX();
this.maxY = rectangle.getMaxY();
this.minX = rectangle.getMinX();
this.minY = rectangle.getMinY();
return true;
}
public boolean checkIsInArea(int x, int y) {
if (this.polygon == null) {
return false;
}
return this.polygon.contains(x, y);
}
public boolean isInArea(float x, float y) {
int iX = (int) (x * this.rate);
int iY = (int) (y * this.rate);
return checkIsInArea(iX, iY);
}
public boolean isInArea(String x, String y) {
if (this.polygon == null) {
return false;
}
if (!isInAreaBounds(x, y)) {
return false;
}
int iX = RuleUtil.toInt(x);
int iY = RuleUtil.toInt(y);
return checkIsInArea(iX, iY);
}
public boolean isInAreaBounds(String x, String y) {
double ix = Double.parseDouble(x);
double iy = Double.parseDouble(y);
if ((ix >= this.minX) && (ix <= this.maxX) && (iy >= this.minY) && (iy <= this.maxY)) {
return true;
}
return false;
}
public static void main(String[] args) {
PolygonAreaJudge poly = new PolygonAreaJudge(
"118.126592,30.130026;118.213014,30.129031;118.2391,30.08164;118.170469,30.080835;118.112945,30.087563;118.102403,30.108645");
System.out.println("IsInArea is " + poly.isInArea(118.11251F, 30.115244F));
System.out.println("IsInArea is " + poly.isInArea("118.112511", "30.115244"));
System.out.println("IsInArea is " + poly.isInArea(118.12659F, 30.130026F));
poly = new PolygonAreaJudge("0,0;0,5;2,8;5,5;5,0");
System.out.println("IsInArea is " + poly.isInArea(2.0F, 3.0F));
}
}
RuleUtil辅助代码
RuleUtil.rate=1000000;
public static int toInt(String coordinate) {
double dx = Double.parseDouble(coordinate);
Double rdx = new Double(dx * rate);
long lx = rdx.longValue();
int ix = (int) lx;
return ix;
}