aheuchamps
aheuchamps

Reputation: 153

How to write recursive C code to plot a Crab Curve?

I am trying to generate a "Crab Curve" (as shown in this link) by using the following recursive C function

/* x1: x-coordinate of first point
y1: y-coordinate of first point
x2: x-coordinate of second point
y2: y-coordinate of second point
m: number of recursion steps */
void crab_curve(double x1, double y1, double x2, double y2, int m)
{
    if (m==0) // Straight line
    {
        draw_line(x1, y1, x2, y2, 1, 0, 0, 0);
    }

    else // Or continue
    {

        double l = sqrt( (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) );
        double angle_depth_new = m * 0.25 * M_PI;
        double r = (0.5 * l) / (cos(angle_depth_new));
        double x1MidNew = x1 + r*cos(angle_depth_new);
        double y1MidNew = y1 + r*sin(angle_depth_new);
        crab_curve(x1, y1, x1MidNew, y1MidNew, m-1);
        crab_curve(x1MidNew, y1MidNew, x2, y2, m-1);
    }
}

where the method "draw_line" draws a straight line between points of coordinates (x1, y1) and (x2, y2), with a linewidth of 1, and rgb colour (0, 0, 0). If the number of recursion steps m is greater than 1, I do not reach the desired outcome, and do not understand why. How can I sort my problem out?

Upvotes: 0

Views: 62

Answers (0)

Related Questions