Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions ConsoleGameLib/CoreTypes/Point.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,64 @@ public Point(int x, int y)

public int X;
public int Y;

public override int GetHashCode()
{
// Uses http://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for referencing the source SO post, great read!

unchecked // Overflow is fine, just wrap
{
int hash = 37;
hash = hash * 67 + X.GetHashCode();
hash = hash * 67 + Y.GetHashCode();
return hash;
}
}

public override bool Equals(object obj)
{
if (obj == null || !(obj is Point))
{
return false;
}
Point oth = (Point)obj;
return X == oth.X && Y == oth.Y;
}

public static bool operator ==(Point left, Point right)
{
// Struct, so no need for null check
return left.X == right.X && left.Y == right.Y;
}

public static bool operator !=(Point left, Point right)
{
// Struct, so no need for null check
return left.X != right.X || left.Y != right.Y;
}

public static Point operator +(Point left, Point right)
{
return new Point(left.X + right.X, left.Y + right.Y);
}

public static Point operator -(Point left, Point right)
{
return new Point(left.X - right.X, left.Y - right.Y);
}

public static Point operator -(Point unary)
{
return new Point(-unary.X, -unary.Y);
}

public static Point operator *(Point point, int scalar)
{
return new Point(point.X * scalar, point.Y * scalar);
}

public static Point operator /(Point point, int scalar)
{
return new Point(point.X / scalar, point.Y / scalar);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding/subtracting points is not really meaningful mathematically, but I understand we're treating these as Vectors essentially. If we later find a need for a Vector class, these methods should instead be implemented there. At the moment, it's convenient to have them though.

}
56 changes: 55 additions & 1 deletion ConsoleGameLib/CoreTypes/Size.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,59 @@ public Size(int width, int height)

public int Width;
public int Height;

public override int GetHashCode()
{
// Uses http://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode
unchecked // Overflow is fine, just wrap
{
int hash = 29;
hash = hash * 89 + Width.GetHashCode();
hash = hash * 89 + Height.GetHashCode();
return hash;
}
}

public override bool Equals(object obj)
{
if (obj == null || !(obj is Size))
{
return false;
}
Size oth = (Size)obj;
return Width == oth.Width && Height == oth.Height;
}

public static bool operator ==(Size left, Size right)
{
// Struct, so no need for null check
return left.Width == right.Width && left.Height == right.Height;
}

public static bool operator !=(Size left, Size right)
{
// Struct, so no need for null check
return left.Width != right.Width || left.Height != right.Height;
}

public static Size operator +(Size left, Size right)
{
return new Size(left.Width + right.Width, left.Height + right.Height);
}

public static Size operator -(Size left, Size right)
{
return new Size(left.Width - right.Width, left.Height - right.Height);
}

public static Size operator *(Size point, int scalar)
{
return new Size(point.Width * scalar, point.Height * scalar);
}

public static Size operator /(Size point, int scalar)
{
return new Size(point.Width / scalar, point.Width / scalar);
}
}
}
}