java判断某个点是否在所画多边形/圆形内
作者:阿杜_ardo 时间:2022-09-30 23:50:45
本文实例为大家分享了java判断某个点是否在所画范围内的具体代码,供大家参考,具体内容如下
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;
}
}
来源:https://blog.csdn.net/ardo_pass/article/details/78552592?locationNum=2&fps=1
标签:java,多边形,圆形
0
投稿
猜你喜欢
android自动生成dimens适配文件的图文教程详解(无需Java工具类)
2023-07-17 12:12:30
RestTemplate使用不当引发的问题及解决
2023-01-21 11:27:19
C#模式画刷HatchBrush用法实例
2021-10-01 08:59:45
Android实现视图轮播效果
2023-04-14 08:52:10
C#实现简单的计算器小功能
2023-05-13 06:46:11
详解Java中的Lambda表达式
2021-08-17 00:06:00
Java如何利用return结束方法调用
2022-05-20 19:58:35
Springboot与vue实现数据导出方法具体介绍
2023-11-06 21:00:34
C#实现读取匿名对象属性值的方法示例总结
2023-02-02 18:56:03
详解IDEA的快捷键及智能提示
2023-10-18 07:27:03
Java保留两位小数的几种写法总结
2022-04-19 06:53:06
Android入门之实现自定义Adapter
2021-09-30 17:34:10
Android使用Intent显示实现页面跳转
2023-04-16 12:02:21
Android中TextView自动适配文本大小的几种解决方案
2022-05-28 09:08:01
Android MaterialCardView的使用介绍与示例
2023-06-23 06:31:56
Android设置项目为系统APP方法
2022-11-16 08:55:02
Java启用Azure Linux虚拟机诊断设置
2022-06-28 05:42:51
Webview实现android简单的浏览器实例代码
2023-04-20 15:21:57
shiro实现单点登录(一个用户同一时刻只能在一个地方登录)
2022-07-04 01:37:55
如何安装java的运行环境IDEA
2022-09-20 10:42:38