자바 는 어떤 점 이 그 려 진 다각형/원형 안에 있 는 지 판단 합 니 다.
IsPtInPoly.java
package com.ardo.util.circle;
import java.util.ArrayList;
import java.util.List;
/**
* java ( 【isPtInPoly】/ 【distencePC】)
* @param point
* @param pts
* @return true, false
* @author ardo
*/
public class IsPtInPoly {
/**
*
* @param point
* @param pts
* @return true, false
*/
public static boolean isPtInPoly(Point2D point, List<Point2D> pts){
int N = pts.size();
boolean boundOrVertex = true; // , , true
int intersectCount = 0;//cross points count of x
double precision = 2e-10; // 0
Point2D p1, p2;//neighbour bound vertices
Point2D p = point; //
p1 = pts.get(0);//left vertex
for(int i = 1; i <= N; ++i){//check all rays
if(p.equals(p1)){
return boundOrVertex;//p is an vertex
}
p2 = pts.get(i % N);//right vertex
if(p.x < Math.min(p1.x, p2.x) || p.x > Math.max(p1.x, p2.x)){//ray is outside of our interests
p1 = p2;
continue;//next ray left point
}
if(p.x > Math.min(p1.x, p2.x) && p.x < Math.max(p1.x, p2.x)){//ray is crossing over by the algorithm (common part of)
if(p.y <= Math.max(p1.y, p2.y)){//x is before of ray
if(p1.x == p2.x && p.y >= Math.min(p1.y, p2.y)){//overlies on a horizontal ray
return boundOrVertex;
}
if(p1.y == p2.y){//ray is vertical
if(p1.y == p.y){//overlies on a vertical ray
return boundOrVertex;
}else{//before ray
++intersectCount;
}
}else{//cross point on the left side
double xinters = (p.x - p1.x) * (p2.y - p1.y) / (p2.x - p1.x) + p1.y;//cross point of y
if(Math.abs(p.y - xinters) < precision){//overlies on a ray
return boundOrVertex;
}
if(p.y < xinters){//before ray
++intersectCount;
}
}
}
}else{//special case when ray is crossing through the vertex
if(p.x == p2.x && p.y <= p2.y){//p crossing over p2
Point2D p3 = pts.get((i+1) % N); //next vertex
if(p.x >= Math.min(p1.x, p3.x) && p.x <= Math.max(p1.x, p3.x)){//p.x lies between p1.x & p3.x
++intersectCount;
}else{
intersectCount += 2;
}
}
}
p1 = p2;//next ray left point
}
if(intersectCount % 2 == 0){//
return false;
} else { //
return true;
}
}
/**
*
* @param p
* @param c
* @return
*/
public static String distencePC(Point2D p,Circle c){//
String s ;
double d2 = Math.hypot( (p.getX() - c.getCC().getX() ), (p.getY() - c.getCC().getY()) );
System.out.println("d2=="+d2);
double r = c.getR();
if(d2 > r){
s = " ";
}else if(d2 < r){
s = " ";
}else{
s = " ";
}
return s;
}
public static void main(String[] args) {
Point2D point = new Point2D(116.404072, 39.916605);
//
List<Point2D> pts = new ArrayList<Point2D>();
pts.add(new Point2D(116.395, 39.910));
pts.add(new Point2D(116.394, 39.914));
pts.add(new Point2D(116.403, 39.920));
pts.add(new Point2D(116.402, 39.914));
pts.add(new Point2D(116.410, 39.913));
if(isPtInPoly(point, pts)){
System.out.println(" ");
}else{
System.out.println(" ");
}
//
Point2D centerPoint = new Point2D(116.404172, 39.916605);
Circle c = new Circle();
c.setCC(centerPoint);
c.setR(0.0056);
String s = distencePC(point,c);
System.out.println(" :"+s);
}
}
Circle.java
/**
*
* @author ardo
*
*/
public class Circle {
private double r;
private Point2D cc;
public void setR(double a){
r = a;
}
public void setCC(Point2D centerOfCir){
cc = centerOfCir;
}
public double getR(){
return r;
}
public Point2D getCC(){
return cc;
}
}
Point2D.java
public class Point2D {
public double x;
public double y;
public Point2D(double x, double y) {
super();
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.