2007-06-13 19:39:38 +00:00
|
|
|
|
|
2007-06-14 23:31:57 +00:00
|
|
|
|
#region ================== Copyright (c) 2007 Pascal vd Heiden
|
|
|
|
|
|
2007-06-13 19:39:38 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
|
|
|
|
|
* This program is released under GNU General Public License
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2007-06-14 23:31:57 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ================== Namespaces
|
|
|
|
|
|
2007-06-13 19:39:38 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
2007-06-14 23:31:57 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2007-06-13 19:39:38 +00:00
|
|
|
|
namespace CodeImp.DoomBuilder.Geometry
|
|
|
|
|
{
|
2007-07-07 09:40:34 +00:00
|
|
|
|
public struct Vector2D
|
2007-06-13 19:39:38 +00:00
|
|
|
|
{
|
|
|
|
|
#region ================== Constants
|
|
|
|
|
|
|
|
|
|
private const float TINY_VALUE = 0.0000000001f;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ================== Variables
|
|
|
|
|
|
|
|
|
|
// Coordinates
|
|
|
|
|
public float x;
|
|
|
|
|
public float y;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ================== Constructors
|
|
|
|
|
|
|
|
|
|
// Constructor
|
|
|
|
|
public Vector2D(float x, float y)
|
|
|
|
|
{
|
|
|
|
|
this.x = x;
|
|
|
|
|
this.y = y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Constructor
|
|
|
|
|
public Vector2D(Vector3D v)
|
|
|
|
|
{
|
|
|
|
|
this.x = v.x;
|
|
|
|
|
this.y = v.y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ================== Statics
|
|
|
|
|
|
|
|
|
|
// Conversion to Vector3D
|
|
|
|
|
public static implicit operator Vector3D(Vector2D a)
|
|
|
|
|
{
|
|
|
|
|
return new Vector3D(a);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This adds two vectors
|
|
|
|
|
public static Vector2D operator +(Vector2D a, Vector2D b)
|
|
|
|
|
{
|
|
|
|
|
return new Vector2D(a.x + b.x, a.y + b.y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This adds to a vector
|
|
|
|
|
public static Vector2D operator +(float a, Vector2D b)
|
|
|
|
|
{
|
|
|
|
|
return new Vector2D(a + b.x, a + b.y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This adds to a vector
|
|
|
|
|
public static Vector2D operator +(Vector2D a, float b)
|
|
|
|
|
{
|
|
|
|
|
return new Vector2D(a.x + b, a.y + b);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This subtracts two vectors
|
|
|
|
|
public static Vector2D operator -(Vector2D a, Vector2D b)
|
|
|
|
|
{
|
|
|
|
|
return new Vector2D(a.x - b.x, a.y - b.y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This subtracts from a vector
|
|
|
|
|
public static Vector2D operator -(Vector2D a, float b)
|
|
|
|
|
{
|
|
|
|
|
return new Vector2D(a.x - b, a.y - b);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This subtracts from a vector
|
|
|
|
|
public static Vector2D operator -(float a, Vector2D b)
|
|
|
|
|
{
|
|
|
|
|
return new Vector2D(a - b.x, a - b.y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This reverses a vector
|
|
|
|
|
public static Vector2D operator -(Vector2D a)
|
|
|
|
|
{
|
|
|
|
|
return new Vector2D(-a.x, -a.y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This scales a vector
|
|
|
|
|
public static Vector2D operator *(float s, Vector2D a)
|
|
|
|
|
{
|
|
|
|
|
return new Vector2D(a.x * s, a.y * s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This scales a vector
|
|
|
|
|
public static Vector2D operator *(Vector2D a, float s)
|
|
|
|
|
{
|
|
|
|
|
return new Vector2D(a.x * s, a.y * s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This scales a vector
|
|
|
|
|
public static Vector2D operator *(Vector2D a, Vector2D b)
|
|
|
|
|
{
|
|
|
|
|
return new Vector2D(a.x * b.x, a.y * b.y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This scales a vector
|
|
|
|
|
public static Vector2D operator /(float s, Vector2D a)
|
|
|
|
|
{
|
|
|
|
|
return new Vector2D(a.x / s, a.y / s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This scales a vector
|
|
|
|
|
public static Vector2D operator /(Vector2D a, float s)
|
|
|
|
|
{
|
|
|
|
|
return new Vector2D(a.x / s, a.y / s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This scales a vector
|
|
|
|
|
public static Vector2D operator /(Vector2D a, Vector2D b)
|
|
|
|
|
{
|
|
|
|
|
return new Vector2D(a.x / b.x, a.y / b.y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This calculates the dot product
|
|
|
|
|
public static float DotProduct(Vector2D a, Vector2D b)
|
|
|
|
|
{
|
|
|
|
|
// Calculate and return the dot product
|
|
|
|
|
return a.x * b.x + a.y * b.y;
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-07 19:10:23 +00:00
|
|
|
|
// This calculates the cross product
|
|
|
|
|
public static Vector2D CrossProduct(Vector2D a, Vector2D b)
|
|
|
|
|
{
|
|
|
|
|
Vector2D result = new Vector2D();
|
|
|
|
|
|
|
|
|
|
// Calculate and return the dot product
|
|
|
|
|
result.x = a.y * b.x;
|
|
|
|
|
result.y = a.x * b.y;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-13 09:06:15 +00:00
|
|
|
|
// This compares a vector
|
|
|
|
|
public static bool operator ==(Vector2D a, Vector2D b)
|
|
|
|
|
{
|
|
|
|
|
return (a.x == b.x) && (a.y == b.y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This compares a vector
|
|
|
|
|
public static bool operator !=(Vector2D a, Vector2D b)
|
|
|
|
|
{
|
|
|
|
|
return (a.x != b.x) || (a.y != b.y);
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-13 19:39:38 +00:00
|
|
|
|
// This reflects the vector v over mirror m
|
|
|
|
|
// Note that mirror m must be normalized!
|
|
|
|
|
// R = V - 2 * M * (M dot V)
|
|
|
|
|
public static Vector2D Reflect(Vector2D v, Vector2D m)
|
|
|
|
|
{
|
|
|
|
|
// Get the dot product of v and m
|
|
|
|
|
float dp = Vector2D.DotProduct(m, v);
|
|
|
|
|
|
|
|
|
|
// Make the reflected vector
|
|
|
|
|
Vector2D mv = new Vector2D();
|
|
|
|
|
mv.x = v.x - (2f * m.x * dp);
|
|
|
|
|
mv.y = v.y - (2f * m.y * dp);
|
|
|
|
|
|
|
|
|
|
// Return the reflected vector
|
|
|
|
|
return mv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This returns the reversed vector
|
|
|
|
|
public static Vector2D Reversed(Vector2D v)
|
|
|
|
|
{
|
|
|
|
|
// Return reversed vector
|
|
|
|
|
return new Vector2D(-v.x, -v.y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This returns a vector from an angle
|
|
|
|
|
public static Vector2D FromAngle(float angle)
|
|
|
|
|
{
|
|
|
|
|
// Return vector from angle
|
|
|
|
|
return new Vector2D((float)Math.Sin(angle), -(float)Math.Cos(angle));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This returns a vector from an angle with a given legnth
|
|
|
|
|
public static Vector2D FromAngle(float angle, float length)
|
|
|
|
|
{
|
|
|
|
|
// Return vector from angle
|
|
|
|
|
return FromAngle(angle) * length;
|
|
|
|
|
}
|
2008-01-18 19:52:19 +00:00
|
|
|
|
|
|
|
|
|
// This calculates the angle
|
|
|
|
|
public static float GetAngle(Vector2D a, Vector2D b)
|
|
|
|
|
{
|
|
|
|
|
// Calculate and return the angle
|
|
|
|
|
return -(float)Math.Atan2(-(a.y - b.y), (a.x - b.x)) + (float)Math.PI * 0.5f;
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-13 19:39:38 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ================== Methods
|
|
|
|
|
|
2008-09-09 12:20:39 +00:00
|
|
|
|
// This returns the perpendicular vector by simply making a normal
|
|
|
|
|
public Vector2D GetPerpendicular()
|
|
|
|
|
{
|
|
|
|
|
return new Vector2D(-y, x);
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-09 05:52:11 +00:00
|
|
|
|
// This returns a vector with the sign of all components
|
|
|
|
|
public Vector2D GetSign()
|
|
|
|
|
{
|
|
|
|
|
return new Vector2D(Math.Sign(x), Math.Sign(y));
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-13 19:39:38 +00:00
|
|
|
|
// This calculates the angle
|
|
|
|
|
public float GetAngle()
|
|
|
|
|
{
|
|
|
|
|
// Calculate and return the angle
|
|
|
|
|
return -(float)Math.Atan2(-y, x) + (float)Math.PI * 0.5f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This calculates the length
|
|
|
|
|
public float GetLength()
|
|
|
|
|
{
|
|
|
|
|
// Calculate and return the length
|
|
|
|
|
return (float)Math.Sqrt(x * x + y * y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This calculates the square length
|
|
|
|
|
public float GetLengthSq()
|
|
|
|
|
{
|
|
|
|
|
// Calculate and return the square length
|
|
|
|
|
return x * x + y * y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This calculates the length
|
|
|
|
|
public float GetManhattanLength()
|
|
|
|
|
{
|
|
|
|
|
// Calculate and return the length
|
|
|
|
|
return Math.Abs(x) + Math.Abs(y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This returns a normalized vector
|
|
|
|
|
public Vector2D GetNormal()
|
|
|
|
|
{
|
|
|
|
|
float lensq = this.GetLengthSq();
|
|
|
|
|
if(lensq > TINY_VALUE)
|
|
|
|
|
{
|
|
|
|
|
// Divide each element by the length
|
|
|
|
|
float mul = 1f / (float)Math.Sqrt(lensq);
|
|
|
|
|
return new Vector2D(x * mul, y * mul);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Cannot make normal
|
|
|
|
|
return new Vector2D(0f, 0f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This scales the vector
|
|
|
|
|
public Vector2D GetScaled(float s)
|
|
|
|
|
{
|
|
|
|
|
// Scale the vector
|
|
|
|
|
return new Vector2D(x * s, y * s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This changes the vector length
|
|
|
|
|
public Vector2D GetFixedLength(float l)
|
|
|
|
|
{
|
|
|
|
|
// Normalize, then scale
|
|
|
|
|
return this.GetNormal().GetScaled(l);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Output
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return x + ", " + y;
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-17 21:22:46 +00:00
|
|
|
|
// Transform
|
2007-10-20 19:50:03 +00:00
|
|
|
|
public unsafe Vector2D GetTransformed(float offsetx, float offsety, float scalex, float scaley)
|
2007-09-17 21:22:46 +00:00
|
|
|
|
{
|
2007-10-20 19:50:03 +00:00
|
|
|
|
return new Vector2D((x + offsetx) * scalex, (y + offsety) * scaley);
|
2007-10-19 14:27:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-10-20 19:50:03 +00:00
|
|
|
|
// Inverse Transform
|
|
|
|
|
public unsafe Vector2D GetInvTransformed(float invoffsetx, float invoffsety, float invscalex, float invscaley)
|
2007-10-19 14:27:46 +00:00
|
|
|
|
{
|
2007-10-20 19:50:03 +00:00
|
|
|
|
return new Vector2D((x * invscalex) + invoffsetx, (y * invscaley) + invoffsety);
|
2007-09-17 21:22:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-05-18 20:03:09 +00:00
|
|
|
|
// Rotate (Added by Anders <20>strand 2008-05-18)
|
|
|
|
|
public unsafe Vector2D GetRotated(float theta)
|
|
|
|
|
{
|
2008-09-09 05:52:11 +00:00
|
|
|
|
double cos = Math.Cos(theta);
|
|
|
|
|
double sin = Math.Sin(theta);
|
|
|
|
|
double rx = cos * x - sin * y;
|
|
|
|
|
double ry = sin * x + cos * y;
|
2008-05-18 20:03:09 +00:00
|
|
|
|
return new Vector2D((float)rx, (float)ry);
|
|
|
|
|
}
|
2007-06-13 19:39:38 +00:00
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|