Создание класса треугольник и сопутствующих методов - C#
Формулировка задачи:
Описать класс, представляющий треугольник. Предусмотреть методы для создания объектов, перемещения на плоскости, изменения размеров и вращения на заданный угол. Описать свойства для получения состояния объекта. При невозможности построения треугольника выбрасывается исключение
Подскажите, люди добрые. Застрял уже после написания конструктора
Решение задачи: «Создание класса треугольник и сопутствующих методов»
textual
Листинг программы
using System;
using System.Drawing.Drawing2D;
struct Point2F : IEquatable<Point2F>
{
public readonly float X;
public readonly float Y;
public Point2F(float X, float Y)
{
this.X = X;
this.Y = Y;
}
public static float Length(Point2F obj1, Point2F obj2)
{
return (float)Math.Sqrt((obj2.X - obj1.X) * (obj2.X - obj1.X) + (obj2.Y - obj1.Y) * (obj2.Y - obj1.Y));
}
public override bool Equals(object obj)
{
if (obj is Point2F)
return Equals((Point2F)obj);
return base.Equals(obj);
}
public static bool operator ==(Point2F first, Point2F second)
{
if ((object)first == null)
return (object)second == null;
return first.Equals(second);
}
public static bool operator !=(Point2F first, Point2F second)
{
return !(first == second);
}
public bool Equals(Point2F other)
{
if (ReferenceEquals(null, other))
return false;
if (ReferenceEquals(this, other))
return true;
return this.X.Equals(other.X) && this.Y.Equals(other.Y);
}
public override int GetHashCode()
{
unchecked
{
int hashCode = 47;
hashCode = (hashCode * 53) ^ X.GetHashCode();
hashCode = (hashCode * 53) ^ Y.GetHashCode();
return hashCode;
}
}
public override string ToString()
{
return $"({this.X}, {this.Y})";
}
}
class Triangle
{
public Point2F A { get; private set; }
public Point2F B { get; private set; }
public Point2F C { get; private set; }
public Triangle(Point2F A, Point2F B, Point2F C)
{
this.A = A;
this.B = B;
this.C = C;
float lenAB = Point2F.Length(this.A, this.B);
float lenBC = Point2F.Length(this.B, this.C);
float lenAC = Point2F.Length(this.A, this.C);
if (!(lenAB < lenAC + lenBC || lenAC < lenAB + lenBC || lenBC < lenAB + lenAC))
throw new ArgumentException("Triangle does not exist.");
}
public Triangle Scale(float scaleX, float scaleY)
{
this.A = this.scale(this.A, scaleX, scaleY);
this.B = this.scale(this.B, scaleX, scaleY);
this.C = this.scale(this.C, scaleX, scaleY);
return this;
}
public Triangle Rotate(float angle)
{
this.A = this.rotate(this.A, angle);
this.B = this.rotate(this.B, angle);
this.C = this.rotate(this.C, angle);
return this;
}
public Triangle Shift( float dx, float dy)
{
this.A = this.shift(this.A, dx, dy);
this.B = this.shift(this.B, dx, dy);
this.C = this.shift(this.C, dx, dy);
return this;
}
private Point2F shift( Point2F point, float dx, float dy)
{
return new Point2F(point.X + dx, point.Y + dy);
}
private Point2F rotate(Point2F point, float angle)
{
var pointToMatrix = convertPointToMatrix(point);
pointToMatrix.Rotate(angle);
return convertMatrixToPoint(pointToMatrix);
}
private Point2F scale(Point2F point, float scaleX, float scaleY)
{
var pointToMatrix = convertPointToMatrix(point);
pointToMatrix.Scale(scaleX, scaleY);
return convertMatrixToPoint(pointToMatrix);
}
private Matrix convertPointToMatrix(Point2F point)
{
return new Matrix(point.X, 0f, point.Y, 0, 0, 0);
}
private Point2F convertMatrixToPoint(Matrix matrix)
{
return new Point2F(matrix.Elements[0], matrix.Elements[2]);
}
public override string ToString()
{
return $"{this.A}, {this.B}, {this.C}";
}
}
class Program
{
static void Main()
{
Triangle abc = new Triangle(new Point2F(1, 1), new Point2F(2, 2), new Point2F(0, 2));
abc.Shift(1, 1).Scale(2, 2).Rotate(45);
Console.WriteLine(abc);
}
}