Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nofishleft
Created May 13, 2019 01:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nofishleft/f31451dbbdb8861055b31e047633cc16 to your computer and use it in GitHub Desktop.
Save nofishleft/f31451dbbdb8861055b31e047633cc16 to your computer and use it in GitHub Desktop.
Finds the 4th point in a rectangle, given three known points
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