by lukiller:
6. March 2011 23:55
Un código matemático para dibujar un reloj analógico. Lo hice en WPF sobre un Canvas, pero puede dibujarse sobre cualquier contenedor:
<Canvas Name="ClockContainer" Height="200" Width="200" />
public void DrawClock(double centerX, double centerY, double width)
{
double radius = width / 2;
// Border.
Ellipse ellipse = new Ellipse()
{
Stroke = Brushes.Navy,
StrokeThickness = 2,
Width = width,
Height = width
};
Canvas.SetLeft(ellipse, centerX - (ellipse.Width / 2));
Canvas.SetTop(ellipse, centerY - (ellipse.Height / 2));
ClockContainer.Children.Add(ellipse);
// Center.
ellipse = new Ellipse()
{
Stroke = Brushes.Navy,
StrokeThickness = 2,
Width = 4,
Height = 4
};
Canvas.SetLeft(ellipse, centerX - (ellipse.Width / 2));
Canvas.SetTop(ellipse, centerY - (ellipse.Height / 2));
ClockContainer.Children.Add(ellipse);
// Ticks.
for (int i = 0; i < 60; i++)
{
if (i % 5 == 0) // 5 minutes tick is different.
{
Line line = new Line();
line.Stroke = System.Windows.Media.Brushes.DodgerBlue;
line.StrokeThickness = 2;
line.X1 = centerX + (radius / 1.10 * Math.Sin(i * 6 * Math.PI / 180));
line.Y1 = centerY - (radius / 1.10 * Math.Cos(i * 6 * Math.PI / 180));
line.X2 = centerX + (radius / 1.25 * Math.Sin(i * 6 * Math.PI / 180));
line.Y2 = centerY - (radius / 1.25 * Math.Cos(i * 6 * Math.PI / 180));
ClockContainer.Children.Add(line);
}
else
{
Line line = new Line();
line.Stroke = System.Windows.Media.Brushes.DodgerBlue;
line.StrokeThickness = 1;
line.X1 = centerX + (radius / 1.10F * Math.Sin(i * 6 * Math.PI / 180));
line.Y1 = centerY - (radius / 1.10F * Math.Cos(i * 6 * Math.PI / 180));
line.X2 = centerX + (radius / 1.15F * Math.Sin(i * 6 * Math.PI / 180));
line.Y2 = centerY - (radius / 1.15F * Math.Cos(i * 6 * Math.PI / 180));
ClockContainer.Children.Add(line);
}
}
}
Así queda:

803bfc3b-576a-4e24-b889-ebc94cf51adf|1|1.0
Tags: wpf, c#, clock
WPF