C# WPF从中心点以一定角度旋转直线
我试图用WPF和c#来画线,并面临以下问题 我必须画一条固定长度的线,我需要以给定的角度旋转这条线。假设45度。 但条件是我应该从中心点旋转这个。 我附上清晰的理解图像C# WPF从中心点以一定角度旋转直线,c#,.net,wpf,xaml,C#,.net,Wpf,Xaml,我试图用WPF和c#来画线,并面临以下问题 我必须画一条固定长度的线,我需要以给定的角度旋转这条线。假设45度。 但条件是我应该从中心点旋转这个。 我附上清晰的理解图像 谁能帮我写一个C#程序。若要按自定义角度旋转直线,请对其应用a。可以将属性设置为0.5 0.5以围绕中心点旋转 <Grid Width="200" Height="200"> <Line X1="0" Y1="0" X2="1" Y2="0" Stretch="Uniform" Stroke="Blu
谁能帮我写一个C#程序。若要按自定义角度旋转直线,请对其应用a。可以将属性设置为
0.5 0.5
以围绕中心点旋转
<Grid Width="200" Height="200">
<Line X1="0" Y1="0" X2="1" Y2="0" Stretch="Uniform" Stroke="Blue" StrokeThickness="2" RenderTransformOrigin="0.5 0.5">
<Line.RenderTransform>
<RotateTransform Angle="45" />
</Line.RenderTransform>
</Line>
</Grid>
如果角度固定(例如,始终相同),则可以计算起点和终点的坐标,并绘制对角线,而无需使用变换:
<Line X1="0" Y1="0" X2="200" Y2="200" Stroke="Blue" StrokeThickness="2" />
这是一个想法,你需要改进它,根据需要塑造它
private void Rotate()
{
while (Degrees >= 360)
Degrees -= 360;
while (Degrees <= -360)
Degrees += 360;
X1 = 0;
Y1 = 0;
var rad = Degrees * Math.PI / 180;
const int radius = 100;
var sin = Math.Sin(rad);
var cos = Math.Cos(rad);
var tan = Math.Tan(rad);
Y2 = sin * radius;
X2 = cos * radius;
}
private void Rotate()
{
而(度>=360)
度-=360度;
while(度数)您使用什么图像/绘图API来渲染线条?或者线条是WPF视图,并且您正在尝试旋转它?
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal" >
<Label Content="Rotation" />
<TextBox Text="{Binding Degrees}" Width="50" Margin="5,5,0,5" HorizontalContentAlignment="Right" VerticalContentAlignment="Center"/>
<Label Content="°" />
<Button Content="Rotate" Margin="5" Command="{Binding RotateCommand}"/>
</StackPanel>
<Grid Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" >
<Line Stroke="Red" X1="{Binding X1}" X2="{Binding X2}" Y1="{Binding Y1}" Y2="{Binding Y2}"/>
</Grid>
</Grid>