tags:

views:

522

answers:

5

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!

A: 

Your perimeter would be the sum of the lengths of the four lines. Presumably the Line objects have or will be able to have a length method.

Similarly, the area is the product of length of the right or left and top or bottom lines.

ysth
+2  A: 

The area of a rectangle will always be:

area = width * height

The perimeter will always be:

perimeter = width * 2 + height * 2

It doesn't matter how you arrive at width or height, it will always be the same.

Lasse V. Karlsen
+1  A: 

Your class constructors define a rectangle, but since the Lines are unprotected, outside code could change them to be parallelograms, other quadrilaterals, or just disjointed lines.

You're on the right track, though--your Rectangle class should be implementing Area and Perimeter methods, that's the "right way" when doing OOP.

You don't need the lines to calculate Area or Perimeter, you only need the original diagonal Points. In fact, you don't need left/top/right/bottom at all, so it might be better to make them lazy-initialized, read-only properties.

Area = Math.Abs( (Point1.X - Point2.X) * (Point1.Y - Point2.Y) )

Perimeter = (Math.Abs(Point1.X - Point2.X) + Math.Abs(Point1.Y - Point2.Y)) * 2
richardtallent
defining the quad by its diagonal can only describe a rectangle
Steve De Caux
A: 

Your perimeter is (endXC - begXC)*2 + (endYC - begYC) *2

Your area is (endXC - begXC) * (endYC - begYC)

...assuming the points are ordered in the poitive x and y directions (otherwise enclose each calc in abs e.g. abs(endXC - begXC)

if area and circumference are likely to be called, you might want to calculate & store them on init

Steve De Caux
A: 

If you look at your class, your wasting more time in the constructor of the class than you'll ever lose to calculate the area and perimeter. Why do you store lines? Points are way more convenient:

class Rectangle
{

public Point bottomLeft { get; set; }
public Point topRight { get; set; }
public int? area;
public int? perimeter;

public Rectangle() : this(new Line(new Point(), new Point())) { }

public Rectangle(Line diaganol)
{
    bottomLeft = diagonal.StartPoint;
    topRight = diagonal.EndPoint;
}

public int Area()
{
  if (area == null)
  {
     area = Math.Abs(diagonal.StartPoint.X - diagonal.EndPoint.X) *
            Math.Abs(diagonal.StartPoint.Y - diagonal.EndPoint.Y);
  } 
  return (int)area;
}
public int Perimeter()
{
  if (perimeter == null)
  {
     perimeter = Math.Abs(diagonal.StartPoint.X - diagonal.EndPoint.X) * 2 +
            Math.Abs(diagonal.StartPoint.Y - diagonal.EndPoint.Y) * 2;
  } 
  return (int)perimeter;
}
}
Jorge Córdoba