godot/modules/mono/glue/cs_files/Vector2.cs

363 lines
8.4 KiB
C#
Raw Normal View History

2017-10-02 21:24:00 +00:00
using System;
using System.Runtime.InteropServices;
// file: core/math/math_2d.h
// commit: 7ad14e7a3e6f87ddc450f7e34621eb5200808451
// file: core/math/math_2d.cpp
// commit: 7ad14e7a3e6f87ddc450f7e34621eb5200808451
// file: core/variant_call.cpp
// commit: 5ad9be4c24e9d7dc5672fdc42cea896622fe5685
namespace Godot
{
[StructLayout(LayoutKind.Sequential)]
public struct Vector2 : IEquatable<Vector2>
{
public float x;
public float y;
public float this[int index]
{
get
{
switch (index)
{
case 0:
return x;
case 1:
return y;
default:
throw new IndexOutOfRangeException();
}
}
set
{
switch (index)
{
case 0:
x = value;
return;
case 1:
y = value;
return;
default:
throw new IndexOutOfRangeException();
}
}
}
2017-11-21 22:32:19 +00:00
internal void Normalize()
2017-10-02 21:24:00 +00:00
{
float length = x * x + y * y;
if (length != 0f)
{
2017-11-21 22:32:19 +00:00
length = Mathf.Sqrt(length);
2017-10-02 21:24:00 +00:00
x /= length;
y /= length;
}
}
2017-11-21 22:32:19 +00:00
private float Cross(Vector2 b)
2017-10-02 21:24:00 +00:00
{
return x * b.y - y * b.x;
}
2017-11-21 22:32:19 +00:00
public Vector2 Abs()
2017-10-02 21:24:00 +00:00
{
2017-11-21 22:32:19 +00:00
return new Vector2(Mathf.Abs(x), Mathf.Abs(y));
2017-10-02 21:24:00 +00:00
}
2017-11-21 22:32:19 +00:00
public float Angle()
2017-10-02 21:24:00 +00:00
{
2017-11-21 22:32:19 +00:00
return Mathf.Atan2(y, x);
2017-10-02 21:24:00 +00:00
}
2017-11-21 22:32:19 +00:00
public float AngleTo(Vector2 to)
2017-10-02 21:24:00 +00:00
{
2017-11-21 22:32:19 +00:00
return Mathf.Atan2(Cross(to), Dot(to));
2017-10-02 21:24:00 +00:00
}
2017-11-21 22:32:19 +00:00
public float AngleToPoint(Vector2 to)
2017-10-02 21:24:00 +00:00
{
2017-11-21 22:32:19 +00:00
return Mathf.Atan2(x - to.x, y - to.y);
2017-10-02 21:24:00 +00:00
}
2017-11-21 22:32:19 +00:00
public float Aspect()
2017-10-02 21:24:00 +00:00
{
return x / y;
}
2017-11-21 22:32:19 +00:00
public Vector2 Bounce(Vector2 n)
2017-10-02 21:24:00 +00:00
{
2017-11-21 22:32:19 +00:00
return -Reflect(n);
2017-10-02 21:24:00 +00:00
}
2017-11-21 22:32:19 +00:00
public Vector2 Clamped(float length)
2017-10-02 21:24:00 +00:00
{
Vector2 v = this;
2017-11-21 22:32:19 +00:00
float l = this.Length();
2017-10-02 21:24:00 +00:00
if (l > 0 && length < l)
{
v /= l;
v *= length;
}
return v;
}
2017-11-21 22:32:19 +00:00
public Vector2 CubicInterpolate(Vector2 b, Vector2 preA, Vector2 postB, float t)
2017-10-02 21:24:00 +00:00
{
Vector2 p0 = preA;
Vector2 p1 = this;
Vector2 p2 = b;
Vector2 p3 = postB;
float t2 = t * t;
float t3 = t2 * t;
return 0.5f * ((p1 * 2.0f) +
(-p0 + p2) * t +
(2.0f * p0 - 5.0f * p1 + 4 * p2 - p3) * t2 +
(-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3);
}
2017-11-21 22:32:19 +00:00
public float DistanceSquaredTo(Vector2 to)
2017-10-02 21:24:00 +00:00
{
return (x - to.x) * (x - to.x) + (y - to.y) * (y - to.y);
}
2017-11-21 22:32:19 +00:00
public float DistanceTo(Vector2 to)
2017-10-02 21:24:00 +00:00
{
2017-11-21 22:32:19 +00:00
return Mathf.Sqrt((x - to.x) * (x - to.x) + (y - to.y) * (y - to.y));
2017-10-02 21:24:00 +00:00
}
2017-11-21 22:32:19 +00:00
public float Dot(Vector2 with)
2017-10-02 21:24:00 +00:00
{
return x * with.x + y * with.y;
}
2017-11-21 22:32:19 +00:00
public Vector2 Floor()
2017-10-02 21:24:00 +00:00
{
2017-11-21 22:32:19 +00:00
return new Vector2(Mathf.Floor(x), Mathf.Floor(y));
2017-10-02 21:24:00 +00:00
}
2017-11-21 22:32:19 +00:00
public bool IsNormalized()
2017-10-02 21:24:00 +00:00
{
2017-11-21 22:32:19 +00:00
return Mathf.Abs(LengthSquared() - 1.0f) < Mathf.Epsilon;
2017-10-02 21:24:00 +00:00
}
2017-11-21 22:32:19 +00:00
public float Length()
2017-10-02 21:24:00 +00:00
{
2017-11-21 22:32:19 +00:00
return Mathf.Sqrt(x * x + y * y);
2017-10-02 21:24:00 +00:00
}
2017-11-21 22:32:19 +00:00
public float LengthSquared()
2017-10-02 21:24:00 +00:00
{
return x * x + y * y;
}
2017-11-21 22:32:19 +00:00
public Vector2 LinearInterpolate(Vector2 b, float t)
2017-10-02 21:24:00 +00:00
{
Vector2 res = this;
res.x += (t * (b.x - x));
res.y += (t * (b.y - y));
return res;
}
2017-11-21 22:32:19 +00:00
public Vector2 Normalized()
2017-10-02 21:24:00 +00:00
{
Vector2 result = this;
2017-11-21 22:32:19 +00:00
result.Normalize();
2017-10-02 21:24:00 +00:00
return result;
}
2017-11-21 22:32:19 +00:00
public Vector2 Reflect(Vector2 n)
2017-10-02 21:24:00 +00:00
{
2017-11-21 22:32:19 +00:00
return 2.0f * n * Dot(n) - this;
2017-10-02 21:24:00 +00:00
}
2017-11-21 22:32:19 +00:00
public Vector2 Rotated(float phi)
2017-10-02 21:24:00 +00:00
{
2017-11-21 22:32:19 +00:00
float rads = Angle() + phi;
return new Vector2(Mathf.Cos(rads), Mathf.Sin(rads)) * Length();
2017-10-02 21:24:00 +00:00
}
2017-11-21 22:32:19 +00:00
public Vector2 Slide(Vector2 n)
2017-10-02 21:24:00 +00:00
{
2017-11-21 22:32:19 +00:00
return this - n * Dot(n);
2017-10-02 21:24:00 +00:00
}
2017-11-21 22:32:19 +00:00
public Vector2 Snapped(Vector2 by)
2017-10-02 21:24:00 +00:00
{
2017-11-21 22:32:19 +00:00
return new Vector2(Mathf.Stepify(x, by.x), Mathf.Stepify(y, by.y));
2017-10-02 21:24:00 +00:00
}
2017-11-21 22:32:19 +00:00
public Vector2 Tangent()
2017-10-02 21:24:00 +00:00
{
return new Vector2(y, -x);
}
public Vector2(float x, float y)
{
this.x = x;
this.y = y;
}
public static Vector2 operator +(Vector2 left, Vector2 right)
{
left.x += right.x;
left.y += right.y;
return left;
}
public static Vector2 operator -(Vector2 left, Vector2 right)
{
left.x -= right.x;
left.y -= right.y;
return left;
}
public static Vector2 operator -(Vector2 vec)
{
vec.x = -vec.x;
vec.y = -vec.y;
return vec;
}
public static Vector2 operator *(Vector2 vec, float scale)
{
vec.x *= scale;
vec.y *= scale;
return vec;
}
public static Vector2 operator *(float scale, Vector2 vec)
{
vec.x *= scale;
vec.y *= scale;
return vec;
}
public static Vector2 operator *(Vector2 left, Vector2 right)
{
left.x *= right.x;
left.y *= right.y;
return left;
}
public static Vector2 operator /(Vector2 vec, float scale)
{
vec.x /= scale;
vec.y /= scale;
return vec;
}
public static Vector2 operator /(Vector2 left, Vector2 right)
{
left.x /= right.x;
left.y /= right.y;
return left;
}
public static bool operator ==(Vector2 left, Vector2 right)
{
return left.Equals(right);
}
public static bool operator !=(Vector2 left, Vector2 right)
{
return !left.Equals(right);
}
public static bool operator <(Vector2 left, Vector2 right)
{
if (left.x.Equals(right.x))
{
return left.y < right.y;
}
else
{
return left.x < right.x;
}
}
public static bool operator >(Vector2 left, Vector2 right)
{
if (left.x.Equals(right.x))
{
return left.y > right.y;
}
else
{
return left.x > right.x;
}
}
public static bool operator <=(Vector2 left, Vector2 right)
{
if (left.x.Equals(right.x))
{
return left.y <= right.y;
}
else
{
return left.x <= right.x;
}
}
public static bool operator >=(Vector2 left, Vector2 right)
{
if (left.x.Equals(right.x))
{
return left.y >= right.y;
}
else
{
return left.x >= right.x;
}
}
public override bool Equals(object obj)
{
if (obj is Vector2)
{
return Equals((Vector2)obj);
}
return false;
}
public bool Equals(Vector2 other)
{
return x == other.x && y == other.y;
}
public override int GetHashCode()
{
return y.GetHashCode() ^ x.GetHashCode();
}
public override string ToString()
{
return String.Format("({0}, {1})", new object[]
{
this.x.ToString(),
this.y.ToString()
});
}
public string ToString(string format)
{
return String.Format("({0}, {1})", new object[]
{
this.x.ToString(format),
this.y.ToString(format)
});
}
}
}