找到右转弯的正确交点

问题描述 投票:0回答:1

我正在尝试计算曲线(它们的坐标),为此我有起点和终点。我需要中心,以便我可以沿正确的方向绘制曲线。现在的问题是,如何获得正确的中心?

我有两个圆的交点,这些交点之一就是正确的中心。但到底是哪一个呢?

举个例子:如果我的起点位于 (50/50),终点位于 (100, 0),则较低的交点将是向右曲线的正确交点。

    public static Vector2D calculatePoint(Vector2D[] vectors, double alpha, double startingAlpha) {
        if (startingAlpha == 0 && alpha < 180) {
            return getLower(vectors);
        }

        if (startingAlpha == 0 && alpha == 180) {
            return getLeft(vectors);
        }

        if (startingAlpha == 0 && alpha > 180) {
            return getHigher(vectors);
        }

        if (startingAlpha == 90 && alpha < 180) {
            return getHigher(vectors);
        }

        return null;

    }

变量 alpha 是新角度,startingalpha 是它已有的角度,向量是包含两个点的数组。

我做了一个startingAlpha为0*的例子。 起始alpha 可以在 0 到 360* 之间。

例如,startingAlpha 和 alpha 的简单相加也不起作用,因为在 90* 起始和 90* alpha 的情况下,会产生 180*,然后必须采用左角。然而,由于曲线不是 180* 而只是 90*,这是错误的。

java math curve
1个回答
0
投票

您可以使用三角计算来找到形成曲线的圆心。

public static Vector2D calculatePoint(Vector2D[] vectors, double alpha, double startingAlpha) {
    // Calculate the angle between the two vectors
    double angle = Math.abs(alpha - startingAlpha);

    // Calculate the direction of the curve (right or left)
    int direction = (startingAlpha + angle <= 360) ? 1 : -1;

    // Calculate the center of the curve using trigonometry
    double centerX = vectors[0].getX() + direction * vectors[0].distanceTo(vectors[1]) / Math.tan(Math.toRadians(angle / 2));
    double centerY = vectors[0].getY() - direction * vectors[0].distanceTo(vectors[1]) / Math.sin(Math.toRadians(angle / 2));

    return new Vector2D(centerX, centerY);
}
© www.soinside.com 2019 - 2024. All rights reserved.