-
Notifications
You must be signed in to change notification settings - Fork 288
/
Rectangle.cs
48 lines (37 loc) · 1.31 KB
/
Rectangle.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Collections.Generic;
namespace Advanced.Algorithms.Geometry;
/// <summary>
/// Rectangle object.
/// </summary>
public class Rectangle
{
public Rectangle()
{
}
public Rectangle(Point leftTop, Point rightBottom)
{
if (rightBottom.Y > leftTop.Y) throw new Exception("Top corner should have higher Y value than bottom.");
if (leftTop.X > rightBottom.X) throw new Exception("Right corner should have higher X value than left.");
LeftTop = leftTop;
RightBottom = rightBottom;
}
public Point LeftTop { get; set; }
public Point RightBottom { get; set; }
internal double Length => Math.Abs(RightBottom.X - LeftTop.X);
internal double Breadth => Math.Abs(LeftTop.Y - RightBottom.Y);
internal double Area()
{
return Length * Breadth;
}
public Polygon ToPolygon()
{
var edges = new List<Line>();
//add all four edge lines of this rectangle
edges.Add(new Line(LeftTop, new Point(RightBottom.X, LeftTop.Y)));
edges.Add(new Line(new Point(RightBottom.X, LeftTop.Y), RightBottom));
edges.Add(new Line(RightBottom, new Point(LeftTop.X, RightBottom.Y)));
edges.Add(new Line(new Point(LeftTop.X, RightBottom.Y), LeftTop));
return new Polygon(edges);
}
}