Hi,
I know that the formula for finding the area of a rectangle is just length * width, and the peremiter formula is 2(length) + 2(width). My question is, whats the most efficient way to find the area and perimeter of a rectangle object made up of other objects?
My Code Snippet:
class Rectangle
{
public Line left { get; set; }
public Line top { get; set; }
public Line right { get; set; }
public Line bottom { get; set; }
public Rectangle() : this(new Line(new Point(), new Point())) { }
public Rectangle(Line diaganol)
{
Point beginningDiagonalPoint = diaganol.startPoint;
Point endingDiagonalPoint = diaganol.endPoint;
int begXC = beginningDiagonalPoint.xCoord;
int begYC = beginningDiagonalPoint.yCoord;
int endXC = endingDiagonalPoint.xCoord;
int endYC = endingDiagonalPoint.yCoord;
Point rightSideEnd = new Point(endXC, begYC);
Point leftSideEnd = new Point(begXC, endYC);
right = new Line(endingDiagonalPoint, rightSideEnd);
left = new Line(beginningDiagonalPoint, leftSideEnd);
top = new Line(leftSideEnd, endingDiagonalPoint);
bottom = new Line(rightSideEnd, beginningDiagonalPoint);
}
}
I want to write two methods, one to calculate the area, and one to caclulate the perimeter, how should I approach this with objects?
I know I could take the xfinal coord - xinitial coord for width, and yfinal - yinitial for the length, but is there another and/or better way of doing this with objects?
Thanks!