0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

円と線分の交差判定

Last updated at Posted at 2019-04-24

円と線分の交差判定

Unityで使える円と線分の交差判定です。

コード

IsLineIntersectedCircle.cs
/// <summary>
/// 円と線分の交差判定
/// </summary>
private bool IsLineIntersectedCircle( Vector2 a, Vector2 b, Vector2 p, float radius )
{
	Vector2 ap = p - a;
	Vector2 ab = b - a;
	Vector2 bp = p - b;
	Vector2 normalAB = ab.normalized;

	float lenAX = Vector2.Dot( normalAB, ap );

	float shortestDistance = 0.0f;
	if ( lenAX < 0 )
	{
		shortestDistance = ap.magnitude;
	}
	else if( lenAX > ab.magnitude )
	{
		shortestDistance = bp.magnitude;
	}
	else
	{
		shortestDistance = Mathf.Abs(Vector3.Cross(normalAB, ap).magnitude);
	}

	Vector2 X = a + ab * lenAX;
	return shortestDistance < radius;
}

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?