Koch curve: Difference between revisions

216 bytes removed ,  4 years ago
m
Minor edit to C++ code
(Added Rust solution)
m (Minor edit to C++ code)
Line 140:
double x0, y0, x1, y1;
size_t j = 0;
for (size_t i = 0; i + 1 < size; ++i, j += 4) {
x0 = points[i].x;
y0 = points[i].y;
Line 147:
double dy = y1 - y0;
double dx = x1 - x0;
output[j++].x = {x0, y0};
output[j++].y = {x0 + dx/3, y0 + dy/3};
output[j + 1+].x = {x0 + dx/2 - dy * sqrt3_2/3, y0 + dy/2 + dx * sqrt3_2/3};
output[j + 1+].y = {x0 + 2 * dx/3, y0 + 2 * dy/3};
output[j + 2].x = x0 + dx/2 - dy * sqrt3_2/3;
output[j + 2].y = y0 + dy/2 + dx * sqrt3_2/3;
output[j + 3].x = x0 + 2 * dx/3;
output[j + 3].y = y0 + 2 * dy/3;
}
output[j].x = {x1, y1};
output[j].y = y1;
return output;
}
Line 169 ⟶ 164:
{x + length/2, y + length * sqrt3_2},
{x + length, y},
{x, y},
};
for (int i = 0; i < iterations; ++i)
Line 180 ⟶ 175:
<< size << "' height='" << size << "'>\n";
out << "<rect width='100%' height='100%' fill='black'/>\n";
out << "<path stroke-width='1' stroke='white' fill='none' d='M ";
auto points(koch_points(size, iterations));
for (size_t i = 0, n = points.size(); i < n; ++i)
out << (i == out0 <<? "LM" : "L") << points[i].x << ',' << points[i].y << '\n';
if (n > 0) {
out << points[0].x << ',' << points[0].y << ' ';
for (size_t i = 1; i < n; ++i)
out << "L " << points[i].x << ',' << points[i].y << '\n';
}
out << "z'/>\n</svg>\n";
}
1,777

edits