I am trying to write a recursive function that will produce a fractal snowflake, but am not getting the correct results. For some reason it gets caught in an infinte loop, but I cannot figure out why. Any help is appreciated.

private void recursiveDraw(double x1, double y1, double x2, double y2, int orderNum, Graphics gr) {
 
if (orderNum == 0) {
gr.drawLine((int) x1, (int) y1, (int) x2, (int) y2);
} else {
 
double x4 = x1 * 2 / 3 + x2 * 1 / 3;
double y4 = y1 * 2 / 3 + y2 * 1 / 3;
double x5 = x1 * 1 / 3 + x2 * 2 / 3;
double y5 = y1 * 1 / 3 + y2 * 2 / 3;
double x6 = (x4 + x5) / 2 + (y4 - y5) * sqrt(3) / 2;
double y6 = (y4 + y5) / 2 + (x5 - x4) * sqrt(3) / 2;
 
recursiveDraw(x1, y1, x4, y4, orderNum, gr);
recursiveDraw(x4, y4, x6, y6, orderNum, gr);
recursiveDraw(x6, y6, x5, y5, orderNum, gr);
recursiveDraw(x5, y5, x2, y2, orderNum, gr);
 
}
}

Recommended Answers

All 2 Replies

where do you change orderNum in order to ever leave the recursion?

orderNum - 1

I can't believe I didn't catch that. Thanks for your help...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.