我正在尝试构建简单的应用程序,它围绕某个枢轴旋转机器人手臂。但我的结果真的很奇怪。我该怎么做才能改变它以给出正确的价值?
我的意思是现在它漂浮在一些我无法识别的奇怪点上,但我已经提供了它应该旋转哪个点的信息。
PS。对不起这段代码中的“乱七八糟”,最终版本会更好。但如果我不能继续简单的草图我怎么能前进:
的 XAML:
<DockPanel>
<Grid DockPanel.Dock="Top">
<Border>
<Canvas Name="plotno" RenderTransformOrigin="0,0" Width="856" Height="520">
<Slider x:Name="slider1" Canvas.Left="631" Canvas.Top="100" Height="21" Width="198" ValueChanged="slider1_ValueChanged" TickPlacement="Both" SmallChange="1" Maximum="360"/>
</Canvas>
</Border>
</Grid>
</DockPanel>
C#
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
Line myLine = new Line();
Line myLine2 = new Line();
long centerheight;
long centerwidth;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
myLine.Stroke = Brushes.LightSteelBlue;
centerwidth = Convert.ToInt64(plotno.Width / 2);
centerheight = Convert.ToInt64(plotno.Height / 2);
myLine.X1 = centerwidth;
myLine.X2 = myLine.X1+100;
myLine.Y1 = centerheight;
myLine.Y2 = myLine.Y1 + 100;
myLine.StrokeThickness = 10;
plotno.Children.Add(myLine);
//rotate by a degree
myLine.RenderTransformOrigin = new Point(myLine.X1, myLine.Y1);
//RotateTransform r = new RotateTransform(0);
//myLine.RenderTransform = r;
//this should stick my second line to first one but first one disappears
var newP1 = myLine.RenderTransform.Transform(new Point(myLine.X2, myLine.Y2));
myLine2.X1 = newP1.X;
myLine2.Y1 = newP1.Y;
myLine2.X2 = myLine2.X1+100;
myLine2.Y2 = myLine2.Y1 + 100;
System.Diagnostics.Debug.WriteLine(myLine.X1);
System.Diagnostics.Debug.WriteLine(myLine.Y1);
System.Diagnostics.Debug.WriteLine(myLine.X2);
System.Diagnostics.Debug.WriteLine(myLine.Y2);
myLine2.Stroke = Brushes.Red;
myLine2.StrokeThickness = 10;
Line whereshouldbeminerotationpivot = new Line();
whereshouldbeminerotationpivot.X1 = myLine.RenderTransformOrigin.X;
whereshouldbeminerotationpivot.X2 = myLine.RenderTransformOrigin.X + 10;
whereshouldbeminerotationpivot.Y1 = myLine.RenderTransformOrigin.Y;
whereshouldbeminerotationpivot.Y2 = myLine.RenderTransformOrigin.Y + 10;
whereshouldbeminerotationpivot.Stroke = Brushes.Red;
whereshouldbeminerotationpivot.StrokeThickness = 20;
plotno.Children.Add(whereshouldbeminerotationpivot);
plotno.Children.Add(myLine2);
}
private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
var linie = plotno.Children.OfType<Line>().ToList();
foreach (var linia in linie)
{
plotno.Children.Remove(linia);
}
for (int i = 0; i < plotno.Children.Capacity; i++)
{ System.Diagnostics.Debug.WriteLine(plotno.Children.Count); }
myLine.X1 = centerwidth;
myLine.X2 = myLine.X1+100;
myLine.Y1 = centerheight;
myLine.Y2 = myLine.Y1 + 100;
myLine.Stroke = Brushes.LightSteelBlue;
myLine.StrokeThickness = 10;
plotno.Children.Add(myLine);
//myLine.LayoutTransform = new RotateTransform(30,0.5,0.5);
//rotate by a degree
myLine.RenderTransformOrigin = new Point(myLine.X1, myLine.Y1);
RotateTransform r = new RotateTransform(e.NewValue);
myLine.RenderTransform = r;
//this should stick my second line to first one but first one disappears
var newP1 = myLine.RenderTransform.Transform(new Point(myLine.X2, myLine.Y2));
myLine2.X1 = newP1.X;
myLine2.Y1 = newP1.Y;
myLine2.X2 = newP1.X+100;
myLine2.Y2 = newP1.Y+100;
System.Diagnostics.Debug.WriteLine(myLine.X1);
System.Diagnostics.Debug.WriteLine(myLine.Y1);
System.Diagnostics.Debug.WriteLine(myLine.X2);
System.Diagnostics.Debug.WriteLine(myLine.Y2);
myLine2.Stroke = Brushes.Gray;
myLine2.StrokeThickness = 10;
Line whereshouldbeminerotationpivot=new Line();
whereshouldbeminerotationpivot.X1 = myLine.RenderTransformOrigin.X;
whereshouldbeminerotationpivot.X2 = myLine.RenderTransformOrigin.X+10;
whereshouldbeminerotationpivot.Y1 = myLine.RenderTransformOrigin.Y;
whereshouldbeminerotationpivot.Y2 = myLine.RenderTransformOrigin.Y+10;
whereshouldbeminerotationpivot.Stroke = Brushes.Red;
whereshouldbeminerotationpivot.StrokeThickness = 20;
plotno.Children.Add(whereshouldbeminerotationpivot);
plotno.Children.Add(myLine2);
}
}
答案 0 :(得分:0)
正如我之前在评论中提到的那样 - 我只是脑力激荡并忘记了我必须实际使用RotateTransform对象的重载构造函数。它看起来像这样:
Line 93 RotateTransform r = new RotateTransform(e.NewValue, centerwidth,centerheight);
但是,尽管它正确旋转(在线的一端附近),当第二个与第一个重叠时,线消失。谁知道为什么?我尝试改变添加(可视化)儿童的顺序,但它不起作用。 我只有第一行的问题。 我在画布中间的标记和第二行总是可以正确显示