CtrEditor/ObjetosSim/UserControls/ThreeLinesControl.xaml.cs

117 lines
3.8 KiB
C#
Raw Normal View History

2024-05-31 10:06:49 -03:00
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace CtrEditor.ObjetosSim.UserControls
{
/// <summary>
/// Interaction logic for ThreeLinesControl.xaml
/// </summary>
public partial class ThreeLinesControl : UserControl
{
public ThreeLinesControl()
{
InitializeComponent();
this.Loaded += ThreeLinesControl_Loaded;
}
private void ThreeLinesControl_Loaded(object sender, RoutedEventArgs e)
{
CreateRectangles();
}
public double AnchoRecto
{
get { return (double)GetValue(AnchoRectoProperty); }
set { SetValue(AnchoRectoProperty, value); }
}
public static readonly DependencyProperty AnchoRectoProperty =
DependencyProperty.Register("AnchoRecto", typeof(double), typeof(ThreeLinesControl), new PropertyMetadata(100.0, OnDimensionsChanged));
public double AnchoCentro
{
get { return (double)GetValue(AnchoCentroProperty); }
set { SetValue(AnchoCentroProperty, value); }
}
public static readonly DependencyProperty AnchoCentroProperty =
DependencyProperty.Register("AnchoCentro", typeof(double), typeof(ThreeLinesControl), new PropertyMetadata(100.0, OnDimensionsChanged));
public double Altura
{
get { return (double)GetValue(AlturaProperty); }
set { SetValue(AlturaProperty, value); }
}
public static readonly DependencyProperty AlturaProperty =
DependencyProperty.Register("Altura", typeof(double), typeof(ThreeLinesControl), new PropertyMetadata(100.0, OnDimensionsChanged));
public double AltoGuia
{
get { return (double)GetValue(AltoGuiaProperty); }
set { SetValue(AltoGuiaProperty, value); }
}
public static readonly DependencyProperty AltoGuiaProperty =
DependencyProperty.Register("AltoGuia", typeof(double), typeof(ThreeLinesControl), new PropertyMetadata(10.0, OnDimensionsChanged));
private static void OnDimensionsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as ThreeLinesControl;
control?.CreateRectangles();
}
private void CreateRectangles()
{
Canvas.Children.Clear();
// Crear Liz
var liz = new Rectangle
{
Width = AnchoRecto,
Height = AltoGuia,
Fill = Brushes.Blue
};
Canvas.SetLeft(liz, 0);
Canvas.SetTop(liz, -AltoGuia / 2);
Canvas.Children.Add(liz);
// Calcular la hipotenusa para Lc
double lcWidth = Math.Sqrt(Math.Pow(AnchoCentro, 2) + Math.Pow(Altura, 2));
// Crear Lc
var lc = new Rectangle
{
Width = lcWidth,
Height = AltoGuia,
Fill = Brushes.Red,
RenderTransformOrigin = new Point(0, 0.5)
};
lc.RenderTransform = new RotateTransform(GetRotationAngle(AnchoCentro, Altura));
Canvas.SetLeft(lc, AnchoRecto);
Canvas.SetTop(lc, -AltoGuia / 2);
Canvas.Children.Add(lc);
// Crear Lde
var lde = new Rectangle
{
Width = AnchoRecto,
Height = AltoGuia,
Fill = Brushes.Green
};
Canvas.SetLeft(lde, AnchoRecto + AnchoCentro);
Canvas.SetTop(lde, Altura - AltoGuia / 2);
Canvas.Children.Add(lde);
}
private double GetRotationAngle(double anchoCentro, double altura)
{
return Math.Atan2(altura, anchoCentro) * 180 / Math.PI;
}
}
}