Sierpinski curve: Difference between revisions

Content added Content deleted
m (Refactored C++ code)
m (C++ - calculate initial x and y from length)
Line 175: Line 175:
class sierpinski_curve {
class sierpinski_curve {
public:
public:
void write(std::ostream& out, int size, double x, double y,
void write(std::ostream& out, int size, int length, int order);
int length, int order);
private:
private:
static std::string rewrite(const std::string& s);
static std::string rewrite(const std::string& s);
Line 187: Line 186:
};
};


void sierpinski_curve::write(std::ostream& out, int size, double x, double y,
void sierpinski_curve::write(std::ostream& out, int size, int length, int order) {
int length, int order) {
length_ = length;
length_ = length;
x_ = x;
x_ = length/std::sqrt(2.0);
y_ = y;
y_ = 2 * x_;
angle_ = 45;
angle_ = 45;
out << "<svg xmlns='http://www.w3.org/2000/svg' width='"
out << "<svg xmlns='http://www.w3.org/2000/svg' width='"
Line 247: Line 245:
}
}
sierpinski_curve s;
sierpinski_curve s;
s.write(out, 545, 5, 10, 7, 5);
s.write(out, 545, 7, 5);
return 0;
return 0;
}</lang>
}</lang>


{{out}}
{{out}}
See: [https://slack-files.com/T0CNUL56D-F01GBK1GF2B-6c7c86c4e7 sierpinski_curve.svg] (offsite SVG image)
See: [https://slack-files.com/T0CNUL56D-F01GZ1BU4RZ-99f34b7076 sierpinski_curve.svg] (offsite SVG image)


=={{header|Factor}}==
=={{header|Factor}}==