import java.util.*;
public class ryamethod{
static class Point{
float x;
float y;
Point(){}
Point(float x, float y){
this.x = x;
this.y = y;
}
}
public static boolean ray(Point p, List<Point> poly) {
float nx = p.x; float ny = p.y;
int cnt = 0; //计算射线穿过多边形的点的数目
int len = poly.size();
for(int i = 0, j = len - 1; i < len; j = i, ++i) {
float bx = poly.get(i).x; float by = poly.get(i).y;
float ux = poly.get(j).x; float uy = poly.get(j).y;
//点与多边形顶点重合
if((nx == bx && ny == by) || (nx == ux && nx == uy)) {
return true;
}
if((by < ny && uy >= ny) || (by >= ny && uy < ny)) {
//边上与点的坐标y相同的x坐标
float x = bx + (ny - by) * (ux - bx) / (uy - by);
//点在多边形的边上
if(x == nx) {
return true;
}
if(x > nx) {
cnt += 1;
}
}
}
if(cnt % 2 == 1) {
return true;
}
else {
return false;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要判断的点");
float x , y;
x = sc.nextFloat();
y = sc.nextFloat();
Point p = new Point(x, y);
System.out.println("请输入多边形顶点个数:");
int n = sc.nextInt();
System.out.println("请输入多边形的各个顶点:");
List<Point> poly = new ArrayList<>();
for(int i = 0; i < n; ++i) {
x = sc.nextFloat();
y = sc.nextFloat();
Point pot = new Point(x, y);
poly.add(pot);
}
boolean flag = rayCasting(p, poly);
if(flag == true) {
System.out.println("点在多边形内");
}else {
System.out.println("点不在多边形内");
}
}
}
----------------
//
测试前端判断点是否在面内
function judgePointINPolygon(point, vs){ //point --[x,y] polygon [点一,点二,点三]
不闭合的点集
var x = point[0], y = point[1];
var inside = false;
for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) {
var xi = vs[i][0], yi = vs[i][1];
var xj = vs[j][0], yj = vs[j][1];
var intersect = ((yi > y) != (yj > y))
&& (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}
return inside;
}
————————————————
版权声明:本文为CSDN博主「六无青年」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/liuwuqingnian/article/details/78959399
public class Point {
/**
* 是否有 横断<br/> 参数为四个点的坐标
*
* @param px1
* @param py1
* @param px2
* @param py2
* @param px3
* @param py3
* @param px4
* @param py4
* @return
*/
public static boolean isIntersect(double px1, double py1, double px2, double py2,
double px3, double py3, double px4, double py4) {
boolean flag = false;
double d = (px2 - px1) * (py4 - py3) - (py2 - py1) * (px4 - px3);
if (d != 0) {
double r = ((py1 - py3) * (px4 - px3) - (px1 - px3) * (py4 - py3))
/ d;
double s = ((py1 - py3) * (px2 - px1) - (px1 - px3) * (py2 - py1))
/ d;
if ((r >= 0) && (r <= 1) && (s >= 0) && (s <= 1)) {
flag = true;
}
}
return flag;
}
/**
* 目标点是否在目标边上边上<br/>
*
* @param px0
* 目标点的经度坐标
* @param py0
* 目标点的纬度坐标
* @param px1
* 目标线的起点(终点)经度坐标
* @param py1
* 目标线的起点(终点)纬度坐标
* @param px2
* 目标线的终点(起点)经度坐标
* @param py2
* 目标线的终点(起点)纬度坐标
* @return
*/
public static boolean isPointOnLine(double px0, double py0, double px1,
double py1, double px2, double py2) {
boolean flag = false;
double ESP = 1e-9;// 无限小的正数
if ((Math.abs(Multiply(px0, py0, px1, py1, px2, py2)) < ESP)
&& ((px0 - px1) * (px0 - px2) <= 0)
&& ((py0 - py1) * (py0 - py2) <= 0)) {
flag = true;
}
return flag;
}
public static double Multiply(double px0, double py0, double px1, double py1,
double px2, double py2) {
return ((px1 - px0) * (py2 - py0) - (px2 - px0) * (py1 - py0));
}
/**
* 判断目标点是否在多边形内(由多个点组成)<br/>
*
* @param px
* 目标点的经度坐标
* @param py
* 目标点的纬度坐标
* @param polygonXA
* 多边形的经度坐标集合
* @param polygonYA
* 多边形的纬度坐标集合
* @return
*/
public static boolean isPointInPolygon(double px, double py,
List<Double> polygonXA, List<Double> polygonYA) {
boolean isInside = false;
double ESP = 1e-9;
int count = 0;
double linePoint1x;
double linePoint1y;
double linePoint2x = 180;
double linePoint2y;
linePoint1x = px;
linePoint1y = py;
linePoint2y = py;
for (int i = 0; i < polygonXA.size() - 1; i++) {
double cx1 = polygonXA.get(i);
double cy1 = polygonYA.get(i);
double cx2 = polygonXA.get(i + 1);
double cy2 = polygonYA.get(i + 1);
// 如果目标点在任何一条线上
if (isPointOnLine(px, py, cx1, cy1, cx2, cy2)) {
return true;
}
// 如果线段的长度无限小(趋于零)那么这两点实际是重合的,不足以构成一条线段
if (Math.abs(cy2 - cy1) < ESP) {
continue;
}
// 第一个点是否在以目标点为基础衍生的平行纬度线
if (isPointOnLine(cx1, cy1, linePoint1x, linePoint1y, linePoint2x,
linePoint2y)) {
// 第二个点在第一个的下方,靠近赤道纬度为零(最小纬度)
if (cy1 > cy2)
count++;
}
// 第二个点是否在以目标点为基础衍生的平行纬度线
else if (isPointOnLine(cx2, cy2, linePoint1x, linePoint1y,
linePoint2x, linePoint2y)) {
// 第二个点在第一个的上方,靠近极点(南极或北极)纬度为90(最大纬度)
if (cy2 > cy1)
count++;
}
// 由两点组成的线段是否和以目标点为基础衍生的平行纬度线相交
else if (isIntersect(cx1, cy1, cx2, cy2, linePoint1x, linePoint1y,
linePoint2x, linePoint2y)) {
count++;
}
}
if (count % 2 == 1) {
isInside = true;
}
return isInside;
}
}
---------------------------
首先面也是由一系列的点组成,比如,一个正方形。我们可以把它当成四个坐标点然后直线连接而成。
java.awt.geom.GeneralPath为我们实现的这个画线的功能。
下面用一个简单的例子来画一个面
/**
* 将经纬度点集合转换为GeneralPath对象
*
* @param points 点集合(有序)
*
* @return GeneralPath对象
*/
public static GeneralPath genGeneralPath(ArrayList<Point2D.Double> points) {
GeneralPath path = new GeneralPath();
if (points.size() < 3) {
return null;
}
path.moveTo((float) points.get(0).getX(), (float) points.get(0).getY());
for (Iterator<Point2D.Double> it = points.iterator(); it.hasNext();) {
Point2D.Double point = (Point2D.Double) it.next();
path.lineTo((float) point.getX(), (float) point.getY());
}
path.closePath();
return path;
}
其中。points是一系列坐标点的集合。我们可以用
Point2D.Double point= new Point2D.Double(x,y);生成新的坐标点
moveTo方法:通过移动到指定的坐标在路径中添加点
直接用
lineTo:通过绘制一条从当前坐标到新指定坐标的直线在路径中添加点。
这样每加一个点。我们就用上一个点到这个点画条直线。这样就成功的画好了一个面了。
然后,我们判读点是否在该面中
GeneralPath类的contains方法就可以了。真的非常方便
java-判断点是否在面内
最新推荐文章于 2023-01-09 17:30:37 发布