図形を描くプログラムをちょっと紹介してみる

こんにちは、ココネでクライアントエンジニアをしているMです。

最近少し調べていたこともあって、今回は図形を描くプログラムを3つほど紹介してみたいと思います。

実際にUnity上でエフェクトを図形にそって生成してみましたので、合わせてご覧ください。

実践

※ 以降コード内に出てくるTWO_PIMathf.PI * 2(2π)となります

星型

float step = 0.1f;  // 角度のステップ幅
float scale = 1; // サイズ

for (float angle = 0; angle < TWO_PI; angle += step)
{
	float x = scale * Mathf.Pow(Mathf.Cos(angle), 3);
	float y = scale * Mathf.Pow(Mathf.Sin(angle), 3);

	Instantiate(object, new Vector2(x, y), Quaternion.identity);
}

試してみた

クローバー型

float step = 0.1f;  // 角度のステップ幅
float scale = 1; // サイズ
int num = 4; // クローバーの数

for (float theta = 0; theta <= TWO_PI; theta += step)
{
	float r = 1 + Mathf.Cos(num * theta);
	float x = r * scale * Mathf.Cos(theta);
	float y = r * scale * Mathf.Sin(theta);
	Instantiate(object, new Vector2(x, y), Quaternion.identity);
}

クローバーは簡単に葉の枚数も自在に変更できます。

試してみた

葉の枚数をかえてみた

ハート型

float step = 0.1f;  // 角度のステップ幅
int scale = 1; // サイズ

for (float theta = 0; theta <= TWO_PI; theta += step)
{
	float r = 1 - Mathf.Sin(theta);
	float x = r * Mathf.Cos(theta);
	float y = r * Mathf.Sin(theta);
	Instantiate(object, new Vector2(x*scale, y*scale), Quaternion.identity);
}

試してみた

おわりに

いかがでしたでしょうか、

最後に紹介したハート型は少し歪な形をしていますが、他にも色々な描き方があります。

下記に参考にしたサイトを貼っておくので、ぜひ他の方法も試していただけたらと思います。

参考

https://nazesuugaku.com/math_graph_conclude/

https://mathworld.wolfram.com/HeartCurve.html

 

ココネでは一緒に働く仲間を募集中です。

ご興味のある方は、ぜひこちらのエンジニア採用サイトをご覧ください。

→ココネ株式会社エンジニアの求人一覧

 

Category

Tag

%d