Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
//"Siv3D January 2016"を使用
# include <Siv3D.hpp>
using namespace std;
#define M 5 //ループ回数
#define P int(pow(4, M - i -1))
#define Q int(pow(4, M - i))
#define P0 j * Q
#define P1 j * Q + 1 * P
#define P2 j * Q + 2 * P
#define P3 j * Q + 3 * P
#define P4 j * Q + 4 * P
void Main()
{
//配列の要素数
int n = 2;
for (int i = 0; i < M; i++) {
n = n + 3 * int(pow(4, i));//x(1)=2, x[n]=x[n-1]+ 3 * 4^(n - 2)の漸化式でn=M+1の時のx[n]の値
}
//動的配列クラステンプレート
vector<double> x(n);
vector<double> y(n);
//60度のラジアン
double const rad = 60. * Pi / 180.;
//左端と右端の座標の設定
x[0] = 60.;
y[0] = 200.;
x[n - 1] = 570.;
y[n - 1] = 200.;
//ループ
for (int i = 0; i < M; i++) {
//この時点での直線の数だけ処理を繰り返す
for (int j = 0; j < pow(4, i); j++) {
//三等分
x[P1] = (2. * x[P0] + x[P4]) / 3;
y[P1] = (2. * y[P0] + y[P4]) / 3;
x[P3] = (x[P0] + 2 * x[P4]) / 3;
y[P3] = (y[P0] + 2 * y[P4]) / 3;
//回転行列:上で求めた2点のうち、1点を原点と見立てて60度回転
x[P2] = (x[P3] - x[P1]) * cos(rad) - (y[P3] - y[P1]) * sin(rad) + x[P1];
y[P2] = (x[P3] - x[P1]) * sin(rad) + (y[P3] - y[P1]) * cos(rad) + y[P1];
}
}
// 線を作成
LineString lines(n, Vec2(0, 0));
for (int i = 0; i < n; i++)
{
lines.point(i).set(x[i], y[i]);
}
while (System::Update())
{
lines.draw(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment