Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.