Created
May 12, 2020 06:25
-
-
Save kitasenjudesign/a761c40cb40cb2095e65d42c66825ce6 to your computer and use it in GitHub Desktop.
垂線 perpendicular line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class PerpendicularTest : MonoBehaviour | |
{ | |
[SerializeField] private GameObject _startPos; | |
[SerializeField] private GameObject _endPos; | |
[SerializeField] private GameObject _point; | |
//https://sleepygamersmemo.blogspot.com/2019/03/perpendicular-foot-point.html | |
void Start() | |
{ | |
} | |
void Update() | |
{ | |
Debug.DrawLine( | |
_startPos.transform.position, | |
_endPos.transform.position, | |
Color.red | |
); | |
//startPos,endPosベクトルにpointからひいた直行ベクトルの 交点P | |
var p = PerpendicularFootPoint( | |
_startPos.transform.position, | |
_endPos.transform.position, | |
_point.transform.position | |
); | |
Debug.DrawLine( | |
_point.transform.position, | |
p, | |
Color.green | |
); | |
} | |
Vector3 PerpendicularFootPoint(Vector3 a, Vector3 b, Vector3 p) | |
{ | |
return a + Vector3.Project(p - a, b - a); | |
} | |
// 点Pから直線ABに下ろした垂線の足の座標を返す | |
Vector2 PerpendicularFootPoint(Vector2 a, Vector2 b, Vector2 p) | |
{ | |
Vector2 ab = (b - a).normalized; | |
return a + Vector2.Dot(p - a, ab) * ab; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment