Skip to content

Instantly share code, notes, and snippets.

/UramSpiral Secret

Created November 24, 2015 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/fd4641aff513cc1338cc to your computer and use it in GitHub Desktop.
Save anonymous/fd4641aff513cc1338cc to your computer and use it in GitHub Desktop.
# include <Siv3D.hpp>
void Main()
{
//ウィンドウの縦横幅
int height = Window::Height();
int width = Window::Width();
//螺旋の大きさは最小で表示したとき1ピクセルが一つの四角になるようにする
int size;
if (height > width)size = height;
else size = width;
//素数かどうかを保存する配列を確保
//ただし、isprime[0]は一つ次の1が素数であるかどうかを表す
std::vector<bool> isprime(size * size);
//エラトステネスの篩
for (int i = 0; i < size * size; i++)isprime[i] = true;
for (int i = 2; i < size * size + 1; i++)
{
if (!isprime[i - 1])continue;
for (int a = i * 2; a < size * size; a += i)
{
isprime[a - 1] = false;
}
}
//螺旋を描画するイメージ
Image spiralimage(size, size, Palette::White);
//1右へ進む1上へ進む2左へ進む2下に進む3右に進むというように繰り返しながら螺旋を描画していく。
//現在位置
int x;
int y;
if (size & 1)//sizeが奇数なら
{
x = (size - 1) / 2;
y = (size - 1) / 2;
}
else
{
x = size / 2 - 1;
y = size / 2;
}
int a = 0;
for (int i = 1; i < size; i += 2)
{
for (int j = 0; j < i; j++)
{
if (isprime[a])spiralimage[y][x] = Palette::Black;
a++;
x++;
if (a >= size * size)break;
}
if (a >= size * size)break;
for (int j = 0; j < i; j++)
{
if (isprime[a])spiralimage[y][x] = Palette::Black;
a++;
y--;
if (a >= size * size)break;
}
if (a >= size * size)break;
for (int j = 0; j < i + 1; j++)
{
if (isprime[a])spiralimage[y][x] = Palette::Black;
a++;
x--;
if (a >= size * size)break;
}
if (a >= size * size)break;
for (int j = 0; j < i + 1; j++)
{
if (isprime[a])spiralimage[y][x] = Palette::Black;
a++;
y++;
if (a >= size * size)break;
}
if (a >= size * size)break;
}
Image Buffer(Window::Size(),Palette::White);
DynamicTexture texture(Buffer);
//倍率
float scale = 1;
//ウィンドウを表示
while (System::Update())
{
const int wheelY = Mouse::Wheel();
if (wheelY < 0)
{
scale *= 1.1;
}
if (wheelY > 0)
{
scale /= 1.1;
if (scale < 1)scale = 1;
}
//spiralimageを拡大したものをBufferに描画
for (int x = 0; x < Buffer.width; x++)
{
for (int y = 0; y < Buffer.height; y++)
{
Buffer[y][x] = spiralimage[(y - Buffer.height / 2) / scale + spiralimage.height / 2][(int)((x - Buffer.width / 2) / scale + spiralimage.width / 2)];
}
}
texture.fill(Buffer);
texture.draw();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment