Advertisement
programusy

Untitled

Sep 15th, 2023
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Input;
  4. using System.Windows.Media;
  5. using System.Windows.Shapes;
  6.  
  7. namespace LineAndCircleDrawingApp
  8. {
  9. public partial class MainWindow : Window
  10. {
  11. private Line currentLine;
  12. private Ellipse currentCircle;
  13. private Point startPoint;
  14. private bool isDrawingLine = false;
  15. private bool isDrawingCircle = false;
  16.  
  17. public MainWindow()
  18. {
  19. InitializeComponent();
  20. }
  21.  
  22. private void btnDrawLine_Click(object sender, RoutedEventArgs e)
  23. {
  24. if (currentCircle != null)
  25. {
  26. canvas.Children.Remove(currentCircle);
  27. currentCircle = null;
  28. }
  29.  
  30. if (currentLine == null)
  31. {
  32. currentLine = new Line
  33. {
  34. Stroke = Brushes.Black,
  35. StrokeThickness = 2
  36. };
  37. canvas.Children.Add(currentLine);
  38. }
  39. isDrawingLine = true;
  40. isDrawingCircle = false;
  41. }
  42.  
  43. private void btnDrawCircle_Click(object sender, RoutedEventArgs e)
  44. {
  45. if (currentLine != null)
  46. {
  47. canvas.Children.Remove(currentLine);
  48. currentLine = null;
  49. }
  50.  
  51. if (currentCircle == null)
  52. {
  53. currentCircle = new Ellipse
  54. {
  55. Stroke = Brushes.Black,
  56. StrokeThickness = 2
  57. };
  58. canvas.Children.Add(currentCircle);
  59. }
  60. isDrawingCircle = true;
  61. isDrawingLine = false;
  62. }
  63.  
  64. private void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  65. {
  66. startPoint = e.GetPosition(canvas);
  67.  
  68. if (isDrawingLine)
  69. {
  70. if (currentLine != null)
  71. {
  72. currentLine.X1 = startPoint.X;
  73. currentLine.Y1 = startPoint.Y;
  74. currentLine.X2 = startPoint.X;
  75. currentLine.Y2 = startPoint.Y;
  76. }
  77. }
  78. else if (isDrawingCircle)
  79. {
  80. if (currentCircle != null)
  81. {
  82. currentCircle.Width = 0;
  83. currentCircle.Height = 0;
  84. Canvas.SetLeft(currentCircle, startPoint.X);
  85. Canvas.SetTop(currentCircle, startPoint.Y);
  86. }
  87. }
  88. }
  89.  
  90. private void canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  91. {
  92. if (isDrawingLine)
  93. {
  94. if (currentLine != null)
  95. {
  96. Point endPoint = e.GetPosition(canvas);
  97. currentLine.X2 = endPoint.X;
  98. currentLine.Y2 = endPoint.Y;
  99. }
  100. }
  101. else if (isDrawingCircle)
  102. {
  103. if (currentCircle != null)
  104. {
  105. Point endPoint = e.GetPosition(canvas);
  106. double radiusX = endPoint.X - startPoint.X;
  107. double radiusY = endPoint.Y - startPoint.Y;
  108. double radius = System.Math.Sqrt(radiusX * radiusX + radiusY * radiusY);
  109.  
  110. currentCircle.Width = 2 * radius;
  111. currentCircle.Height = 2 * radius;
  112.  
  113. Canvas.SetLeft(currentCircle, startPoint.X - radius);
  114. Canvas.SetTop(currentCircle, startPoint.Y - radius);
  115. }
  116. }
  117. }
  118.  
  119. private void canvas_MouseMove(object sender, MouseEventArgs e)
  120. {
  121. if (isDrawingLine)
  122. {
  123. if (currentLine != null && e.LeftButton == MouseButtonState.Pressed)
  124. {
  125. Point currentPoint = e.GetPosition(canvas);
  126. currentLine.X2 = currentPoint.X;
  127. currentLine.Y2 = currentPoint.Y;
  128. }
  129. }
  130. else if (isDrawingCircle)
  131. {
  132. if (currentCircle != null && e.LeftButton == MouseButtonState.Pressed)
  133. {
  134. Point currentPoint = e.GetPosition(canvas);
  135. double radiusX = currentPoint.X - startPoint.X;
  136. double radiusY = currentPoint.Y - startPoint.Y;
  137. double radius = System.Math.Sqrt(radiusX * radiusX + radiusY * radiusY);
  138.  
  139. currentCircle.Width = 2 * radius;
  140. currentCircle.Height = 2 * radius;
  141.  
  142. Canvas.SetLeft(currentCircle, startPoint.X - radius);
  143. Canvas.SetTop(currentCircle, startPoint.Y - radius);
  144. }
  145. }
  146. }
  147. }
  148. }
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement