How can I improve this code or make it tidier?
public class Point2d : IEquatable<Point2d>
{
public double X { get; set; }
public double Y { get; set; }
#region constructor
public Point2d(double x, double y)
{
X = x;
Y = y;
}
public Point2d(double [] points)
{
if (points.GetLength(0) < 2)
throw new Exception("[in Point2d(double [] points)] the array must be at least 2 elements long.");
X = points[0];
Y = points[1];
}
#endregion
public void Print()
{
Console.Write("(");
Console.Write(X);
Console.Write(",");
Console.Write(Y);
Console.Write(") ");
}
public double GetDistance(Point2d otherPoint)
{
return Math.Sqrt(GetSquaredDistance(otherPoint));
}
public double GetSquaredDistance(Point2d otherPoint)
{
return ((otherPoint.X - X) * (otherPoint.X - X))
+ ((otherPoint.Y - Y) * (otherPoint.Y - Y));
}
public Point2d GetTranslated(double x, double y)
{
return GetTranslated(new Point2d(x, y));
}
public Point2d GetTranslated(Point2d center)
{
return new Point2d(X + center.X, Y + center.Y);
}
#region override string ToString()
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("({0:0.00}, {1:0.00})", X, Y);
return sb.ToString();
}
#endregion
#region equality comparison implementations
public override bool Equals(object other)
{
if (!(other is Point2d)) return false;
return Equals((Point2d)other);
}
public bool Equals(Point2d other)
{
return Math.Round(X, 2) == Math.Round(other.X, 2) && Math.Round(Y,2) == Math.Round(other.Y, 2);
}
public override int GetHashCode()
{
return (int)Math.Round(Y * 31.0 + X, 0); // 31 = some prime number
}
public static bool operator ==(Point2d a1, Point2d a2)
{
return a1.Equals(a2);
}
public static bool operator !=(Point2d a1, Point2d a2)
{
return !a1.Equals(a2);
}
#endregion
}
public class Pixel2d : Point2d, IEquatable<Pixel2d>
{
public int State { get; set; }
public Pixel2d(): this(0,0,0)
{
}
public Pixel2d(int x, int y, int State) : base(x, y)
{
this.State = State;
}
#region equality comparison implementations
public override bool Equals(object other)
{
if (!(other is Pixel2d)) return false;
return Equals((Pixel2d)other);
}
public bool Equals(Pixel2d other)
{
return Math.Round(X, 2) == Math.Round(other.X, 2)
&& Math.Round(Y, 2) == Math.Round(other.Y, 2)
&& State == other.State;
}
public override int GetHashCode()
{
return (int)Math.Round(Y * 31.0 + X, 0); // 31 = some prime number
}
public static bool operator ==(Pixel2d a1, Pixel2d a2)
{
return a1.Equals(a2);
}
public static bool operator !=(Pixel2d a1, Pixel2d a2)
{
return !a1.Equals(a2);
}
#endregion
}
For example:
- Is it possible to merge
IEquatable<>
implementations into a single generic function or separate them into another class? - Is there any better way to write '
Point2d
andPixel2d
parameterized constructors? - What else can be made tidier?