gpt4 book ai didi

wpf - 如何在 XAML 中绘制一个三等分的圆?

转载 作者:行者123 更新时间:2023-12-04 13:09:57 24 4
gpt4 key购买 nike

在我的 WPF 应用程序中,我想绘制一个分为三个相等弧的圆,例如和平符号或饼图。

换句话说,我想画这个:http://i.stack.imgur.com/7Mxwn.jpg

我知道如何创建 System.Windows.Shape s。它在代码中的路径,但不是如何在 XAML 中这样做。

为此类形状创建 Path 元素的正确 XAML 标记是什么?

更新 :给出的答案让我意识到我不清楚我在寻找什么:我想为三个封闭扇区(饼图的切片)中的每一个都有一个 Geometry 对象(单个路径或 GeometryGroup) .)

最佳答案

有几种方法可以做到这一点,最简单的可能是这样的:

<Image Width="200" Height="200">
<Image.Source>
<DrawingImage>
<DrawingImage.Drawing>
<GeometryDrawing>
<GeometryDrawing.Pen>
<Pen Brush="Red"/>
</GeometryDrawing.Pen>
<GeometryDrawing.Geometry>
<GeometryGroup>
<PathGeometry>
<PathFigure StartPoint="100,100">
<PathFigure.Segments>
<LineSegment Point="100,0"/>
</PathFigure.Segments>
</PathFigure>
</PathGeometry>
<PathGeometry>
<PathFigure StartPoint="100,100">
<PathFigure.Segments>
<LineSegment Point="186.6,150"/>
</PathFigure.Segments>
</PathFigure>
</PathGeometry>
<PathGeometry>
<PathFigure StartPoint="100,100">
<PathFigure.Segments>
<LineSegment Point="13.4,150"/>
</PathFigure.Segments>
</PathFigure>
</PathGeometry>
<EllipseGeometry Center="100,100" RadiusX="100" RadiusY="100"/>

</GeometryGroup>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingImage.Drawing>
</DrawingImage>
</Image.Source>
</Image>

enter image description here

可以使用几何迷你语言将上述几何压缩为以下内容:
<GeometryGroup>
<PathGeometry Figures="M100,100 L100,0"/>
<PathGeometry Figures="M100,100 L186.6,150"/>
<PathGeometry Figures="M100,100 L13.4,150"/>
<EllipseGeometry Center="100,100" RadiusX="100" RadiusY="100"/>
</GeometryGroup>

这只是创建一个圆和从中心到边缘的三条线,您需要通过 polar to cartesian conversion 计算点.

另一种方法是使用 ArcSegments ,这是一个主要的痛苦。

编辑:可怕的 ArcSegment 版本:
<Image Width="200" Height="200" Margin="20">
<Image.Source>
<DrawingImage>
<DrawingImage.Drawing>
<DrawingGroup>

<GeometryDrawing Brush="Red">
<GeometryDrawing.Pen>
<Pen Brush="Black" />
</GeometryDrawing.Pen>
<GeometryDrawing.Geometry>
<PathGeometry>
<PathFigure StartPoint="100,100">
<PathFigure.Segments>
<LineSegment Point="100,0"/>
<ArcSegment Point="186.6,150" SweepDirection="Clockwise" Size="100,100"/>
<LineSegment Point="100,100"/>
</PathFigure.Segments>
</PathFigure>
</PathGeometry>
</GeometryDrawing.Geometry>
</GeometryDrawing>

<GeometryDrawing Brush="Blue">
<GeometryDrawing.Pen>
<Pen Brush="Black"/>
</GeometryDrawing.Pen>
<GeometryDrawing.Geometry>
<PathGeometry>
<PathFigure StartPoint="100,100">
<PathFigure.Segments>
<LineSegment Point="186.6,150"/>
<ArcSegment Point="13.4,150" SweepDirection="Clockwise" Size="100,100"/>
<LineSegment Point="100,100"/>
</PathFigure.Segments>
</PathFigure>
</PathGeometry>
</GeometryDrawing.Geometry>
</GeometryDrawing>

<GeometryDrawing Brush="Green">
<GeometryDrawing.Pen>
<Pen Brush="Black"/>
</GeometryDrawing.Pen>
<GeometryDrawing.Geometry>
<PathGeometry>
<PathFigure StartPoint="100,100">
<PathFigure.Segments>
<LineSegment Point="13.4,150"/>
<ArcSegment Point="100,0" SweepDirection="Clockwise" Size="100,100"/>
<LineSegment Point="100,100"/>
</PathFigure.Segments>
</PathFigure>
</PathGeometry>
</GeometryDrawing.Geometry>
</GeometryDrawing>

</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
</Image.Source>
</Image>

enter image description here

压缩几何:
<GeometryDrawing Brush="Red" Geometry="M100,100 L100,0 A100,100,0,0,1,186.6,150 L100,100"/>
<GeometryDrawing Brush="Blue" Geometry="M100,100 L186.6,150 A100,100,0,0,1,13.4,150 L100,100"/>
<GeometryDrawing Brush="Green" Geometry="M100,100 L13.4,150 A100,100,0,0,1,100,0 L100,100"/>

这里的关键点是 ArcSegment.Size定义结果椭圆的半径,因此应为“100,100”,因为这是实际圆的半径。

关于wpf - 如何在 XAML 中绘制一个三等分的圆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5668777/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com