Skip to content

Instantly share code, notes, and snippets.

@asus4
Last active December 11, 2015 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asus4/4602757 to your computer and use it in GitHub Desktop.
Save asus4/4602757 to your computer and use it in GitHub Desktop.
[Unity] double 精度の Vector3 クラス
using UnityEngine;
namespace Imu {
/// <summary>
/// Double Accuracy Vector3 class.
/// </summary>
public struct DVector3 {
public double x;
public double y;
public double z;
#region constractor
public DVector3 (double xd, double yd, double zd)
{
x = xd;
y = yd;
z = zd;
}
public DVector3 (Vector3 vf)
{
x = vf.x;
y = vf.y;
z = vf.z;
}
public DVector3 (float xf, float yf, float zf)
{
x = xf;
y = yf;
z = zf;
}
#endregion
#region public overrides
// Indexer declaration
public double this [int index] {
get {
if (index == 0) {
return x;
} else if (index == 1) {
return y;
} else if (index == 2) {
return z;
} else {
throw new System.IndexOutOfRangeException ();
}
}
set {
if (index == 0) {
x = value;
} else if (index == 1) {
y = value;
} else if (index == 2) {
z = value;
} else {
throw new System.IndexOutOfRangeException ();
}
}
}
public override string ToString ()
{
return string.Format ("({0:0.000}, {1:0.000}, {2:0.000})", x, y, z);
}
public override bool Equals (object obj)
{
if (obj == null || this.GetType () != obj.GetType ()) {
return false;
}
DVector3 v = (DVector3)obj;
return this == v;
}
public override int GetHashCode ()
{
//XOR
return x.GetHashCode () ^ y.GetHashCode () ^ z.GetHashCode ();
}
#endregion
#region pubic
public Vector3 ToVector3 ()
{
return new Vector3 ((float)x, (float)y, (float)z);
}
#endregion
#region Class Variables
public static DVector3 zero {
get { return new DVector3 (0, 0, 0); }
}
public static DVector3 one {
get { return new DVector3 (1, 1, 1); }
}
public static DVector3 forward {
get { return new DVector3 (0, 0, 1); }
}
public static DVector3 up {
get { return new DVector3 (0, 1, 0); }
}
public static DVector3 right {
get { return new DVector3 (1, 0, 0); }
}
#endregion
#region operator overrides
/*
* OVERRIDES
*
* operator + Adds two vectors.
* operator - Subtracts one vector from another.
* operator * Multiplies a vector by a number.
* operator / Divides a vector by a number.
* operator == Returns true if the vectors are equal.
* operator != Returns true if vectors different.
*/
// +
public static DVector3 operator+ (DVector3 a, DVector3 b)
{
return new DVector3 (a.x + b.x, a.y + b.y, a.z + b.z);
}
public static DVector3 operator+ (Vector3 a, DVector3 b)
{
return new DVector3 (a.x + b.x, a.y + b.y, a.z + b.z);
}
public static DVector3 operator+ (DVector3 a, Vector3 b)
{
return new DVector3 (a.x + b.x, a.y + b.y, a.z + b.z);
}
public static DVector3 operator+ (DVector3 v, double d)
{
return new DVector3 (v.x + d, v.y + d, v.z + d);
}
public static DVector3 operator+ (double d, DVector3 v)
{
return new DVector3 (v.x + d, v.y + d, v.z + d);
}
// -
public static DVector3 operator- (DVector3 a, DVector3 b)
{
return new DVector3 (a.x - b.x, a.y - b.y, a.z - b.z);
}
public static DVector3 operator- (Vector3 a, DVector3 b)
{
return new DVector3 (a.x - b.x, a.y - b.y, a.z - b.z);
}
public static DVector3 operator- (DVector3 a, Vector3 b)
{
return new DVector3 (a.x - b.x, a.y - b.y, a.z - b.z);
}
public static DVector3 operator- (DVector3 v, double d)
{
return new DVector3 (v.x - d, v.y - d, v.z - d);
}
public static DVector3 operator- (double d, DVector3 v)
{
return new DVector3 (v.x - d, v.y - d, v.z - d);
}
// *
public static DVector3 operator* (DVector3 a, DVector3 b)
{
return new DVector3 (a.x * b.x, a.y * b.y, a.z * b.z);
}
public static DVector3 operator* (Vector3 a, DVector3 b)
{
return new DVector3 (a.x * b.x, a.y * b.y, a.z * b.z);
}
public static DVector3 operator* (DVector3 a, Vector3 b)
{
return new DVector3 (a.x * b.x, a.y * b.y, a.z * b.z);
}
public static DVector3 operator* (DVector3 v, double d)
{
return new DVector3 (v.x * d, v.y * d, v.z * d);
}
public static DVector3 operator* (double d, DVector3 v)
{
return new DVector3 (v.x * d, v.y * d, v.z * d);
}
// /
public static DVector3 operator/ (DVector3 a, DVector3 b)
{
return new DVector3 (a.x / b.x, a.y / b.y, a.z / b.z);
}
public static DVector3 operator/ (Vector3 a, DVector3 b)
{
return new DVector3 (a.x / b.x, a.y / b.y, a.z / b.z);
}
public static DVector3 operator/ (DVector3 a, Vector3 b)
{
return new DVector3 (a.x / b.x, a.y / b.y, a.z / b.z);
}
public static DVector3 operator/ (DVector3 v, double d)
{
return new DVector3 (v.x / d, v.y / d, v.z / d);
}
public static DVector3 operator/ (double d, DVector3 v)
{
return new DVector3 (v.x / d, v.y / d, v.z / d);
}
// ==
public static bool operator== (DVector3 a, DVector3 b)
{
return (a.x == b.x) && (a.y == b.y) && (a.z == b.z);
}
public static bool operator== (Vector3 a, DVector3 b)
{
return (a.x == b.x) && (a.y == b.y) && (a.z == b.z);
}
public static bool operator== (DVector3 a, Vector3 b)
{
return (a.x == b.x) && (a.y == b.y) && (a.z == b.z);
}
// !=
public static bool operator!= (DVector3 a, DVector3 b)
{
return (a.x != b.x) || (a.y != b.y) || (a.z == b.z);
}
public static bool operator!= (Vector3 a, DVector3 b)
{
return (a.x != b.x) || (a.y != b.y) || (a.z == b.z);
}
public static bool operator!= (DVector3 a, Vector3 b)
{
return (a.x != b.x) || (a.y != b.y) || (a.z == b.z);
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment