using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace TransportGame.Model
{
public struct Vector2
{
///
/// Zero vector
///
public static readonly Vector2 Zero = new Vector2(0, 0);
///
/// Unit vector
///
public static readonly Vector2 Unit = new Vector2(1, 0);
///
/// Gets the X component
///
public float X { get; private set; }
///
/// Gets the Y component
///
public float Y { get; private set; }
///
/// Initializes a vector2
///
/// X component
/// Y component
public Vector2(float x, float y)
: this()
{
X = x;
Y = y;
}
///
/// Gets the length of the vector
///
public float Length
{
get
{
return (float)Math.Sqrt(LengthSq);
}
}
///
/// Gets the length of the vector squared
///
public float LengthSq
{
get
{
return X * X + Y * Y;
}
}
///
/// Gets the normalized vector
///
/// Normalized vector
public Vector2 Normalized
{
get
{
float len = Length;
return new Vector2(X / len, Y / len);
}
}
///
/// Rotates vector by given number of radians
///
///
///
public Vector2 Rotate(float radians)
{
float sin = (float)Math.Sin(radians);
float cos = (float)Math.Cos(radians);
return new Vector2(X * cos - Y * sin, X * sin + Y * cos);
}
///
/// Rotates vector by given number of degrees
///
///
///
public Vector2 RotateDeg(float degrees)
{
return Rotate(degrees * (float)Math.PI / 180f);
}
///
/// Sum operator
///
/// First vector
/// Second vector
/// Result of addition
public static Vector2 operator +(Vector2 a, Vector2 b)
{
return new Vector2(a.X + b.X, a.Y + b.Y);
}
///
/// Subtract operator
///
/// First vector
/// Second vector
/// Result of subtraction
public static Vector2 operator -(Vector2 a, Vector2 b)
{
return new Vector2(a.X - b.X, a.Y - b.Y);
}
///
/// Multiply by constant
///
/// Vector
/// Constant
/// Result
public static Vector2 operator *(Vector2 a, float c)
{
return new Vector2(a.X * c, a.Y * c);
}
///
/// Multiply by constant
///
/// Constant
/// Vector
/// Result
public static Vector2 operator *(float c, Vector2 a)
{
return new Vector2(a.X * c, a.Y * c);
}
///
/// Divide by constant
///
/// Vector
/// Constant
/// Result
public static Vector2 operator /(Vector2 a, float c)
{
return new Vector2(a.X / c, a.Y / c);
}
///
/// Equality operator
///
/// First vector
/// Second vector
/// True if vectors are equal
public static bool operator ==(Vector2 a, Vector2 b)
{
return a.X == b.X && a.Y == b.Y;
}
///
/// Inequality operator
///
/// First vector
/// Second vector
/// True if vectors are not equal
public static bool operator !=(Vector2 a, Vector2 b)
{
return a.X != b.X || a.Y != b.Y;
}
///
/// Calculates dot product of two vectors
///
/// First vector
/// Second vector
/// Dot product
public static float Dot(Vector2 a, Vector2 b)
{
return a.X * b.X + a.Y * b.Y;
}
///
/// Gets the vector corresponding with specified angle (in radians)
///
/// Radians
/// Vector
public static Vector2 FromRadians(float rads)
{
return new Vector2((float)Math.Cos(rads), (float)Math.Sin(rads));
}
///
/// Gets the vector corresponding with specified angle (in degrees)
///
/// Degrees
/// Vector
public static Vector2 FromDegrees(float degs)
{
float rads = (degs * (float)Math.PI / 180f);
return FromRadians(rads);
}
public override string ToString()
{
return String.Format("({0}, {1})", X, Y);
}
public override bool Equals(object obj)
{
if (obj is Vector2)
{
Vector2 other = (Vector2)obj;
return X == other.X && Y == other.Y;
}
return false;
}
public override int GetHashCode()
{
return X.GetHashCode() * 7 + Y.GetHashCode();
}
}
}