给定一个点的角度和距离,找到坐标

Find the coordinate given the angle and distance from a point

我试图通过给定点 (Xo,Yo)、距离和角度计算最后一个点 (X,Y),在 android canvas 上画线。示意图如下:

我正在使用以下公式从磁传感器和加速度计值计算方位角方向角度数

            if (accelValues != null && magnetValues != null) {
                float rotation[] = new float[9];
                float orientation[] = new float[3];
                if (SensorManager.getRotationMatrix(rotation, null, accelValues, magnetValues)) {
                    SensorManager.getOrientation(rotation, orientation);
                    float azimuthDegree = (float) (Math.toDegrees(orientation[0]) + 360) % 360;
                    orientationDegree = Math.round(azimuthDegree);

                }
            }

我将所有行保存在一个数组中,然后调用 onDraw 重新绘制 canvas。下面是我的 onDraw 代码和计算步骤,因此它会根据用户的方向在用户采取的每个步骤中重新绘制线条。 (假设距离长度为60)

        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub
            super.onDraw(canvas);
            drawRotateImage(canvas);
            canvas.drawPoint(Position.x_axis, Position.y_axis, paint);

            for (Line l : listLine) {
                canvas.drawLine(l.StartX, l.StartY, l.EndX, l.EndY, paint);
            }

private void stepDetector () {

            l = new Line();
            l.setStartX(lastX);
            l.setStartY(lastY);
            l.setOrientationDegree(orientationDegree);
            lineX = (float) (lastX + (60 * cos(orientationDegree)));
            lineY = (float) (lastY + (60 * sin(orientationDegree)));
            l.setEndX(lineX);
            l.setEndY(lineY);
            listLine.add(l);
            System.out.println ("" + l.toString());
            invalidate();
            lastX = lineX;
            lastY = lineY;
        }

我面临的问题是线条的绘制方向不正确。无论定向方向如何,它都会朝任何方向移动。我得到如下所示的内容:

根据我的分析,考虑到方向度,线绘制不准确。我相信它与 Android 坐标系有关。如果有人可以帮助我计算任何方向 [0-360] 的精确定向度,将不胜感激。

圆轴各象限最后一点(X,Y)的计算公式是否不同?

我相信您观察到的行为是由于您的方位角以度为单位,而三角函数需要弧度。您可以使用 Math.toRadians 将度数转换回弧度。但是,我会坚持在任何地方都使用弧度并取消调用 Math.toDegrees(并且也停止四舍五入)。