Создание класса треугольник и сопутствующих методов - C#

Узнай цену своей работы

Формулировка задачи:

Описать класс, представляющий треугольник. Предусмотреть методы для создания объектов, перемещения на плоскости, изменения размеров и вращения на заданный угол. Описать свойства для получения состояния объекта. При невозможности построения треугольника выбрасывается исключение Подскажите, люди добрые. Застрял уже после написания конструктора

Решение задачи: «Создание класса треугольник и сопутствующих методов»

textual
Листинг программы
  1. using System;
  2. using System.Drawing.Drawing2D;
  3.  
  4. struct Point2F : IEquatable<Point2F>
  5. {
  6.     public readonly float X;
  7.     public readonly float Y;
  8.  
  9.     public Point2F(float X, float Y)
  10.     {
  11.         this.X = X;
  12.         this.Y = Y;
  13.     }
  14.  
  15.     public static float Length(Point2F obj1, Point2F obj2)
  16.     {
  17.         return (float)Math.Sqrt((obj2.X - obj1.X) * (obj2.X - obj1.X) + (obj2.Y - obj1.Y) * (obj2.Y - obj1.Y));
  18.     }
  19.  
  20.     public override bool Equals(object obj)
  21.     {
  22.         if (obj is Point2F)
  23.             return Equals((Point2F)obj);
  24.         return base.Equals(obj);
  25.     }
  26.  
  27.     public static bool operator ==(Point2F first, Point2F second)
  28.     {
  29.         if ((object)first == null)
  30.             return (object)second == null;
  31.         return first.Equals(second);
  32.     }
  33.  
  34.     public static bool operator !=(Point2F first, Point2F second)
  35.     {
  36.         return !(first == second);
  37.     }
  38.  
  39.     public bool Equals(Point2F other)
  40.     {
  41.         if (ReferenceEquals(null, other))
  42.             return false;
  43.         if (ReferenceEquals(this, other))
  44.             return true;
  45.         return this.X.Equals(other.X) && this.Y.Equals(other.Y);
  46.     }
  47.  
  48.     public override int GetHashCode()
  49.     {
  50.         unchecked
  51.         {
  52.             int hashCode = 47;
  53.             hashCode = (hashCode * 53) ^ X.GetHashCode();
  54.             hashCode = (hashCode * 53) ^ Y.GetHashCode();
  55.             return hashCode;
  56.         }
  57.     }
  58.  
  59.     public override string ToString()
  60.     {
  61.         return $"({this.X}, {this.Y})";
  62.     }
  63. }
  64.  
  65. class Triangle
  66. {
  67.     public Point2F A { get; private set; }
  68.     public Point2F B { get; private set; }
  69.     public Point2F C { get; private set; }
  70.  
  71.     public Triangle(Point2F A, Point2F B, Point2F C)
  72.     {
  73.         this.A = A;
  74.         this.B = B;
  75.         this.C = C;
  76.  
  77.         float lenAB = Point2F.Length(this.A, this.B);
  78.         float lenBC = Point2F.Length(this.B, this.C);
  79.         float lenAC = Point2F.Length(this.A, this.C);
  80.  
  81.         if (!(lenAB < lenAC + lenBC || lenAC < lenAB + lenBC || lenBC < lenAB + lenAC))
  82.             throw new ArgumentException("Triangle does not exist.");
  83.     }
  84.  
  85.     public Triangle Scale(float scaleX, float scaleY)
  86.     {
  87.         this.A = this.scale(this.A, scaleX, scaleY);
  88.         this.B = this.scale(this.B, scaleX, scaleY);
  89.         this.C = this.scale(this.C, scaleX, scaleY);
  90.  
  91.         return this;
  92.     }
  93.  
  94.     public Triangle Rotate(float angle)
  95.     {
  96.         this.A = this.rotate(this.A, angle);
  97.         this.B = this.rotate(this.B, angle);
  98.         this.C = this.rotate(this.C, angle);
  99.  
  100.         return this;
  101.     }
  102.  
  103.     public Triangle Shift( float dx, float dy)
  104.     {
  105.         this.A = this.shift(this.A, dx, dy);
  106.         this.B = this.shift(this.B, dx, dy);
  107.         this.C = this.shift(this.C, dx, dy);
  108.  
  109.         return this;
  110.     }
  111.  
  112.     private Point2F shift( Point2F point, float dx, float dy)
  113.     {
  114.         return new Point2F(point.X + dx, point.Y + dy);
  115.     }
  116.  
  117.     private Point2F rotate(Point2F point, float angle)
  118.     {
  119.         var pointToMatrix = convertPointToMatrix(point);
  120.         pointToMatrix.Rotate(angle);
  121.  
  122.         return convertMatrixToPoint(pointToMatrix);
  123.     }
  124.  
  125.     private Point2F scale(Point2F point, float scaleX, float scaleY)
  126.     {
  127.         var pointToMatrix = convertPointToMatrix(point);
  128.         pointToMatrix.Scale(scaleX, scaleY);
  129.  
  130.         return convertMatrixToPoint(pointToMatrix);
  131.     }
  132.  
  133.     private Matrix convertPointToMatrix(Point2F point)
  134.     {
  135.         return new Matrix(point.X, 0f, point.Y, 0, 0, 0);
  136.     }
  137.  
  138.     private Point2F convertMatrixToPoint(Matrix matrix)
  139.     {
  140.         return new Point2F(matrix.Elements[0], matrix.Elements[2]);
  141.     }
  142.  
  143.     public override string ToString()
  144.     {
  145.         return $"{this.A}, {this.B}, {this.C}";
  146.     }
  147. }
  148.  
  149. class Program
  150. {
  151.     static void Main()
  152.     {
  153.         Triangle abc = new Triangle(new Point2F(1, 1), new Point2F(2, 2), new Point2F(0, 2));
  154.         abc.Shift(1, 1).Scale(2, 2).Rotate(45);
  155.  
  156.         Console.WriteLine(abc);
  157.     }
  158. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

14   голосов , оценка 3.714 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы