CtrEditor/ObjetosSim/UserControls/GearControl.xaml.cs

121 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace CtrEditor.ObjetosSim.UserControls
{
/// <summary>
/// Interaction logic for GearControl.xaml
/// </summary>
public partial class GearControl : UserControl
{
public static readonly DependencyProperty TeethCountProperty =
DependencyProperty.Register("TeethCount", typeof(int), typeof(GearControl),
new PropertyMetadata(10, OnGearPropertyChanged));
public static readonly DependencyProperty InnerRadiusProperty =
DependencyProperty.Register("InnerRadius", typeof(double), typeof(GearControl),
new PropertyMetadata(40.0, OnGearPropertyChanged));
public static readonly DependencyProperty OuterRadiusProperty =
DependencyProperty.Register("OuterRadius", typeof(double), typeof(GearControl),
new PropertyMetadata(60.0, OnGearPropertyChanged));
public static readonly DependencyProperty ToothWidthRatioProperty =
DependencyProperty.Register("ToothWidthRatio", typeof(double), typeof(GearControl),
new PropertyMetadata(0.5, OnGearPropertyChanged));
public int TeethCount
{
get { return (int)GetValue(TeethCountProperty); }
set { SetValue(TeethCountProperty, value); }
}
public double InnerRadius
{
get { return (double)GetValue(InnerRadiusProperty); }
set { SetValue(InnerRadiusProperty, value); }
}
public double OuterRadius
{
get { return (double)GetValue(OuterRadiusProperty); }
set { SetValue(OuterRadiusProperty, value); }
}
public double ToothWidthRatio
{
get { return (double)GetValue(ToothWidthRatioProperty); }
set { SetValue(ToothWidthRatioProperty, value); }
}
public GearControl()
{
InitializeComponent();
DrawGear();
}
private static void OnGearPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as GearControl;
control?.DrawGear();
}
private void DrawGear()
{
GearCanvas.Children.Clear();
double centerX = ActualWidth / 2;
double centerY = ActualHeight / 2;
double angleStep = 360.0 / TeethCount;
double toothWidthAngle = angleStep * ToothWidthRatio;
// Draw inner circle
Ellipse innerCircle = new Ellipse
{
Width = InnerRadius * 2,
Height = InnerRadius * 2,
Stroke = Brushes.Black,
StrokeThickness = 2,
Fill = Brushes.Gray
};
Canvas.SetLeft(innerCircle, centerX - InnerRadius);
Canvas.SetTop(innerCircle, centerY - InnerRadius);
GearCanvas.Children.Add(innerCircle);
for (int i = 0; i < TeethCount; i++)
{
double angle = i * angleStep;
double radianStart = (angle - toothWidthAngle / 2) * Math.PI / 180;
double radianEnd = (angle + toothWidthAngle / 2) * Math.PI / 180;
Point p1 = new Point(centerX + InnerRadius * Math.Cos(radianStart), centerY + InnerRadius * Math.Sin(radianStart));
Point p2 = new Point(centerX + OuterRadius * Math.Cos(radianStart), centerY + OuterRadius * Math.Sin(radianStart));
Point p3 = new Point(centerX + OuterRadius * Math.Cos(radianEnd), centerY + OuterRadius * Math.Sin(radianEnd));
Point p4 = new Point(centerX + InnerRadius * Math.Cos(radianEnd), centerY + InnerRadius * Math.Sin(radianEnd));
Polygon tooth = new Polygon
{
Stroke = Brushes.Black,
StrokeThickness = 2,
Fill = Brushes.LightGray,
Points = new PointCollection { p1, p2, p3, p4 }
};
GearCanvas.Children.Add(tooth);
}
}
}
}