Sierpinski curve: Difference between revisions
Content added Content deleted
Alpha bravo (talk | contribs) (Added AutoHotkey) |
m (Refactored C++ code) |
||
Line 173: | Line 173: | ||
#include <string> |
#include <string> |
||
class sierpinski_curve { |
|||
⚫ | |||
public: |
|||
void write(std::ostream& out, int size, double x, double y, |
|||
⚫ | |||
private: |
|||
⚫ | |||
void line(std::ostream& out); |
|||
void execute(std::ostream& out, const std::string& s); |
|||
double x_; |
|||
double y_; |
|||
int angle_; |
|||
int length_; |
|||
}; |
|||
void sierpinski_curve::write(std::ostream& out, int size, double x, double y, |
|||
int length, int order) { |
|||
length_ = length; |
|||
x_ = x; |
|||
y_ = y; |
|||
angle_ = 45; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
std::string sierpinski_curve::rewrite(const std::string& s) { |
|||
std::string t; |
std::string t; |
||
for (char c : s) { |
for (char c : s) { |
||
Line 184: | Line 215: | ||
} |
} |
||
void line(std::ostream& out |
void sierpinski_curve::line(std::ostream& out) { |
||
double theta = (3.14159265359 * angle_)/180.0; |
|||
x_ += length_ * std::cos(theta); |
|||
y_ -= length_ * std::sin(theta); |
|||
out << " L" << x_ << ',' << y_; |
|||
out << 'L' << x << ',' << y << '\n'; |
|||
} |
} |
||
void execute(std::ostream& out, const std::string& s |
void sierpinski_curve::execute(std::ostream& out, const std::string& s) { |
||
out << 'M' << x_ << ',' << y_; |
|||
out << 'M' << x << ',' << y << '\n'; |
|||
for (char c : s) { |
for (char c : s) { |
||
switch (c) { |
|||
case 'F': |
|||
⚫ | |||
case 'G': |
|||
line(out); |
|||
break; |
|||
case '+': |
|||
angle_ = (angle_ + 45) % 360; |
|||
break; |
|||
case '-': |
|||
angle_ = (angle_ - 45) % 360; |
|||
break; |
|||
} |
|||
} |
} |
||
} |
} |
||
int main() { |
int main() { |
||
const int size = 545; |
|||
const int order = 5; |
|||
const double x = 5, y = 10, length = 7; |
|||
std::ofstream out("sierpinski_curve.svg"); |
std::ofstream out("sierpinski_curve.svg"); |
||
if (!out) { |
if (!out) { |
||
Line 214: | Line 246: | ||
return 1; |
return 1; |
||
} |
} |
||
sierpinski_curve s; |
|||
⚫ | |||
s.write(out, 545, 5, 10, 7, 5); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
return 0; |
return 0; |
||
}</lang> |
}</lang> |
||
{{out}} |
{{out}} |
||
See: [https://slack-files.com/T0CNUL56D- |
See: [https://slack-files.com/T0CNUL56D-F01GBK1GF2B-6c7c86c4e7 sierpinski_curve.svg] (offsite SVG image) |
||
=={{header|Factor}}== |
=={{header|Factor}}== |