本文介绍了点测试内部多边形的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一天,我在Java中做了一个类来计算,如果点(X,Y)是一个多边形内。 ( X ,因为将地理-coordinates)。

我知道,Java有类多边形,但我不得不使用 Path2D中的Point2D ,因为多边形不允许的,只是整数:(

有一次,我在 Path2D中多边形做的,我用的方法包含 Path2D中有它),我的问题得到解决。

但现在,我想导入到Android,问题就在这里,因为 Path2D中需要进口:

 进口java.awt.geom.Path2D;
进口java.awt.geom.Point2D中;
 

和Android中并不存在AWT,所以无法使用。

那么,有没有任何一类类似于 Path2D中的有包含的方法?或者我要计算由我自己?

下面是我怎么没在Java中使用 Path2D中

 私人无效ConstructPolygon(矢量<&的Point2D GT; coodinates)
{
    this.polygon.moveTo(coodinates.get(0).getX(),coodinates.get(0).getY());

    //System.out.println(coodinates.get(0).getX()++ coodinates.get(0).getY());
    //System.out.println("asda);

    的for(int i = 1; I< this.num_points;我++)
    {
        //System.out.println(coodinates.get(ⅰ).getX()++ coodinates.get(ⅰ).getY());
        this.polygon.lineTo(coodinates.get(ⅰ).getX(),coodinates.get(ⅰ).getY());
    }
    this.polygon.closePath();
}
公共布尔InsideCity(的Point2D PUNTO)
{
    返回this.polygon.contains(PUNTO);
}
 

解决方案

您可以用我的简单的库正是为了这一点:https://github.com/sromku/polygon-contains-point.

prepare多边形:

 多边形多边形= Polygon.Builder()
    .addVertex(新点(1,3))
    .addVertex(新点(2,8))
    .addVertex(新点(5,4))
    .addVertex(新点(5,9))
    .addVertex(新点(7,5))
    .addVertex(新点(6,1))
    .addVertex(新点(3,1))
    。建立();
 

和检查,而该点位于多边形内部:

 点对点=新的点(4.5F,7);
布尔包含= polygon.contains(点);
 

它与浮动类型,并与包含孔的多边形:)

The other day I did a class in Java to calculate if a point(X,Y) is inside a polygon. (X and Y are double, because will be geo-coordinates).

I know that Java has the class Polygon, but I had to use Path2D and Point2D, because Polygon don't allow double's, just integers :(

Once I have the polygon done in Path2D, I used the method contains (Path2D had it), and my problem was solved.

But now, I want to import to Android, and the problem is here, because Path2D needs to import:

import java.awt.geom.Path2D;
import java.awt.geom.Point2D;

and in Android don't exist awt, so I can't use.

So, is there any class similar to Path2D that had contains method? or I have to calculate by myself?

Here is how I did in Java using Path2D:

private void ConstructPolygon(Vector<Point2D> coodinates)
{
    this.polygon.moveTo(coodinates.get(0).getX(), coodinates.get(0).getY());

    //System.out.println(coodinates.get(0).getX() + "   " + coodinates.get(0).getY());
    //System.out.println("asda");

    for(int i = 1; i < this.num_points; i++)
    {
        //System.out.println(coodinates.get(i).getX() + "   " + coodinates.get(i).getY());
        this.polygon.lineTo(coodinates.get(i).getX(), coodinates.get(i).getY());
    }
    this.polygon.closePath();
}
public boolean InsideCity(Point2D punto)
{
    return this.polygon.contains(punto);
}
解决方案

You can use my simple library exactly for this: https://github.com/sromku/polygon-contains-point.

Prepare polygon:

Polygon polygon = Polygon.Builder()
    .addVertex(new Point(1, 3))
    .addVertex(new Point(2, 8))
    .addVertex(new Point(5, 4))
    .addVertex(new Point(5, 9))
    .addVertex(new Point(7, 5))
    .addVertex(new Point(6, 1))
    .addVertex(new Point(3, 1))
    .build();

And check whereas the point is inside the polygon:

Point point = new Point(4.5f, 7);
boolean contains = polygon.contains(point);

It works with float types and with polygons that contain holes :)

这篇关于点测试内部多边形的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 10:53