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.