"ベクトルの正規化と点の移動"
# include <Siv3D.hpp> // OpenSiv3D v0.6.10
 
void Main()
{
	// 背景の色を設定する | Set the background color
	Scene::SetBackground(Palette::Lemonchiffon);
	Console.open();
	//1点目,2点目
	Vec2 p1, p2;
	//p1p2をつなぐベクトル
	Vec2 vp12;
	p1.x = 10;
	p1.y = 20;
	p2.x = 50;
	p2.y = 124;
	//p1->p2のベクトルは 終点の座標から、支店の座標を引く
	vp12.x = p2.x - p1.x;
	vp12.y = p2.y - p1.y;
	//         component x, componet y x成分、y成分
	//vp12 = { p2.x - p1.x, p2.y - p1.y };
	//原点を始点として、vp12を描く
	//p1とp2の距離=vp12の長さ
	//ルートはsqrt(実数) sqrt(実数変数)
	//double dist = sqrt((p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y));
	double dist = sqrt(vp12.x * vp12.x + vp12.y * vp12.y);
	//p1p2をつなぐベクトルの単位ベクトル化したもの(正規化)
	Vec2 unitv = { vp12.x / dist, vp12.y / dist };
	std::cout << unitv.length() << std::endl;
	std::cout << vp12.length() << std::endl;
 
	Vec2 radP = p1;
	while (System::Update())
	{
		//Line{ {0,0}, vp12 }.drawArrow(3, Vec2{ 10,10 }, Palette::Orange);
 
		Circle{ p1, 3 }.draw(Palette::Black);
		Circle{ p2, 3 }.draw(Palette::Red);
 
		Line{p1, p1 + vp12.length() * unitv }.drawArrow(3, Vec2{ 10,10 }, Palette::Orange);
		//p1を始点に、unitv方向に1フレームに距離2.0ずつp1を移動しよう。
		//p1から、半径4の真緑の点を移動しよう!
		Circle{ radP, 4 }.draw(Palette::Darkgreen);
		radP = radP + 0.5 * unitv;
	}
}
  • game-engineer/classes/2023/game-mathematics/first-term/5/06-26-05.txt
  • 最終更新: 3年前
  • by root