0

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?

4
  • 5
    What's the desired output, what do you get instead? Commented Nov 18, 2023 at 17:36
  • @cafce25 the desired output is shown here link The idea is to always split a straight line into 2 other ones so as to form a right-angled isosceles triangle. What I get is nothing like the desired output for m greater than 1. Commented Nov 19, 2023 at 9:49
  • 2
    Please don't answer in a comment, edit your question instead, also please add examples of both to the question itself, not only as an external link. Commented Nov 19, 2023 at 10:00
  • angle_depth_new only depends on m and not on the actual direction between (x1, y1) and (x2, y2), so it can't be correct. Commented Nov 19, 2023 at 10:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.