Koch curve: Difference between revisions
Content added Content deleted
m (Simplified code) |
m (Renamed constant) |
||
Line 128: | Line 128: | ||
#include <vector> |
#include <vector> |
||
constexpr double |
constexpr double sqrt3_2 = 0.86602540378444; // sqrt(3)/2 |
||
struct point { |
struct point { |
||
Line 151: | Line 151: | ||
output[j + 1].x = x0 + dx/3; |
output[j + 1].x = x0 + dx/3; |
||
output[j + 1].y = y0 + dy/3; |
output[j + 1].y = y0 + dy/3; |
||
output[j + 2].x = x0 + dx/2 - dy * |
output[j + 2].x = x0 + dx/2 - dy * sqrt3_2/3; |
||
output[j + 2].y = y0 + dy/2 + dx * |
output[j + 2].y = y0 + dy/2 + dx * sqrt3_2/3; |
||
output[j + 3].x = x0 + 2 * dx/3; |
output[j + 3].x = x0 + 2 * dx/3; |
||
output[j + 3].y = y0 + 2 * dy/3; |
output[j + 3].y = y0 + 2 * dy/3; |
||
Line 162: | Line 162: | ||
std::vector<point> koch_points(int size, int iterations) { |
std::vector<point> koch_points(int size, int iterations) { |
||
double length = size * |
double length = size * sqrt3_2 * 0.95; |
||
double x = (size - length)/2; |
double x = (size - length)/2; |
||
double y = size/2 - length * |
double y = size/2 - length * sqrt3_2/3; |
||
std::vector<point> points{ |
std::vector<point> points{ |
||
{x, y}, |
{x, y}, |
||
{x + length/2, y + length * |
{x + length/2, y + length * sqrt3_2}, |
||
{x + length, y}, |
{x + length, y}, |
||
{x, y}, |
{x, y}, |