Discussion:
Q1: Write a program use C++ language with Recursion algorithm to do this exercise. Prefer compile with Visual C++ or DevC Snowflake.h and image included below:
Q2: What changes have to made in the program in snowflake.h to draw a line. Try it and experiment with other possibilities to generate other curves.
//************************ snowflake.h ***************************
// Visual C++ program
#include
double PI = 3.14159;
class vonKoch {
public:
vonKoch(int,int,CDC*);
void snowflake();
private:
double side, angle;
int level;
CPoint currPt, pt;
CDC *pen;
void right(double x) {
angle += x;
}
void left (double x) {
angle -= x;
}
void drawFourLines(double side, int level);
};
vonKoch::vonKoch(int s, int lvl, CDC *pDC) {
pen = pDC;
currPt.x = 200;
currPt.y = 100;
pen->MoveTo(currPt);
angle = 0.0;
side = s;
level = lvl;
}
void vonKoch::drawFourLines(double side, int level) {
// arguments to sin() and cos() are angles
// specified in radians, i.e., the coefficient
// PI/180 is necessary;
if (level == 0) { //
pt.x = int(cos(angle*PI/180)*side) + currPt.x;
pt.y = int(sin(angle*PI/180)*side) + currPt.y;
pen->LineTo(pt);
currPt.x = pt.x;
currPt.y = pt.y;
}
else {
drawFourLines(side/3,level-1);
left (60);
drawFourLines(side/3,level-1);
right(120);
drawFourLines(side/3,level-1);
left (60);
drawFourLines(side/3,level-1);
}
}
void vonKoch::snowflake() {
for (int i = 1; i <= 3; i++) {
drawFourLines(side,level);
right(120);
}
}
// The function OnDraw() is generated by Visual C++ when creating
// a snowflake project of type MFC AppWizard (exe), Single Document;
#include "a:snowflake.h"
void CSnowflakeView::OnDraw(CDC* pDC)
{
CSnowflakeDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
vonKoch(200,4,pDC).snowflake();
}