地圖演算法(二):判斷當前點是不是在該多邊形上
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;
}
相關文章
- 地圖演算法(一):判斷當前點是不是在該線上地圖演算法
- 如何判斷一個點在地圖上?如何判斷一個點在多邊形內?地圖
- 判斷點是否在多邊形內斷點
- 判斷點是否在多邊形內部斷點
- JS 射線法 判斷點是否在多邊形內部JS斷點
- 地圖 判斷點在區域內地圖斷點
- 判斷當前瀏覽器是不是微信瀏覽器瀏覽器
- AutoCAD C# 判斷多邊形與點的位置關係C#
- javascript判斷當前瀏覽器是不是微信瀏覽器JavaScript瀏覽器
- 視覺化學習:利用向量判斷多邊形邊界視覺化
- 判斷點是否在多邊形內的Python實現及小應用(射線法)斷點Python
- 多邊形裁剪二:Weiler-Atherton演算法演算法
- 百度地圖繪製多邊形區域地圖
- 【計算幾何】點在多邊形內部
- JS在瀏覽器中判斷當前網路狀態JS瀏覽器
- android下不規則多邊形填充點陣圖Android
- Android 判斷當前系統語言Android
- JZ79 判斷是不是平衡二叉樹二叉樹
- 舒服的判斷當前應用是否處於前臺
- 在 Web 中判斷頁面是不是重新整理Web
- js 怎樣判斷使用者是否在瀏覽當前頁面JS
- JS判斷當前裝置是 PC IOS AndriodJSiOS
- jQuery判斷當前元素是第幾個元素jQuery
- android判斷當前系統的語言Android
- vue透過自定義指令判斷當前元素是否在可視區域Vue
- javascript如何判斷當前瀏覽器是否支援cookieJavaScript瀏覽器Cookie
- Android判斷當前棧頂Activity的包名Android
- Python判斷當前使用者是否是rootPython
- 手機的九宮格圖形解鎖演算法(判斷是否合法)演算法
- 如何判斷深度學習推理是不是真的跑在顯示卡上了深度學習
- 在Linux下判斷系統當前是否開啟了超執行緒Linux執行緒
- 判斷客戶端是iOS還是Android,判斷是不是在微信瀏覽器開啟客戶端iOSAndroid瀏覽器
- JavaScript判斷當前裝置和瀏覽器型別JavaScript瀏覽器型別
- 判斷Android 當前版本是否為debug版本Android
- Android判斷當前裝置是否設定了代理Android
- 如何判斷當前瀏覽器是否支援html5瀏覽器HTML
- Android 判斷當前的介面是否是桌面的方法Android
- 判斷當前viewcontroller是否是某個特定類ViewController