沿任意轴寻找拐点

Finding inflection points along arbitrary axis

我试图找到相对于给定轴(绿线)的拐点,给定一系列(蓝色)点形成路径。我们可以从下图中看到有两个拐点(粉红色的线),或者路径相对于任务轴改变方向的地方。

棘手的部分是轴的方向可能会改变,所以它必须在任何方向上工作。我的第一次尝试沿路径取2个连续的点,获取它们之间的方向,然后将其与轴的方向进行比较。

跳过一些无聊的步骤(比如记录之前的点),伪代码如下所示:

foreach point cur_point in path:
    direction = prev_point - cur_point
    (normalize direction)
    relative_direction = dir_towards_axis - direction
    (normalize relative direction)
    if the sign of either x or y has changed between relative_direction and prev_relative_direction
        # we have found an inflection point

不幸的是,这不起作用。关于我哪里出错的任何指示?

看看 https://pomax.github.io/bezierinfo/#aligning - 想法是将基线与 x 或 y 轴对齐,方法是将每个点移动 {-p1.x, -p1.y},然后旋转定义曲线的所有坐标( 假设你的曲线对仿射变换是不变的!)这样你的最后一个坐标就位于轴上(为此你使用 atan2 函数找到要旋转的角度,这几乎所有的编程语言都带有)。

轴对齐后,您可以执行任何需要完成的分析"with respect to the axis"。

为了找到需要的点,您可以检测cross product轴和当前曲线方向之间的符号变化(orientation/handedness测试)。

 axis_direction = axis_end - axis_start = B - A
 ....
 direction = cur_point - prev_point
 cross =  cross_product(direction, axis_direction) = 
          direction.x * axis_direction.y - direction.y * axis_direction.x

注意:如果您的曲线是连续的解析曲线,则可能存在闭式公式