84 lines
2.5 KiB
C#
84 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace BrewMonster.Scripts
|
|
{
|
|
public class APoint<T> where T : IComparable<T>
|
|
{
|
|
public T x, y;
|
|
|
|
public APoint()
|
|
{
|
|
x = default(T);
|
|
y = default(T);
|
|
}
|
|
public APoint(T x, T y)
|
|
{
|
|
this.x = x;
|
|
this.y = y;
|
|
}
|
|
public APoint(APoint<T> p)
|
|
{
|
|
x = p.x;
|
|
y = p.y;
|
|
}
|
|
|
|
// Offset point
|
|
public void Offset(T ox, T oy)
|
|
{
|
|
x = Add(x, ox);
|
|
y = Add(y, oy);
|
|
}
|
|
// Set point
|
|
public void Set(T _x, T _y) { x = _x; y = _y; }
|
|
|
|
|
|
// helpers for int/float only
|
|
static T Add(T a, T b)
|
|
{
|
|
if (typeof(T) == typeof(int)) return (T)(object)((int)(object)a + (int)(object)b);
|
|
if (typeof(T) == typeof(float)) return (T)(object)((float)(object)a + (float)(object)b);
|
|
throw new NotSupportedException("APoint<T> supports only int and float");
|
|
}
|
|
static T Sub(T a, T b)
|
|
{
|
|
if (typeof(T) == typeof(int)) return (T)(object)((int)(object)a - (int)(object)b);
|
|
if (typeof(T) == typeof(float)) return (T)(object)((float)(object)a - (float)(object)b);
|
|
throw new NotSupportedException("APoint<T> supports only int and float");
|
|
}
|
|
static T Neg(T a)
|
|
{
|
|
if (typeof(T) == typeof(int)) return (T)(object)(-(int)(object)a);
|
|
if (typeof(T) == typeof(float)) return (T)(object)(-(float)(object)a);
|
|
throw new NotSupportedException("APoint<T> supports only int and float");
|
|
}
|
|
|
|
// == and != operator
|
|
public static bool operator !=(APoint<T> p1, APoint<T> p2)
|
|
{
|
|
return !(p1 == p2);
|
|
}
|
|
public static bool operator ==(APoint<T> p1, APoint<T> p2)
|
|
{
|
|
return EqualityComparer<T>.Default.Equals(p1.x, p2.x) &&
|
|
EqualityComparer<T>.Default.Equals(p1.y, p2.y);
|
|
}
|
|
|
|
// + and - operator
|
|
public static APoint<T> operator +(APoint<T> p1, APoint<T> p2)
|
|
{
|
|
return new APoint<T>(
|
|
Add(p1.x, p2.x),
|
|
Add(p1.y, p2.y)
|
|
);
|
|
}
|
|
public static APoint<T> operator -(APoint<T> p1, APoint<T> p2)
|
|
{
|
|
return new APoint<T>(
|
|
Sub(p1.x, p2.x),
|
|
Sub(p1.y, p2.y)
|
|
);
|
|
}
|
|
}
|
|
} |