Skip to content

Instantly share code, notes, and snippets.

@tkoki
Last active July 6, 2020 07:10
Show Gist options
  • Save tkoki/9c0623793f419dd3a98f1e428646aae2 to your computer and use it in GitHub Desktop.
Save tkoki/9c0623793f419dd3a98f1e428646aae2 to your computer and use it in GitHub Desktop.
三角関数を使わない円のペイント
# include <Siv3D.hpp>
void PaintCircle(int cx, int cy, int r, Color c) {
int x, y;
x = r;
y = 0;
while (x >= y) {
Line(cx + x, cy + y, cx + x, cy - y).draw(1, c);
Line(cx - x, cy + y, cx - x, cy - y).draw(1, c);
Line(cx + y, cy + x, cx + y, cy - x).draw(1, c);
Line(cx - y, cy + x, cx - y, cy - x).draw(1, c);
if ((r -= (y++ << 1) - 1) < 0) {
r += (x-- - 1) << 1;
}
}
}
bool PaintCircleStep(int cx, int cy, int *r, int *x, int *y, Color c) {
Line(cx + *x, cy + *y, cx + *x, cy - *y).draw(1, c);
Line(cx - *x, cy + *y, cx - *x, cy - *y).draw(1, c);
Line(cx + *y, cy + *x, cx + *y, cy - *x).draw(1, c);
Line(cx - *y, cy + *x, cx - *y, cy - *x).draw(1, c);
if (((*r) -= ((*y)++ << 1) - 1) < 0) {
(*r) += ((*x)-- - 1) << 1;
}
return *x >= *y ? false : true;
}
void Main() {
const Font font(60);
const int cx = Scene::Width() / 2;
const int cy = Scene::Height() / 2;
const int R = 160;
int x, y, r;
Window::SetTitle(U"三角関数を使わない円のペイント");
Scene::SetBackground(Color(30, 30, 30));
x = r = R;
y = 0;
while (System::Update()) {
Line(0, cy, Scene::Width(), cy).draw(1, Palette::White);
Line(cx, 0, cx, Scene::Height()).draw(1, Palette::White);
PaintCircle(cx, cy, R, Color(10, 80, 220, 50));
if (PaintCircleStep(cx, cy, &r, &x, &y, Palette::Yellow)) {
x = r = R;
y = 0;
}
ClearPrint();
Print << U"({}, {}, {})"_fmt(x, y, r);
}
}
@tkoki
Copy link
Author

tkoki commented Jul 6, 2020

実行するにはSiv3Dをインストールする必要があります。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment