绘制行进路径的最佳方法

Best way to draw a path traveled

我正在制作一个基于 GPS 坐标来跟踪车辆的应用程序。

我创建了一个SurfaceView给他画田地、车辆和路径(路线)

结果如下所示:

黑色圆点代表GPS坐标即将到来,蓝色矩形代表行进路径所覆盖的区域。 (路径宽度可配置)

我用蓝色矩形绘制的方式(这是我的问题),蓝色矩形是行进路径所覆盖的区域。 (路径宽度可配置)

因此我需要克服一些情况。

以后我需要:

我想要一些绘制这条路径的技巧。

我的代码:

public void draw(Canvas canvas) {
    Log.d(getClass().getSimpleName(), "draw");
    canvas.save();

    // translate canvas to vehicle positon
    canvas.translate((float) center.cartesian(0), (float) center.cartesian(1));

    float fieldRotation = 0;

    if (trackerHistory.size() > 1) {
         /*
        Before drawing the way, only takes the last position and finds the angle of rotation of the field.
         */
        Vector lastPosition = new Vector(convertToTerrainCoordinates(lastPoint));
        Vector preLastPosition = new Vector(convertToTerrainCoordinates(preLastPoint));
        float shift = (float) lastPosition.distanceTo(preLastPosition);

        /*
        Having the last coordinate as a triangle, 'preLastCoord' saves the values of the legs, while 'shift' is the hypotenuse
        */
        // If the Y offset is negative, then the opposite side is the Y displacement
        if (preLastPosition.cartesian(1) < 0) {
            // dividing the opposite side by hipetenusa, we have the sine of the angle that must be rotated.
            double sin = preLastPosition.cartesian(1) / shift;

            // when Y is negative, it is necessary to add or subtract 90 degrees depending on the value of X
            // The "Math.asin()" calculates the radian arc to the sine previously calculated.
            // And the "Math.toDegress()" converts degrees to radians from 0 to 360.
            if (preLastPosition.cartesian(0) < 0) {
                fieldRotation = (float) (Math.toDegrees(Math.asin(sin)) - 90d);
            } else {
                fieldRotation = (float) (Math.abs(Math.toDegrees(Math.asin(sin))) + 90d);
            }
        }
        // if not, the opposite side is the X offset
        else {
            // dividing the opposite side by hipetenusa have the sine of the angle that must be rotated.
            double senAngulo = preLastPosition.cartesian(0) / shift;

            // The "Math.asin()" calculates the radian arc to the sine previously calculated.
            // And the "Math.toDegress()" converts degrees to radians from 0 to 360.
            fieldRotation = (float) Math.toDegrees(Math.asin(senAngulo));
        }
    }

    final float dpiTrackerWidth = Navigator.meterToDpi(trackerWidth); // width of rect

    final Path positionHistory = new Path(); // to draw the route
    final Path circle = new Path(); // to draw the positions

    /*
    Iterate the historical positions and draw the path
    */
    for (int i = 1; i < trackerHistory.size(); i++) {
        Vector currentPosition = new Vector(convertToTerrainCoordinates(trackerHistory.get(i))); // vector with X and Y position
        Vector lastPosition = new Vector(convertToTerrainCoordinates(trackerHistory.get(i - 1))); // vector with X and Y position

        circle.addCircle((float) currentPosition.cartesian(0), (float) currentPosition.cartesian(1), 3, Path.Direction.CW);
        circle.addCircle((float) lastPosition.cartesian(0), (float) lastPosition.cartesian(1), 3, Path.Direction.CW);

        if (isInsideOfScreen(currentPosition.cartesian(0), currentPosition.cartesian(1)) ||
                isInsideOfScreen(lastPosition.cartesian(0), lastPosition.cartesian(1))) {
            /*
            Calcule degree by triangle sides
             */
            float shift = (float) currentPosition.distanceTo(lastPosition);
            Vector dif = lastPosition.minus(currentPosition);
            float sin = (float) (dif.cartesian(0) / shift);

            float degress = (float) Math.toDegrees(Math.asin(sin));

            /*
            Create a Rect to draw displacement between two coordinates
             */
            RectF rect = new RectF();
            rect.left = (float) (currentPosition.cartesian(0) - (dpiTrackerWidth / 2));
            rect.right = rect.left + dpiTrackerWidth;
            rect.top = (float) currentPosition.cartesian(1);
            rect.bottom = rect.top - shift;

            Path p = new Path();
            Matrix m = new Matrix();
            p.addRect(rect, Path.Direction.CCW);
            m.postRotate(-degress, (float) currentPosition.cartesian(0), (float) currentPosition.cartesian(1));
            p.transform(m);

            positionHistory.addPath(p);
        }
    }

    // rotates the map to make the route down.
    canvas.rotate(fieldRotation);

    canvas.drawPath(positionHistory, paint);
    canvas.drawPath(circle, paint2);

    canvas.restore();
}

我的目标是拥有类似这个应用程序的东西:https://play.google.com/store/apps/details?id=hu.zbertok.machineryguide(但目前只有 2D)

编辑:

进一步澄清我的疑惑:

通常这样的路径是使用图形库中的 "path" 方法绘制的。 在该库中,您可以创建折线,并给出线宽。 您进一步指定角的填充方式。 (BEVEL_JOIN, MITTER_JOIN)

主要问题是路径是边走边画还是后记。 后记是没有问题的。 驾驶时绘制可能有点棘手,以避免每秒重新绘制路径。

当使用带 moveTo 和 lineTo 的 Path 创建多段线时,您可以设置线宽,图形库会为您完成这一切。 那么就没有空隙了,因为是折线

经过一段时间的研究,我得出了一个成功的结果。我将评论我的想法以及解决方案。

正如我在问题中解释的那样,沿途我有车辆行驶的坐标,还应该绘制路径宽度的设置。

使用LibGDX库已经准备好一些功能,例如"orthographic camera"实现定位、旋转等

使用 LibGDX 我将我这边的 GPS 坐标转换为行驶的道路。像这样:

下一个挑战是填补走过的路。首先我尝试使用矩形,但结果如我的问题所示。

所以解决方案是使用路径的边作为顶点来追踪三角形。像这样:

然后简单地填写三角形。像这样:

最后,我使用 Stencil 设置了 OpenGL 以突出显示重叠部分。像这样:

已解决的其他问题:

  • 要计算覆盖面积,只需计算路径上现有三角形的面积即可。
  • 要检测重叠,只需检查车辆的当前位置是否在三角形内。

感谢:

  • AlexWien 的关注和他们的时间。
  • 康纳·安德森 videos of LibGDX
  • 特别感谢 Luis Eduardo 提供的知识,对我帮助很大。

sample source code.