How do I create a rectangle using the Line class in c#?

Ella McGillivray 26 Reputation points
2020-12-11T19:45:53.18+00:00

How do I create a rectangle using the Line class in c#? The output of this code should show 4 points all connected together by lines! Please fill in where it says //TODO. Also include the Main() that way it will show the rectangle. Thank you!

namespace Rectangle {
class Rectangle {

    //TODO CREATE A PRIVATE ARRAY OF 4 LINES
    private Rectangle[] lines = new Rectangle[4];
    //TODO CREATE PRIVATE FIELD & GET ONLY PROPERTY FOR THE AREA
    private int mArea;
    public int Area { get; }
    public Rectangle(int x, int y, int length, int width) {
        lines[0] = new Rectangle(x, y, x + width, y);
        lines[1] = new Rectangle(x + width, y, x + width, y + length);
        lines[2] = new Rectangle(x + width, y + length, x, y + length);
        lines[3] = new Rectangle(x, y + length, x, y);
    }//end 
    // . . .


    public Rectangle(Point p0, int length, int width) {

    }//end constructor  

    public Rectangle(Point p0, Point p1, Point p2, Point p3) {
        //TODO
    }//end constructor                

    public Rectangle(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
        //TODO
    }//end constructor


    //MOVES RECTANGLE TO A NEW POSITION
    public void Move(int newX, int NewY) {
        //TODO
    }

    //CHANGES DIMENSIONS OF THE RECT
    public void Reform(int x1, int y1, int length, int width) {

    }

    public void Reform(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {

    }

    public void Draw() {

    }//end method

}//end class

}//end namespace

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,288 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,576 Reputation points
    2020-12-14T06:43:30.903+00:00

    According to your description, you need four lines to form a rectangle, but in the code, you create a rectangle array. Maybe it should be an line array?

    You can write a line class yourself:

    class Line  
    {  
        public int X { get; set; }  
        public int Y { get; set; }  
        public int X2 { get; set; }  
        public int Y2 { get; set; }  
        public Line(int x1, int y1, int x2, int y2)  
        {  
            X = x1;  
            Y = y1;  
            X2 = x2;  
            Y2 = y2;  
        }  
        public Line(Point startPoint, Point endPoint) : this(startPoint.X, startPoint.Y, endPoint.X, endPoint.Y) { }  
    }  
    

    One thing to note is that the figure composed of four points is not necessarily a rectangle, it may be an irregular quadrilateral. You can try to use the following code to determine whether the shape is a rectangle:

           public bool isRectangle(int x1, int y1,  
                     int x2, int y2,  
                     int x3, int y3,  
                     int x4, int y4)  
            {  
                double cx, cy;  
                double dd1, dd2, dd3, dd4;  
      
                cx = (x1 + x2 + x3 + x4) / 4;  
                cy = (y1 + y2 + y3 + y4) / 4;  
      
                dd1 = Math.Pow((cx - x1),2) + Math.Pow((cy - y1),2);  
                dd2 = Math.Pow((cx - x2),2) + Math.Pow((cy - y2),2);  
                dd3 = Math.Pow((cx - x3),2) + Math.Pow((cy - y3),2);  
                dd4 = Math.Pow((cx - x4),2) + Math.Pow((cy - y4),2);  
                return dd1 == dd2 && dd1 == dd3 && dd1 == dd4;  
            }  
    

    After this, the code in the constructor is the code written by Viorel-1. Now that you know the two points, you can directly create the line object.

            public Rectangle(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)  
            {  
                if (!isRectangle(x1,y1,x2,y2,x3,y3,x4,y4))  
                {  
                    //do something  
                }  
                else  
                {  
                    lines[0] = new Line(x1, y1, x2, y2);  
                    lines[1] = new Line(x1, y1, x3, y3);  
                    lines[2] = new Line(x2, y2, x4, y4);  
                    lines[3] = new Line(x3, y3, x4, y4);  
                }  
            }  
    

    The remaining constructor only needs to call this constructor in the code, just like what I did in the Line class.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments