powerfun-unity/Assets/AR/GeometryUtil.cs
2023-01-31 18:22:15 +08:00

35 lines
1.5 KiB
C#

using System;
using UnityEngine;
namespace Assets.AR
{
public static class GeometryUtil
{
public static bool CircleCenter(Vector3 aP0, Vector3 aP1, Vector3 aP2, out Vector3 center)
{
Vector3 lhs = aP1 - aP0;
Vector3 vector3 = aP2 - aP0;
Vector3 rhs = Vector3.Cross(lhs, vector3);
if ((double)rhs.sqrMagnitude < 9.9999998245167E-15)
{
center = Vector3.zero;
return false;
}
rhs.Normalize();
if ((double)rhs.sqrMagnitude < 9.9999998245167E-15)
{
center = Vector3.zero;
return false;
}
Vector3 normalized1 = Vector3.Cross(lhs, rhs).normalized;
Vector3 normalized2 = Vector3.Cross(vector3, rhs).normalized;
Vector3 from = (lhs - vector3) * 0.5f;
float num1 = Vector3.Angle(normalized1, normalized2);
float num2 = Vector3.Angle(from, normalized1);
float num3 = from.magnitude * Mathf.Sin(num2 * ((float)Math.PI / 180f)) / Mathf.Sin(num1 * ((float)Math.PI / 180f));
center = (double)Vector3.Dot(lhs, aP2 - aP1) <= 0.0 ? aP0 + vector3 * 0.5f + normalized2 * num3 : aP0 + vector3 * 0.5f - normalized2 * num3;
return !float.IsInfinity(center.x) && !float.IsNaN(center.x) && !float.IsInfinity(center.y) && !float.IsNaN(center.y) && !float.IsInfinity(center.z) && !float.IsNaN(center.z);
}
}
}