0
我在學習遞歸。無法分解練習來繪製遞歸樹 - 在基本案例下面的代碼中包含函數window.drawPolarLine(branchbase,length,angle),它返回剛繪製的分支末尾的GPoint(x和y)。任何遞歸解決方案都必須跟蹤大量的GPoint。如果這應該發生在堆棧幀中,或者我應該使用矢量來存儲它們,我無法解決。使用矢量似乎正在走向迭代解決方案的道路?C++中的遞歸分解
無論如何,這是迄今爲止我非常雜亂的代碼;
/**
* File: trees.cpp
* ---------------
* Draws a recursive tree as specified in the Assignment 3 handout.
*/
#include <string> // for string
#include <iostream> // for cout, endl
using namespace std;
#include "console.h" // required of all CS106 C++ programs
#include "gwindow.h" // for GWindow class and its setTitle, setColor, and drawPolarLine methods
#include "gtypes.h" // for GPoint class
#include "random.h" // for randomChance function
const static double kWindowWidth = 600;
const static double kWindowHeight = 600;
const static string kWindowTitle = "Recursive Trees";
const static double kTrunkLength = kWindowHeight/4;
const static double kShrinkFactor = 0.70;
const static int kBranchAngleSeparation = 15;
const static int kTrunkStartAngle = 90;
const static string kLeafColor = "#2e8b57";
const static string kTrunkColor = "#8b7765";
const static double kBranchProbability = 1.0;
static GPoint drawTree(GWindow& window, int order, GPoint branchBase, double length, int angle, Vector<GPoint>& branches);
const static int kHighestOrder = 5;
int main() {
GWindow window(kWindowWidth, kWindowHeight);
window.setWindowTitle(kWindowTitle);
cout << "Repeatedly click the mouse in the graphics window to draw " << endl;
cout << "recursive trees of higher and higher order." << endl;
GPoint trunkBase(window.getWidth()/2, window.getHeight());
Vector<GPoint> branches;
for (int order = 0; order <= kHighestOrder; order++) {
waitForClick();
window.clear();
drawTree(window, order, trunkBase, kTrunkLength, kTrunkStartAngle, branches);
}
cout << endl;
cout << "All trees through order " << kHighestOrder << " have been drawn." << endl;
cout << "Click the mouse anywhere in the graphics window to quit." << endl;
waitForClick();
return 0;
}
static GPoint drawTree(GWindow& window, int order, GPoint branchbase, double length, int angle, Vector<GPoint>& branches) {
if (order == 0) {
GPoint base = window.drawPolarLine(branchbase, length, angle);
branches.add(base);
return branchbase;
}
window.setColor(order < 2 ? kLeafColor : kTrunkColor);
branchbase = branches.get(order - 1);
drawTree(window, order - 1, branchbase, length * kShrinkFactor, angle - 45, branches);
drawTree(window, order - 1, branchbase, length * kShrinkFactor, angle - 30, branches);
drawTree(window, order - 1, branchbase, length * kShrinkFactor, angle - 15, branches);
drawTree(window, order - 1, branchbase, length * kShrinkFactor, angle, branches);
drawTree(window, order - 1, branchbase, length * kShrinkFactor, angle + 15, branches);
drawTree(window, order - 1, branchbase, length * kShrinkFactor, angle + 30, branches);
drawTree(window, order - 1, branchbase, length * kShrinkFactor, angle + 45, branches);
return drawTree(window, order - 1, branchbase, length * kShrinkFactor, angle, branches);
}
// update this function to wrap around another version of drawTree, which
// recursively draws the tree of the specified order....
非常感謝巴特,我來試試這個... –
有趣。這會在第一次點擊時拋出整棵樹,然後在每次連續點擊時從外部樹葉到樹幹分階段移除樹。但它與運動所需要的非常接近 - 即從樹幹開始分階段地繪製樹,走出樹葉。 –