Koch curve: Difference between revisions

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