Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Shapes;
- namespace LineAndCircleDrawingApp
- {
- public partial class MainWindow : Window
- {
- private Line currentLine;
- private Ellipse currentCircle;
- private Point startPoint;
- private bool isDrawingLine = false;
- private bool isDrawingCircle = false;
- public MainWindow()
- {
- InitializeComponent();
- }
- private void btnDrawLine_Click(object sender, RoutedEventArgs e)
- {
- if (currentCircle != null)
- {
- canvas.Children.Remove(currentCircle);
- currentCircle = null;
- }
- if (currentLine == null)
- {
- currentLine = new Line
- {
- Stroke = Brushes.Black,
- StrokeThickness = 2
- };
- canvas.Children.Add(currentLine);
- }
- isDrawingLine = true;
- isDrawingCircle = false;
- }
- private void btnDrawCircle_Click(object sender, RoutedEventArgs e)
- {
- if (currentLine != null)
- {
- canvas.Children.Remove(currentLine);
- currentLine = null;
- }
- if (currentCircle == null)
- {
- currentCircle = new Ellipse
- {
- Stroke = Brushes.Black,
- StrokeThickness = 2
- };
- canvas.Children.Add(currentCircle);
- }
- isDrawingCircle = true;
- isDrawingLine = false;
- }
- private void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- startPoint = e.GetPosition(canvas);
- if (isDrawingLine)
- {
- if (currentLine != null)
- {
- currentLine.X1 = startPoint.X;
- currentLine.Y1 = startPoint.Y;
- currentLine.X2 = startPoint.X;
- currentLine.Y2 = startPoint.Y;
- }
- }
- else if (isDrawingCircle)
- {
- if (currentCircle != null)
- {
- currentCircle.Width = 0;
- currentCircle.Height = 0;
- Canvas.SetLeft(currentCircle, startPoint.X);
- Canvas.SetTop(currentCircle, startPoint.Y);
- }
- }
- }
- private void canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- if (isDrawingLine)
- {
- if (currentLine != null)
- {
- Point endPoint = e.GetPosition(canvas);
- currentLine.X2 = endPoint.X;
- currentLine.Y2 = endPoint.Y;
- }
- }
- else if (isDrawingCircle)
- {
- if (currentCircle != null)
- {
- Point endPoint = e.GetPosition(canvas);
- double radiusX = endPoint.X - startPoint.X;
- double radiusY = endPoint.Y - startPoint.Y;
- double radius = System.Math.Sqrt(radiusX * radiusX + radiusY * radiusY);
- currentCircle.Width = 2 * radius;
- currentCircle.Height = 2 * radius;
- Canvas.SetLeft(currentCircle, startPoint.X - radius);
- Canvas.SetTop(currentCircle, startPoint.Y - radius);
- }
- }
- }
- private void canvas_MouseMove(object sender, MouseEventArgs e)
- {
- if (isDrawingLine)
- {
- if (currentLine != null && e.LeftButton == MouseButtonState.Pressed)
- {
- Point currentPoint = e.GetPosition(canvas);
- currentLine.X2 = currentPoint.X;
- currentLine.Y2 = currentPoint.Y;
- }
- }
- else if (isDrawingCircle)
- {
- if (currentCircle != null && e.LeftButton == MouseButtonState.Pressed)
- {
- Point currentPoint = e.GetPosition(canvas);
- double radiusX = currentPoint.X - startPoint.X;
- double radiusY = currentPoint.Y - startPoint.Y;
- double radius = System.Math.Sqrt(radiusX * radiusX + radiusY * radiusY);
- currentCircle.Width = 2 * radius;
- currentCircle.Height = 2 * radius;
- Canvas.SetLeft(currentCircle, startPoint.X - radius);
- Canvas.SetTop(currentCircle, startPoint.Y - radius);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement