А что замочную скважину может заесть при низких температурах, с этим все сталкивались.
struct Point
{
public double X { get; set; }
public double Y { get; set; }
public double GetDistance(Point otherPoint)
{
return Math.Sqrt(
Math.Pow(otherPoint.X - this.X, 2)
+ Math.Pow(otherPoint.Y - this.Y, 2)
);
}
}
struct Quadrilateral
{
public Quadrilateral( Point leftTop, Point rightTop,
Point leftBottom, Point rightBottom )
{
_leftTop = leftTop;
_rightTop = rightTop;
_rightBottom = rightBottom;
_leftBottom = leftBottom;
}
public virtual double GetPerimeter ()
{
return _leftTop.GetDistance(_rightTop)
+ _rightTop.GetDistance(_rightBottom)
+ _rightBottom.GetDistance(_leftBottom)
+ _leftBottom.GetDistance(_leftTop);
}
protected Point _leftTop;
protected Point _rightTop;
protected Point _leftBottom;
protected Point _rightBottom;
}
struct Line
{
public Line (Point p1, Point p2)
{
if (p1 == p2) throw new InvalidArgumentException("Это не линия!");
_point1 = p1;
_point2 = p2;
}
public double GetAngleWith(Line otherLine)
{
double a1 = _point2.Y - _point1.Y;
double b1 = _point2.X - _point1.X;
double a2 = otherLine._point2.Y - otherLine._point1.Y;
double b2 = otherLine._point2.X - otherLine._point1.X;
return Math.Acos( ( b1 * b2 + a1 * a2 )
/ Math.Sqrt( Math.Pow(b1,2) + Math.Pow(a1,2) )
* Math.Sqrt( Math.Pow(b2,2) + Math.Pow(a2,2) )
);
}
private Point _point1;
private Point _point2;
}
struct Square: Quadrilateral
{
public Square( Point leftTop, Point rightTop,
Point leftBottom, Point rightBottom)
: base(leftTop, rightTop, leftBottom, rightBottom)
{
if ( !IsShapeValid() )
{
throw new InvalidArgumentException("Это не квадрат");
}
}
public override double GetPerimeter()
{
return _leftTop.GetDistance(_rightTop) * 4.;
}
protected bool IsShapeValid()
{
Func<Line, Line, bool> checkAngle => (l1, l2) => l1.GetAngleWith(l2) - Math.Pi/2. < 0.01;
var top = new Line(_leftTop, _rightTop);
var right = new Line(_rightTop, _rightBottom);
var bottom = new Line(_leftBottom, _rightBottom);
var left = new Line(_leftTop, _leftBottom);
return checkAngle(top, right)
&& checkAngle(bottom, right)
&& checkAngle(left, bottom)
&& checkAngle(left, top)
}
}
не надо будет пилить свои велосипеды, если не хватает финансов.