Skip to content

Instantly share code, notes, and snippets.

@inoook
Created March 5, 2024 12:44
Show Gist options
  • Save inoook/b1629d9228c42c63cbd4b1112373a8a5 to your computer and use it in GitHub Desktop.
Save inoook/b1629d9228c42c63cbd4b1112373a8a5 to your computer and use it in GitHub Desktop.
doubleを使用して精度を上げる
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// UnityのMathfをdoubleで行う。
/// 精度を上げるため
/// </summary>
public class MathExtend
{
public class Vector3d
{
public double x;
public double y;
public double z;
public Vector3d(double x, double y, double z)
{
this.x = x;
this.y = y;
this.z = z;
}
//public Vector3d normalized
//{
// get { return }
//}
public Vector3d normalized
{
get {
double magnitude = Math.Sqrt(x * x + y * y + z * z);
double _x = x / magnitude;
double _y = y / magnitude;
double _z = z / magnitude;
return new Vector3d(_x, _y, _z);
}
}
public static Vector3d FromVector3(Vector3 v)
{
return new Vector3d(v.x, v.y, v.z);
}
}
public static double SignedAngle(Vector3 from, Vector3 to, Vector3 axis)
{
double angle = Angle(Vector3d.FromVector3(from), Vector3d.FromVector3(to));
double sign = Math.Sign(Dot(Vector3d.FromVector3(axis), Cross(from, to)));
double signedAngle = angle * sign;
return signedAngle;
}
public const double Rad2Deg = 180.0 / Math.PI;
public static double Angle(Vector3d from, Vector3d to)
{
double cosTheta = Dot(from.normalized, to.normalized);
double theta = Acos(cosTheta);
double angle = theta * Rad2Deg;
return angle;
}
public static Vector3d Cross(Vector3 a, Vector3 b)
{
double x = a.y * b.z - a.z * b.y;
double y = a.z * b.x - a.x * b.z;
double z = a.x * b.y - a.y * b.x;
//return new Vector3((float)x, (float)y, (float)z);
return new Vector3d(x, y, z);
}
public static double Dot(Vector3d a, Vector3d b)
{
double dotProduct = a.x * b.x + a.y * b.y + a.z * b.z;
return dotProduct;
}
public static double Acos(double x)
{
if (x < -1.0 || x > 1.0)
{
throw new ArgumentOutOfRangeException("x", "x must be in range [-1, 1]");
}
double theta = Math.Acos(x);
return theta;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment