我必须建立一个空气动力学的画布与不同的矩形,其中一些必须交叉。我通过添加两条线作为矩形的对角线来完成此操作。问题是,当我将线条设置得更粗时,线条会像图片中那样越过矩形轮廓:

有没有办法把这条线设置成只在矩形内?
下面是我用来添加矩形和线条的代码:
private void DrawRectangle()
{
var rectangle = new Rectangle();
rectangle.Height = 100;
rectangle.Width = 100;
rectangle.Fill = Brushes.Yellow;
rectangle.Stroke = System.Windows.Media.Brushes.Blue;
rectangle.StrokeThickness = 1;
_canvas.Children.Add(rectangle);
}
private void DrawBroken(Rectangle rectangle, long left, long bottom)
{
DrawBrokenLine(0, 0, 100, 100);
DrawBrokenLine(0, 100, 100, 0);
}
private void DrawBrokenLine(long x1, long y1, long x2, long y2)
{
var line = new Line();
line.X1 = x1;
line.Y1 = y1;
line.X2 = x2;
line.Y2 = y2;
line.Stroke = Brushes.Indigo;
line.StrokeThickness = 10;
_canvas.Children.Add(line);
}发布于 2018-07-17 22:06:02
您可以将矩形和线条添加到剪辑其子元素的另一个画布中:
<Canvas Width="100" Height="100" ClipToBounds="True">
<Rectangle Width="100" Height="100" Fill="Yellow" Stroke="Blue" StrokeThickness="1"/>
<Line X1="0" Y1="0" X2="100" Y2="100" Stroke="Indigo" StrokeThickness="10"/>
<Line X1="0" Y1="100" X2="100" Y2="0" Stroke="Indigo" StrokeThickness="10"/>
</Canvas>发布于 2018-07-18 03:58:50
如果你不喜欢线条的方形末端,你可以设置它们的末端封口。
<Line X1="0" Y1="0" X2="100" Y2="100" Stroke="Indigo" StrokeThickness="10" StrokeEndLineCap="Triangle" StrokeStartLineCap="Triangle"/>
<Line X1="0" Y1="100" X2="100" Y2="0" Stroke="Indigo" StrokeThickness="10" StrokeEndLineCap="Triangle" StrokeStartLineCap="Triangle"/>此外,请记住StrokeThickness为10,线条将超过起点和终点坐标,因此您可能需要进行相应的调整(除非您按照另一个人的建议进行裁剪)。将4加到开头(0 => 4),然后从结尾减去4 (100 => 96)似乎是正确的。
https://stackoverflow.com/questions/51383185
复制相似问题