Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System;
- using System.Collections.Generic;
- public struct IntVector2 {
- public static IntVector2 zero { get { return new IntVector2(0, 0); } }
- public static IntVector2 one { get { return new IntVector2(1, 1); } }
- // -----------------------------------
- private readonly int _x;
- private readonly int _y;
- public IntVector2(int x, int y) {
- this._x = x;
- this._y = y;
- }
- public int x {
- get { return _x; }
- }
- public int y {
- get { return _y; }
- }
- public Vector2 ToVector2 () {
- return new Vector2 (_x, _y);
- }
- public Vector3 PadLast() {
- return new Vector3(_x, _y, 0);
- }
- public Vector3 PadMiddle() {
- return new Vector3(_x, 0, _y);
- }
- public IntVector2 Reverse() {
- return new IntVector2(-_x, -_y);
- }
- public float Average () {
- return (_x + _y) / 2f;
- }
- public int Sum () {
- return _x + _y;
- }
- public float Hypoteneuse () {
- return Mathf.Sqrt(_x * _x + _y * _y);
- }
- public bool IsDiagonal() {
- return x != 0 && y != 0 && Math.Abs(x) == Math.Abs(y);
- }
- public bool IsOrthogonal () {
- return x == 0 || y == 0;
- }
- public int Magnitude {
- get { return Math.Abs(x) + Math.Abs(y); }
- }
- public int LargestDimension {
- get { return Mathf.Max (Math.Abs(x), Math.Abs(y)); }
- }
- public int ManhattanDistanceTo (IntVector2 other) {
- return Math.Abs(_x - other._x) + Math.Abs(_y - other._y);
- }
- public int DiagonalDistanceTo (IntVector2 other) {
- return Mathf.Max(Mathf.Abs (other.x - x), Mathf.Abs (other.y - y));
- }
- public bool IsZero {
- get { return x == 0 && y == 0; }
- }
- public static bool operator ==(IntVector2 a, IntVector2 b) {
- return a._x == b._x && a._y == b._y;
- }
- public static bool operator !=(IntVector2 a, IntVector2 b) {
- return !(a == b);
- }
- public override bool Equals(object obj) {
- if (!(obj is IntVector2)) {
- return false;
- }
- IntVector2 other = (IntVector2)obj;
- return _x == other._x && _y == other._y;
- }
- public override int GetHashCode() {
- return _x + _y;
- }
- public override string ToString() {
- return "(" + _x + "," + _y + ")";
- }
- public static IntVector2 operator +(IntVector2 c1, IntVector2 c2) {
- return new IntVector2(c1._x + c2._x, c1._y + c2._y);
- }
- public static IntVector2 operator -(IntVector2 c1, IntVector2 c2) {
- return new IntVector2(c1._x - c2._x, c1._y - c2._y);
- }
- public static IntVector2 operator -(IntVector2 c) {
- return new IntVector2(-c._x, -c._y);
- }
- public static IntVector2 operator *(IntVector2 c1, int scalar) {
- return new IntVector2(c1._x * scalar, c1._y * scalar);
- }
- public static Vector2 operator *(IntVector2 c1, float scalar) {
- return new Vector2(c1._x * scalar, c1._y * scalar);
- }
- public static IntVector2 operator / (IntVector2 c1, int divisor) {
- return new IntVector2(c1._x / divisor, c1._y / divisor);
- }
- public static implicit operator Vector2(IntVector2 v) {
- return new Vector2(v.x, v.y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment