Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class Test : MonoBehaviour {
- void Start () {
- Vector3[] coords = {new Vector3(2,1,0), new Vector3(1,1,0), new Vector3(1,2,0), new Vector3(0,0,0)};
- coords[3] = Find4th(coords[0], coords[1], coords[2]);
- foreach (Vector3 v in coords) {
- new GameObject().transform.position = v;
- }
- }
- 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);
- int ai = (i != 0) ? i-1 : 2;
- int bi = (i != 2) ? i+1 : 0;
- Debug.Log($"{i}: {ang}, {ai}, {bi}");
- if (ang < angleDelta) {
- angleDelta = ang;
- pointIndex = i;
- }
- }
- point = points[pointIndex];
- cornerA = points[pointIndex != 0 ? pointIndex-1 : 2];
- cornerB = points[pointIndex != 2 ? pointIndex+1 : 0];
- Debug.Log(cornerA + cornerB - point);
- return cornerA + cornerB - point;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement