0

在我的MapView我可以点击地图以创建任意数量的点,并且这些点通过一条线连接,但我不想像这样在地图上绘制任何可能与另一条线相交的线

在此处输入图像描述

无论如何要获取线的坐标来检查新线是否会与其他线相交?

或者,如果其他人对如何检查这一点有任何建议,那也很棒

这是我绘制线条的绘制方法

@Override
    public void draw(final Canvas canvas, final MapView mapView, final boolean shadow){
        super.draw(canvas, mapView, shadow);

        try{
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setColor(Color.RED);
        paint.setStrokeWidth(3);
        paint.setAlpha(100);
        paint.setStyle(Style.STROKE);

        Path path = new Path();
        Point point1 = null;
        boolean runOnce = false;
        for(int i = 0;i<items.size();i++){
            Point point = new Point();
            mapView.getProjection().toPixels(items.get(i).mGeoPoint, point);

            if(!runOnce)
            {
                path.moveTo(point.x, point.y);
                point1 = new Point(point.x, point.y);
                runOnce = true;
            }
            else
            {
                path.lineTo(point.x, point.y);
            }
        }
        path.lineTo(point1.x, point1.y);
        canvas.drawPath(path, paint);

        path.lineTo(point1.x, point1.y);
        canvas.drawPath(path, paint);

        Paint paint2 = new Paint();
        paint2.setAntiAlias(true);
        paint2.setDither(true);
        paint2.setColor(Color.RED);
        paint2.setStrokeWidth(3);
        paint2.setAlpha(20);
        paint2.setStyle(Style.FILL);

        Path path2 = new Path();
        Point point2 = null;
        boolean runOnce2 = false;
        for(int i = 0; i < items.size(); i++)
        {
            Point point = new Point();
            mapView.getProjection().toPixels(items.get(i).mGeoPoint, point);

            if(!runOnce2)
            {
                path2.moveTo(point.x, point.y);
                point2 = new Point(point.x, point.y);
                runOnce2 = true;
            }
            else
            {
                path2.lineTo(point.x, point.y);
            }
        }

        path2.lineTo(point2.x, point2.y);
        canvas.drawPath(path2, paint2);
        }catch(Exception e){

        }
    }
4

1 回答 1

1

你去:-)

paulbourke.net/geometry/lineline2d

告诉你如何用数学方法确定两条线段的交点。

如果您不希望您的端点算作交叉点,那么只需先检查您的端点是否重叠。这可以使用如下的 if 语句来解决:

if(Math.abs(x1 - x2) < EPSILON && Math.abs(y1 - y2) < EPSILON)

其中 EPSILON 是您的容错值,例如 0.001。

于 2012-07-10T14:57:48.097 回答