Created
May 13, 2019 01:25
-
-
Save nofishleft/f31451dbbdb8861055b31e047633cc16 to your computer and use it in GitHub Desktop.
Finds the 4th point in a rectangle, given three known points
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 UnityEngine; | |
public class Find4thPoint { | |
public Vector3 Find4th(Vector3 a, Vector3 b, Vector3 c) { | |
Vector3 point, cornerA, cornerB; | |
Vector3[] points = new Vector3[] {a, b, c}; | |
float angleDelta = 180; | |
int pointIndex = -1; | |
//find point with angle closest to 90 degrees | |
for (int i = 0; i < 3; ++i) { | |
point = points[i]; | |
cornerA = points[i != 0 ? i-1 : 2]; | |
cornerB = points[i != 2 ? i+1 : 0]; | |
Vector3 lineA = cornerA - point; | |
Vector3 lineB = cornerB - point; | |
float ang = Mathf.Abs(Vector3.Angle(lineA, lineB)-90); | |
if (ang < angleDelta) { | |
angleDelta = ang; | |
pointIndex = i; | |
} | |
} | |
point = points[pointIndex]; | |
cornerA = points[pointIndex != 0 ? pointIndex-1 : 2]; | |
cornerB = points[pointIndex != 2 ? pointIndex+1 : 0]; | |
return cornerA + cornerB - point; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment