views:

62

answers:

1

I have information on paths I would like to draw. The information consists of a sequence of straight sections and curves. For straight sections, I have only the length. For curves, I have the radius, direction and angle. Basically, I have a turtle that can move straight or move in a circular arc from the current position (after which moving straight will be in a different direction).

I would like some way to draw these paths with the following conditions:

  1. Minimal (preferably no) trigonometry.
  2. Ability to center on a canvas and scale to fit any arbitrary size.

From what I can tell, GDI+ gives me number 2, Cairo gives me number 1, but neither one makes it particularly easy to get both. I'm open to suggestions of how to make GDI+ or Cairo (preferably pycairo) work, and I'm also open to any other library (preferably C# or Python).

I'm even open to abstract mathematical explanations of how this would be done that I can convert into code.

+2  A: 

For 2D motion, the state is [x, y, a]. Where the angle a is relative to the positive x-axis. Assuming initial state of [0, 0, 0]. 2 routines are needed to update the state according to each type of motion. Each path yields a new state, so the coordinates can be used to configure the canvas accordingly. The routines should be something like:

//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;
}
ahmadabdolkader
This is definitely helpful. I just need to figure out now how to take the state before and after following a circle and convert that into the arguments for some graphics library.
Daniel Straight