locked
Struggling with C# RRS feed

  • Question

  • I am struggling to create a class that stores 4 cartesian coordinates for the corners of a rectange. The method verifies if it is a rectangle or a square. read only properties that get the length, width, area, and perimeter ((BUT NOT USING AN INSTANCE VARIABLE)) Without using the variable is giving me a very hard time. 

    Saturday, April 12, 2008 2:44 AM

Answers

  • Why aren't you just using the Rectangle class?
     
    Are you looking for a class, not a struct?
     
    Try something like this:
     

    Code Snippet

    public class MyRectangle

    {

    public int Height { get { return Math.Abs(_y1 - _y2); } }

    public int Width { get { return Math.Abs(_x1 - _x2); } }

    public int Area { get { return Math.Abs(_y1 - _y2) * Math.Abs(_x1 - _x2); } }

    public int Perimeter { get { return (Math.Abs(_x1 - _x2) + Math.Abs(_y1 - _y2)) * 2; } }

    public int X1 { get { return _x1; } }

    public int X2 { get { return _x2; } }

    public int Y1 { get { return _y1; } }

    public int Y2 { get { return _y2; } }

    private int _x1;

    private int _x2;

    private int _y1;

    private int _y2;

     

    public MyRectangle(int x1, int y1, int x2, int y2)

    {

    _x1 = x1;

    _y1 = y1;

    _x2 = x2;

    _y2 = y2;

    }

    }

     

     

    Saturday, April 12, 2008 3:07 AM
    Moderator

All replies

  • Why aren't you just using the Rectangle class?
     
    Are you looking for a class, not a struct?
     
    Try something like this:
     

    Code Snippet

    public class MyRectangle

    {

    public int Height { get { return Math.Abs(_y1 - _y2); } }

    public int Width { get { return Math.Abs(_x1 - _x2); } }

    public int Area { get { return Math.Abs(_y1 - _y2) * Math.Abs(_x1 - _x2); } }

    public int Perimeter { get { return (Math.Abs(_x1 - _x2) + Math.Abs(_y1 - _y2)) * 2; } }

    public int X1 { get { return _x1; } }

    public int X2 { get { return _x2; } }

    public int Y1 { get { return _y1; } }

    public int Y2 { get { return _y2; } }

    private int _x1;

    private int _x2;

    private int _y1;

    private int _y2;

     

    public MyRectangle(int x1, int y1, int x2, int y2)

    {

    _x1 = x1;

    _y1 = y1;

    _x2 = x2;

    _y2 = y2;

    }

    }

     

     

    Saturday, April 12, 2008 3:07 AM
    Moderator
  •  robee31 wrote:

    I am struggling to create a class that stores 4 cartesian coordinates for the corners of a rectange. The method verifies if it is a rectangle or a square. read only properties that get the length, width, area, and perimeter ((BUT NOT USING AN INSTANCE VARIABLE)) Without using the variable is giving me a very hard time. 

     

    Well, are you still struggling? Here is a simple code that should make sure that four points thrown onto a plane are indeed the four corners of a rectangle. I did not do part of the conversion (from int to double, so you may get an exception. If I did it completely that would have obscrured the idea.

     

    class doRectangle

    {

    private bool isRectangle ( Point A, Point B, Point C, Point D )

    {

    // compute distances between all possible points. For four points

    // there are 6 possible distances

    double Dab = Math.Sqrt ( ( A.X - B.X ) ^ 2 + ( A.Y - B.Y ) ^ 2 );

    double Dac = Math.Sqrt ( ( A.X - C.X ) ^ 2 + ( A.Y - C.Y ) ^ 2 );

    double Dad = Math.Sqrt ( ( A.X - D.X ) ^ 2 + ( A.Y - D.Y ) ^ 2 );

    double Dbc = Math.Sqrt ( ( B.X - C.X ) ^ 2 + ( B.Y - C.Y ) ^ 2 );

    double Dbd = Math.Sqrt ( ( B.X - D.X ) ^ 2 + ( B.Y - D.Y ) ^ 2 );

    double Dcd = Math.Sqrt ( ( C.X - D.X ) ^ 2 + ( C.Y - D.Y ) ^ 2 );

    // make sure none of them is zero; otherwise return false;

    if ( Dab == 0 || Dac == 0 || Dad == 0 || Dbc == 0 || Dbd == 0 || Dcd == 0 )

    {

    return false;

    }

    // assuming that all Points are distinct, none of the distances are zero

    // the figure is a parallelogram if the opposite sides are equal

    if ( Dab == Dcd & Dbc == Dad )

    {

    // this condition is necessary but not sufficient,

    // you have to make sure that

    // the angles between adjacent sides are right

    // using the Pythagorean theorem again:

    if ( Dab ^ 2 + Dad ^ 2 == Dbd ^ 2 & Dab != Dad)

    {

    return true;

    }

    }

return false;

}

}

 

You can do it in a more elegant way if you wish, perhaps it will cover more general cases. You may consider the sides of the rectangle as vectors, Take a vector product (in coordinates) of the opposite sides, compute the determinant of the matrix (tensor of the second rank) and if it is zero, the sides are parallel. Compute the scalar product of the adjacent sides, if the result is zero, they are perpendicular. You will have to look the expressions up.

Tuesday, April 15, 2008 1:09 PM