【发布时间】:2020-03-13 04:55:01
【问题描述】:
我想画一个带箭头的粗透明箭头:
这是绘制箭头轴的代码。请注意,我必须偏移矩形,以便从矩形的中点进行计算。
private void DrawMovementArrow(bool color, double StartX, double StartY, double EndX, double EndY)
{
SolidColorBrush partiallyTransparentSolidColorBrush;
Rectangle myRectangle = new Rectangle();
// This will be replaced by piece size
int width = 35;
myRectangle.Width = width;
// Apparently necessary to offset the drawing of the path so that the point is in the center of the path; not the edge.
StartX -= width / 2;
EndX -= width / 2;
myRectangle.Height = Map.EuclideanDistance(StartX, StartY, EndX, EndY) ;
int angle = CalculateAngle(StartX , StartY , EndX , EndY );
// This selects the midpoint of edge of the rectangle to rotate around (weird system)
myRectangle.RenderTransformOrigin = new Point(0.5, 0);
angle = angle - 180;
RotateTransform rotateTransform1 = new RotateTransform(angle, 0 , 0 );
myRectangle.RenderTransform = rotateTransform1;
if (color)
partiallyTransparentSolidColorBrush = new SolidColorBrush(Colors.Blue);
else
partiallyTransparentSolidColorBrush = new SolidColorBrush(Colors.Red);
partiallyTransparentSolidColorBrush.Opacity = 0.4;
myRectangle.Fill = partiallyTransparentSolidColorBrush;
MovementCanvas1.Children.Clear();
MovementCanvas1.Children.Add(myRectangle);
Canvas.SetTop(myRectangle, StartY);
Canvas.SetLeft(myRectangle, StartX);
DrawArrowhead(color, EndX, EndY, angle + 90, width);
ShowUnitCenter(MovementArrowList[0]);
}
注意,这段代码选择了边缘中间的一个点来旋转矩形:
// This selects the midpoint of edge of the rectangle to rotate around (weird system)
myRectangle.RenderTransformOrigin = new Point(0.5, 0);
问题是我找不到箭头(三角形)的那个点。这是绘制箭头的代码:
public void DrawArrowhead(bool color, double x, double y, int angle, int width)
{
x += width /2 ;
width = width + (width / 2);
//Add the Polygon Element
Polygon myPolygon = new Polygon();
myPolygon.Opacity = 0.4;
if (color)
{
myPolygon.Fill = new SolidColorBrush(Colors.Blue);
myPolygon.Stroke = System.Windows.Media.Brushes.Blue;
}
else
{
myPolygon.Fill = new SolidColorBrush(Colors.Red);
myPolygon.Stroke = System.Windows.Media.Brushes.Red;
}
myPolygon.StrokeThickness = 0;
RotateTransform rotateTransform1 = new RotateTransform(angle, 0, 0);
myPolygon.RenderTransform = rotateTransform1;
// This selects the midpoint of edge of the triangle to rotate around (weird system)
myPolygon.RenderTransformOrigin = new Point(0.0, 0.5);
System.Windows.Point Point1 = new System.Windows.Point(0, 0);
System.Windows.Point Point2 = new System.Windows.Point(width / 2, width / 2);
System.Windows.Point Point3 = new System.Windows.Point(0,width);
PointCollection myPointCollection = new PointCollection();
myPointCollection.Add(Point1);
myPointCollection.Add(Point2);
myPointCollection.Add(Point3);
myPolygon.Points = myPointCollection;
MovementCanvas1.Children.Add(myPolygon);
Canvas.SetTop(myPolygon, y );
Canvas.SetLeft(myPolygon, x );
}
注意创建三角形的 myPointCollection。问题是我已经尝试了 RenderTransformOrigin 中几乎所有可能的值组合,以找到用于旋转点的点(三角形的中心底部)。似乎什么都没有解决。
谁能建议正确的值?
编辑已解决 我通过改变三角形的点来解决它。这比试图找出旋转点更容易。
【问题讨论】:
-
如何渲染三角形 1. 没有变换 2. 有变换但有默认原点?可以加这样的截图吗?
-
你的问题的代码太多了,但还不够。请参阅minimal reproducible example。更一般地说:你做错了。您应该将您的形状声明为 XAML 中的几何对象(这样的简单形状很容易手动完成,对于更复杂的形状,请使用 Inkscape 之类的工具,它可以导出与 XAML 兼容的矢量数据),然后旋转对象作为一个整体,而不是试图让各个部分进行协调。
-
由于形状的大小是在运行时确定的,我怎么能用 XAML 做到这一点? WPF 的问题似乎是,无论我怎么做,都是错误的。
标签: c# wpf rendertransform