Advertisement
StreetKatya

class

Dec 21st, 2022
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Shapes;
  8. using System.Windows.Controls;
  9. using System.Windows.Media;
  10. using System.Windows.Input;
  11.  
  12. namespace FiguresLib
  13. {
  14. #region Interfaces
  15. public struct MyPoint
  16. {
  17. public double X;
  18. public double Y;
  19. public MyPoint(double x, double y)
  20. {
  21. X = x; Y = y;
  22. }
  23. }
  24. interface IDeletable
  25. {
  26. bool Delete(MyPoint p);
  27. }
  28. interface ITriangle
  29. {
  30. bool CheckCrossPointWithTriangle(MyPoint p);
  31. }
  32. interface ISquare
  33. {
  34. bool CheckCrossPointWithSquare(MyPoint p);
  35. }
  36. interface ICircle
  37. {
  38. bool CheckCrossPointWithCircle(MyPoint p);
  39. }
  40. #endregion
  41. public abstract class BaseFigure: IDeletable
  42. {
  43. #region Поля
  44. private MyPoint _mousePos;
  45. public MyPoint mousePos
  46. {
  47. get { return _mousePos; }
  48. set { _mousePos = value; }
  49. }
  50. private Color _color;
  51. public Color color
  52. {
  53. get { return _color; }
  54. set { _color = value; }
  55. }
  56. public int size;
  57. #endregion
  58. public abstract bool Delete(MyPoint point);
  59. protected BaseFigure(MyPoint mousePos, Color color, int size)
  60. {
  61. this.mousePos = mousePos;
  62. this.color = color;
  63. this.size = size;
  64. }
  65. }
  66. public class Triangle : BaseFigure, ITriangle
  67. {
  68. #region Поля
  69. private MyPoint _point1, _point2, _point3;
  70. public MyPoint point1 { get { return _point1; } set { _point1 = value; } }
  71. public MyPoint point2 { get { return _point2; } set { _point2 = value; } }
  72. public MyPoint point3 { get { return _point3; } set { _point3 = value; } }
  73. #endregion
  74. public Triangle(MyPoint mousePos, Color color, int size) : base(mousePos, color, size)
  75. {
  76. this.mousePos = mousePos;
  77. this.color = color;
  78. this.size = size;
  79.  
  80. double h = Math.Sqrt((size * size) / 12);
  81. point1 = new MyPoint(mousePos.X, (int)(mousePos.Y - ((size*Math.Sqrt(3))/3)));
  82. point2 = new MyPoint(mousePos.X - size / 2, (int)(mousePos.Y + h));
  83. point3 = new MyPoint(mousePos.X + size / 2, (int)(mousePos.Y + h));
  84. }
  85.  
  86. public bool CheckCrossPointWithTriangle(MyPoint p)
  87. {
  88. double checkMult1 = (point1.X-p.X)*(point2.Y-point1.Y)-(point2.X-point1.X)*(point1.Y-p.Y);
  89. double checkMult2 = (point2.X - p.X) * (point3.Y - point2.Y) - (point3.X - point2.X) * (point2.Y - p.Y);
  90. double checkMult3 = (point3.X - p.X) * (point1.Y - point3.Y) - (point1.X - point3.X) * (point3.Y - p.Y);
  91. if ((checkMult1 < 0 && checkMult2 < 0 && checkMult3 < 0) ||
  92. (checkMult1 > 0 && checkMult2 > 0 && checkMult3 > 0) ||
  93. checkMult1 == 0 || checkMult2 == 0 || checkMult3 == 0)
  94. {
  95. return true;
  96. }
  97. else return false;
  98. }
  99.  
  100. public override bool Delete(MyPoint point)
  101. {
  102. if (CheckCrossPointWithTriangle(point))
  103. {
  104. return true;
  105. }
  106. else return false;
  107. }
  108. }
  109. public class Square : BaseFigure, ISquare
  110. {
  111. #region Поля
  112. private MyPoint _point1, _point2, _point3, _point4;
  113. public MyPoint point1 { get { return _point1; } set { _point1 = value; } }
  114. public MyPoint point2 { get { return _point2; } set { _point2 = value; } }
  115. public MyPoint point3 { get { return _point3; } set { _point3 = value; } }
  116. public MyPoint point4 { get { return _point4; } set { _point4 = value; } }
  117. #endregion
  118. public Square(MyPoint mousePos, Color color, int size) : base(mousePos, color, size)
  119. {
  120. this.mousePos = mousePos;
  121. this.color = color;
  122. this.size = size;
  123.  
  124. int halfSize = size/2;
  125. point1 = new MyPoint(mousePos.X - halfSize, mousePos.Y - halfSize);
  126. point2 = new MyPoint(mousePos.X + halfSize, mousePos.Y - halfSize);
  127. point3 = new MyPoint(mousePos.X + halfSize, mousePos.Y + halfSize);
  128. point4 = new MyPoint(mousePos.X - halfSize, mousePos.Y + halfSize);
  129. }
  130.  
  131. public bool CheckCrossPointWithSquare(MyPoint p)
  132. {
  133. if(p.X >= point1.X && p.Y >= point1.Y && p.X <= point3.X && p.Y <= point3.Y)
  134. {
  135. return true;
  136. }
  137. else return false;
  138. }
  139.  
  140. public override bool Delete(MyPoint point)
  141. {
  142. if(CheckCrossPointWithSquare(point))
  143. {
  144. return true;
  145. }
  146. else return false;
  147. }
  148. }
  149. public class Circle : BaseFigure, ICircle
  150. {
  151. public Circle(MyPoint mousePos, Color color, int size) : base(mousePos, color, size)
  152. {
  153. this.mousePos = mousePos;
  154. this.color = color;
  155. this.size = size;
  156. }
  157. public bool CheckCrossPointWithCircle(MyPoint p)
  158. {
  159. if(Math.Pow(p.X, 2) + Math.Pow(p.Y, 2) <= Math.Pow(size,2))
  160. {
  161. return true;
  162. }
  163. return false;
  164. }
  165.  
  166. public override bool Delete(MyPoint point)
  167. {
  168. if (CheckCrossPointWithCircle(point))
  169. {
  170. return true;
  171. }
  172. else return false;
  173. }
  174. }
  175. }
  176.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement