I'm in the process of creating a program that can model points, vectors, lines and segments. I have the classes built and confirmed to be working correctly. The classes are built to handle multiple dimensions, so as an example, the same point class is used for R1, R2, R3, ... Rn.
The issue that I am dealing with is how to handle operations between classes if the dimensions are different. For example, you can add two vectors that are in R2 but you cannot add a vector that is in R2 and another in R3.
My original plan was to create an exception class that I would throw if the dimensions between the two objects was different. This would work but would entail needing to wrap a lot of operations in try/catch blocks. Is this the best course of action to take or is there a better method that i could impliment?
Below is my point class:
public class Point
{
private List<double> values;
public List<double> VALUES
{
get { return values; }
set { values = value; }
}
public int DIMENSION
{
get { return VALUES.Count; }
}
public Point(List<double> args)
{
values = new List<double>();
this.VALUES = args;
}
public override string ToString()
{
string temp = "( ";
for (int i = 0; i < DIMENSION; i++)
{
temp += VALUES[i];
if(i != DIMENSION - 1)
temp += ", ";
}
temp += ");";
return temp;
}
public static double Distance(Point point1, Point point2)
{
if (!SameDimension(point1, point2))
{
//add exception
}
double temp = 0;
for (int i = 0; i < point1.DIMENSION; i++)
{
temp += Math.Pow(point2.VALUES[i] - point1.VALUES[i], 2);
}
return Math.Sqrt(temp);
}
public static Point Midpoint(Point point1, Point point2)
{
if (!SameDimension(point1, point2))
{
//add exception
}
List<double> temp = new List<double>();
for (int i = 0; i < point1.DIMENSION; i++)
{
temp.Add((point2.VALUES[i] + point1.VALUES[i]) / 2);
}
return new Point(temp);
}
//This is the method used to check dimensions
public static bool SameDimension(Point point1, Point point2)
{
if (point1.DIMENSION != point2.DIMENSION)
return false;
else
return true;
}
}//working, needs exceptions added
This is the most simple class but all check the dimension by using this meathod.
EDIT: The reason for needing to encase operations in try/catch blocks is say I have:
Point p1 = new Point(new List<double> { 0, 0});
Point p2 = new Point(new List<double> { 3, 3});
Point p3 = new Point(new List<double> { 4, 4, 4});
double distance = Point.Distance(p1, p2);
This would work completely fine, giving the distance of 3sqrt(3). But if this was tried:
distance = Point.Distance(p1,p3);
An error should occur because you are trying to compare a point in 2-Dimensions to a point in 3-Dimensions. Code will be written to reduce the risk of this happening, but I want a safe-guard in place in case this situation arises. If I go with the exception route, try/catch blocks will be needed. This is more apparent in the Vector class where when you add two vectors a new vector is returned. In that case, if a dimension error is encountered, the return value would be null, causing the possibility of other errors. Using exceptions, in my opinion, would mitigate potential errors as well as give me a capacity to track when they occur during run-time.