CtrEditor/ObjetosSim/UserControls/GearControl.xaml.cs

130 lines
4.7 KiB
C#
Raw Permalink Normal View History

2024-05-25 07:53:34 -03:00
using System.Windows;
2024-05-23 14:56:14 -03:00
using System.Windows.Controls;
using System.Windows.Media;
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);
2024-05-25 07:53:34 -03:00
// Draw the zero angle marker
double zeroAngle = 0;
double radianZero = zeroAngle * Math.PI / 180;
double markerLength = InnerRadius * 1.2; // Length of the marker line extending outside the inner circle
Line zeroAngleMarker = new Line
{
X1 = centerX,
Y1 = centerY,
X2 = centerX + markerLength * Math.Cos(radianZero),
Y2 = centerY - markerLength * Math.Sin(radianZero),
Stroke = Brushes.Red,
StrokeThickness = 2
};
GearCanvas.Children.Add(zeroAngleMarker);
2024-05-23 14:56:14 -03:00
for (int i = 0; i < TeethCount; i++)
{
2024-05-25 07:53:34 -03:00
// Offset the angle to center the first tooth on the 0 angle
2024-05-23 14:56:14 -03:00
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
{
2024-05-25 07:53:34 -03:00
Fill = Brushes.Black,
2024-05-23 14:56:14 -03:00
Points = new PointCollection { p1, p2, p3, p4 }
};
GearCanvas.Children.Add(tooth);
}
}
2024-05-25 07:53:34 -03:00
2024-05-23 14:56:14 -03:00
}
}