void buildH(GWindow& gw, int lengthCnt, int requiredOrder, GPoint& centerPt, double length, double angle){ if(requiredOrder >= 0){ lengthCnt++; int koef = lengthCnt%2; if(koef){ length = length / 2; }else{requiredOrder--;} GPoint pt1 = gw.drawPolarLine(centerPt, length, angle); GPoint pt2 = gw.drawPolarLine(centerPt, length, (180 + angle)); /* Left brunch */ buildH(gw, lengthCnt, requiredOrder, pt1, (length), (angle + 90)); /* Right brunch */ buildH(gw, lengthCnt, requiredOrder, pt2, (length), (angle - 90)); } }
GPoint drawFractalTree(GWindow & gw, GPoint pt, double size, double theta, int order){ GPoint ptMid = gw.drawPolarLine (pt, size, theta); if(order == 0){ return gw.drawPolarLine (pt, size, theta); }else{ double angle = 20; //double angle = randomReal (10,60); // use it for more fun drawFractalTree (gw, ptMid, size * 0.7, theta - angle, order - 1); drawFractalTree (gw, ptMid, size * 0.7, theta + angle, order - 1); return drawFractalTree (gw, pt, size, theta, order - 1 ); } }
//builds two lines from each of two gotten vertex and calls itself recursively until the depth of recursion becomes zero void drawPythagoreanTree(GWindow &gw, GPoint endPoint1, GPoint endPoint2, double input_length, double theta, int recursion_depth){ double theta_one = theta - THETA; double theta_two = theta + THETA; double length = input_length * cos(THETA_RADIAN); GPoint branch1_1 = gw.drawPolarLine(endPoint1, length*2, theta_one); GPoint branch1_2 = gw.drawPolarLine(endPoint2, length, theta_one); gw.setColor("YELLOW"); gw.drawLine(branch1_1, branch1_2); GPoint branch2_1 = gw.drawPolarLine(endPoint1, length, theta_two); GPoint branch2_2 = gw.drawPolarLine(endPoint2, length*2, theta_two); gw.setColor("GREEN"); gw.drawLine(branch2_1, branch2_2); recursion_depth--; if(recursion_depth == 0){ return; } drawPythagoreanTree(gw, branch1_1, branch1_2, length, theta_one, recursion_depth); drawPythagoreanTree(gw, branch2_1, branch2_2, length, theta_two, recursion_depth); }
static void drawTree(GWindow& window, int order, GPoint startPoint, int startAngle, double len) { if (order == 0) { window.setColor(kLeafColor); window.drawPolarLine(startPoint, len, startAngle); } else { double nextLen = len * kShrinkFactor; window.setColor(order < 2 ? kLeafColor : kTrunkColor); GPoint trunkEnd = window.drawPolarLine(startPoint, len, startAngle); for (int i = 0; i < 7; ++i) { if (randomReal(0.0, 1.0) < 0.8) { int angle = startAngle - 45 + i * kBranchAngleSeparation; drawTree(window, order - 1, trunkEnd, angle, nextLen); } } } }
static GPoint drawSierpinskiTriangle (int edgeLength, int fractalOrder, double cx, double cy, GWindow &window){ // BASE CASE: ORDER = 0 if (fractalOrder==0){ int outlineTriangleHeight = sqrt(edgeLength*edgeLength - (edgeLength/2)*(edgeLength/2)); GPoint pt0 (cx - edgeLength/2, cy + outlineTriangleHeight/2); pt0 = window.drawPolarLine(pt0, edgeLength, 0); pt0 = window.drawPolarLine(pt0, edgeLength, +120); pt0 = window.drawPolarLine(pt0, edgeLength, -120); return pt0; } // RECURSIVE STEP edgeLength = edgeLength/2; int triangleHeight = sqrt(edgeLength*edgeLength - (edgeLength/2)*(edgeLength/2)); // Three bottom left points for three inner triangles GPoint pt1 (cx - edgeLength, cy + triangleHeight); GPoint pt2 (cx, cy + triangleHeight); GPoint pt3 (cx - edgeLength/2, cy); // Draw bottom left triangle pt1 = window.drawPolarLine(pt1, edgeLength, 0); pt1 = window.drawPolarLine(pt1, edgeLength, +120); pt1 = window.drawPolarLine(pt1, edgeLength, -120); int bottomLeftCentreXDimension = cx-edgeLength/2; int bottomLeftCentreYDimension = cy+triangleHeight/2; drawSierpinskiTriangle (edgeLength, fractalOrder-1, bottomLeftCentreXDimension, bottomLeftCentreYDimension, window); // Draw bottom right triangle pt2 = window.drawPolarLine(pt2, edgeLength, 0); pt2 = window.drawPolarLine(pt2, edgeLength, +120); pt2 = window.drawPolarLine(pt2, edgeLength, -120); int bottomRightCentreXDimension = cx+edgeLength/2; int bottomRightCentreYDimension = cy+triangleHeight/2; drawSierpinskiTriangle (edgeLength, fractalOrder-1, bottomRightCentreXDimension, bottomRightCentreYDimension, window); // Draw top triangle pt3 = window.drawPolarLine(pt3, edgeLength, 0); pt3 = window.drawPolarLine(pt3, edgeLength, +120); pt3 = window.drawPolarLine(pt3, edgeLength, -120); int topXDimension = cx; int topYDimention = cy - triangleHeight/2; drawSierpinskiTriangle (edgeLength, fractalOrder-1, topXDimension, topYDimention, window); return pt1; }