Created
April 1, 2020 09:41
-
-
Save openroomxyz/c67e77f710eb53aaaae7390ded9ce86d to your computer and use it in GitHub Desktop.
Unity : How to calculate the Area of Triangle in 3D?
This file contains hidden or 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; | |
using UnityEditor; | |
public class AreaOfTrinagle : EditorWindow | |
{ | |
string objectBaseName = ""; | |
int objectId = 1; | |
GameObject objectToSpawn; | |
float objectScale; | |
float areaResult = 0f; | |
Vector3 pointA; | |
Vector3 pointB; | |
Vector3 pointC; | |
[MenuItem("Tools/AreaOfTrinagle")] | |
public static void ShowWindow() | |
{ | |
GetWindow(typeof(AreaOfTrinagle)); //GetWindow is a method inherited from the EditorWindow class | |
} | |
public void OnGUI() | |
{ | |
GUILayout.Label("Spawn new Object", EditorStyles.boldLabel); | |
//EditorGUILayout.FloatField("Spawn Radius", spawnRadius); | |
pointA = EditorGUILayout.Vector3Field("A", pointA); | |
pointB = EditorGUILayout.Vector3Field("B", pointB); | |
pointC = EditorGUILayout.Vector3Field("C", pointC); | |
//https://math.stackexchange.com/questions/128991/how-to-calculate-the-area-of-a-3d-triangle | |
areaResult = EditorGUILayout.FloatField("Area : ", CalculateAreaOfTrinagle(pointA, pointB, pointC)); | |
} | |
float CalculateAreaOfTrinagle(Vector3 n1, Vector3 n2 , Vector3 n3) | |
{ | |
float res = Mathf.Pow(((n2.x * n1.y) - (n3.x * n1.y) - (n1.x * n2.y) + (n3.x * n2.y) + (n1.x * n3.y) - (n2.x * n3.y)), 2.0f); | |
res += Mathf.Pow(((n2.x * n1.z) - (n3.x * n1.z) - (n1.x * n2.z) + (n3.x * n2.z) + (n1.x * n3.z) - (n2.x * n3.z)), 2.0f); | |
res += Mathf.Pow(((n2.y * n1.z) - (n3.y * n1.z) - (n1.y * n2.z) + (n3.y * n2.z) + (n1.y * n3.z) - (n2.y * n3.z)), 2.0f); | |
return Mathf.Sqrt(res) * 0.5f; | |
} | |
private void CalculateTheArea() | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment