【问题标题】:How to draw paths specified in terms of straight and curved motion如何绘制根据直线和曲线运动指定的路径
【发布时间】:2010-02-19 16:12:36
【问题描述】:

我有关于我想绘制的路径的信息。该信息由一系列直线部分和曲线组成。对于直线部分,我只有长度。对于曲线,我有半径、方向和角度。基本上,我有一只乌龟,它可以从当前位置直线移动或沿圆弧移动(之后直线移动将朝不同的方向移动)。

我想通过以下方式绘制这些路径:

  1. 最小(最好没有)三角函数。
  2. 能够在画布上居中并缩放以适应任意大小。

据我所知,GDI+ 给了我第 2 名,Cairo 给了我第 1 名,但两者都不是特别容易获得的。我愿意接受有关如何使 GDI+ 或 Cairo(最好是 pycairo)工作的建议,我也愿意接受任何其他库(最好是 C# 或 Python)。

我什至愿意接受抽象的数学解释,说明如何将其转换为代码。

【问题讨论】:

    标签: language-agnostic math graphics drawing


    【解决方案1】:

    对于 2D 运动,状态为 [x, y, a]。其中角度a 相对于正x 轴。假设初始状态为[0, 0, 0]。需要 2 个例程来根据每种运动类型更新状态。每条路径都会产生一个新状态,因此可以使用坐标来相应地配置画布。例程应该是这样的:

    //by the definition of the state
    State followLine(State s, double d) {
        State s = new State();
        s.x = s0.x + d * cos(s0.a);
        s.y = s0.y + d * sin(s0.a);
        s.a = s0.a;
        return s;
    }
    
    State followCircle(State s0, double radius, double arcAngle, boolean clockwise) {
        State s1 = new State(s0);
        //look at the end point on the arc
        if(clockwise) {
            s1.a = s0.a - arcAngle / 2;
        } else {
            s1.a = s0.a + arcAngle / 2;
        }
        //move to the end point of the arc
        State s = followLine(s1, 2 * radius * sin(arcAngle/ 2));
        //fix new angle
        if(clockwise) {
            s.a = s0.a - arcAngle;
        } else {
            s.a = s0.a + arcAngle;
        }
        return s;
    }
    

    【讨论】:

    • 这绝对有帮助。我现在只需要弄清楚如何获取跟随圆圈前后的状态并将其转换为某些图形库的参数。
    猜你喜欢
    • 2018-05-24
    • 1970-01-01
    • 2019-12-31
    • 2013-05-30
    • 2011-02-26
    • 2018-01-11
    • 2021-10-29
    • 2017-04-10
    • 1970-01-01
    相关资源
    最近更新 更多