Skip to content

Instantly share code, notes, and snippets.

/astroid.cpp Secret

Created November 29, 2015 13:35
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/8ba5bd2a87d888422824 to your computer and use it in GitHub Desktop.
Save anonymous/8ba5bd2a87d888422824 to your computer and use it in GitHub Desktop.
# include <Siv3D.hpp>
void drawline(double, double, double, double);
void Main()
{
double theta = 0; //媒介変数
double x = 0; //x座標
double y = 0; //y座標
double ox = 0; //直前のx
double oy = 0; //直前のy
double r1 = 200; //外側の円の半径
double cx = Window::Width() / 2 + 50; //中央の座標
double cy = Window::Height() / 2;
double cx2 = 0; //内側の円の中心
double cy2 = 0;
int frm = 0; //現在のフレーム数
//GUI関連
GUI gui(GUIStyle::Default);
gui.addln(L"display", GUICheckBox::Create({ L"座標軸", L"直線", L"円(内側)", L"円(外側)", L"アステロイド" , L"直線(軌跡)"}, {0,2,3,4}, true));
//アステロイドを描くイメージ
Image image(Window::Size());
DynamicTexture tex(image);
//直線の軌跡を描くイメージ
Image track(Window::Size());
DynamicTexture tracktex(track);
bool oastroid = true;
bool otrack = false;
while (System::Update())
{
ox = x;
oy = y;
cx2 = cx + Math::Cos(theta) * (r1 * 3 / 4);
cy2 = cy - Math::Sin(theta) * (r1 * 3 / 4);
x =cx + r1 * Math::Pow(Math::Cos(theta),3);
y =cy - r1 * Math::Pow(Math::Sin(theta), 3);
theta = theta + 0.01;
//座標軸
if (gui.checkBox(L"display").checked(0))
{
Line(cx - r1 * 1.2, cy, cx + r1 * 1.2, cy).drawArrow();
Line(cx, cy + r1 * 1.2, cx, cy - r1 * 1.2).drawArrow();
}
//直線の軌跡
if (gui.checkBox(L"display").checked(5))
{
if (frm % 20 == 0)Line(cx + Math::Cos(theta) * r1, cy, cx, cy - Math::Sin(theta) * r1).overwrite(track, Palette::Blue);
tracktex.fill(track);
tracktex.draw();
otrack = true;
}
else
{
//描画を消したときにイメージをクリアする
if (otrack)track.fill(Color(0,0,0,0));
otrack = false;
}
//アステロイド
if (gui.checkBox(L"display").checked(4))
{
if (frm != 0)Line(ox, oy, x, y).overwrite(image, Palette::White);
tex.fill(image);
tex.draw();
oastroid = true;
}
else
{
//描画を消したときにイメージをクリアする
if (oastroid)image.fill(Color(0,0,0,0));
oastroid = false;
}
//外側の円
if (gui.checkBox(L"display").checked(3))Circle(cx, cy, r1).drawFrame();
//直線
if (gui.checkBox(L"display").checked(1))drawline(cx + Math::Cos(theta) * r1, cy, cx, cy - Math::Sin(theta) * r1);
//内側の円
if ((gui.checkBox(L"display").checked(2)))
{
Line(cx2, cy2, x, y).draw();
Circle(cx2, cy2, r1 / 4).drawFrame();
}
frm++;
}
}
void drawline(double x1, double y1, double x2, double y2)
{
double xstart[] = { 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9 };
double xend[] = { 1, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9 };
double ystart[] = { 0, -0.05, -0.05, -0.05, -0.05, -0.1, -0.05, -0.05, -0.05, -0.05 };
double yend[] = { 0, 0.05, 0.05, 0.05, 0.05, 0.1, 0.05, 0.05, 0.05, 0.05 };
for (int i = 0; i < 10; i++)
{
Line(xstart[i] * (x2 - x1) + ystart[i] * (y1 - y2) + x1
, xstart[i] * (y2 - y1) + ystart[i] * (x2 - x1) + y1
, xend[i] * (x2 - x1) + yend[i] * (y1 - y2) + x1
, xend[i] * (y2 - y1) + yend[i] * (x2 - x1) + y1)
.draw(Palette::White);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment