将几何图形转换为 wpf 中的路径(使用混合?)

发布于 2024-10-07 11:23:34 字数 470 浏览 0 评论 0原文

我的问题很简单。

我怎样才能将此代码转换

<Path>
    <Path.Data>
        <EllipseGeometry Center="5,4" RadiusX="4" RadiusY="4"/>
    </Path.Data>
</Path>

为类似的东西

<Path Data="M 0 5 L 3 10 10 0"/>

(请注意,第二个生成一个复选标记,而不是椭圆形。这只是为了说明目的,我的目标就是:找到什么序列给出了椭圆形)

编辑:我也阅读有关 xaml 中贝塞尔曲线的文档,我完全意识到我可以通过计算贝塞尔曲线的精确点来简单地生成正确的代码以获得椭圆,但我不想自己麻烦地进行此计算,所以我想知道是否有一种简单的方法可以做到这一点(也许在 Blend 中)

my question is simple.

how can I convert this code:

<Path>
    <Path.Data>
        <EllipseGeometry Center="5,4" RadiusX="4" RadiusY="4"/>
    </Path.Data>
</Path>

into something like

<Path Data="M 0 5 L 3 10 10 0"/>

(please note that the second one produces a checkmark, not an ellipse. It was just for illustration purposes, and my goal is just that: find what sequence gives an ellipse)

edit: I also read the doc about Bezier curves in xaml and am fully aware that I could simply produce the right code by calculating the exact points of the Bezier curve to get an ellipse, but I do not wish to go to the hassle of doing this calculation myself, so I was wondering if there is an easy way to do this (in Blend maybe)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三月梨花 2024-10-14 11:23:34

这展示了如何从代码中做到这一点。我不确定这是否能解决你的问题 - 你说“混合?”在标题中,我不知道如何在 Blend 中做到这一点。但我希望这会有所帮助。

第一步是将 EllipseGeometry 转换为 PathGeometry

var geom = new EllipseGeometry(new Point(5, 4), 4, 4);
var pathGeometry = PathGeometry.CreateFromGeometry(geom);

获得 PathGeometry 后,您只需调用 ToString< /code> ,它会给你字符串表示形式:

string pathText = pathGeometry.ToString();

在我的系统上,这会产生以下相当详细的文本:

“M9,4C9,6.20913899932317 7.20913899932317,8 5,8 2.79086100067683,8 1,6.20913899932317 1,4 1,1.79 086100067683 2.79086100067683 ,0 5,0 7.20913899932317,0 9,1.79086100067683 9,4z"

现在,如果您希望最终得到将该字符串输入到 Xaml 中所得到的结果,则还需要一步,因为 Xaml 的形式您提供的:

<Path Data="M 0 5 L 3 10 10 0"/>

不会生成 PathGeometry。它生成一个 StreamGeometry,这是一种稍微高效但固定的路径表示形式。 (主要区别在于,您无法在 StreamGeometry 中获取代表各种图形和分段的单个对象,而使用 PathGeometry 则可以。StreamGeometry 更便宜,因此。)

您可以从路径文本中获取 StreamGeometry

var streamGeometry = StreamGeometry.Parse(pathText);

然后,如果您希望在 Path 中构建它:

var p = new Path { Data = streamGeometry };

取决于根据您的确切目标,可能有更有效的方法来做到这一点。 (StreamGeometry 是一种更有效的表示形式,因为它最终创建的对象要少得多。但是我的代码到达该表示形式的路线不是很有效,因此您最好停止使用 <或者,如果您的目标实际上只是获取路径文本,那么 PathGeometry 也足以满足您的需求。)

This shows how to do it from code. I'm not sure if that'll solve your problem or not - you say "with blend?" in the title, and I'm not aware of a way to do this in Blend. But I hope this may help.

The first step would be to convert the EllipseGeometry into a PathGeometry:

var geom = new EllipseGeometry(new Point(5, 4), 4, 4);
var pathGeometry = PathGeometry.CreateFromGeometry(geom);

Once you've got a PathGeometry you can simply call ToString and it'll give you the string representation:

string pathText = pathGeometry.ToString();

On my system, this produces the following rather verbose text:

"M9,4C9,6.20913899932317 7.20913899932317,8 5,8 2.79086100067683,8 1,6.20913899932317 1,4 1,1.79086100067683 2.79086100067683,0 5,0 7.20913899932317,0 9,1.79086100067683 9,4z"

Now if you want to end up with exactly what you would have got if you'd fed that string into Xaml, you need one more step, because Xaml of the form you've supplied:

<Path Data="M 0 5 L 3 10 10 0"/>

doesn't produce a PathGeometry. It produces a StreamGeometry, which is a slightly more efficient, but fixed representation of the path. (The main difference being that you can't get hold of individual objects representing the various figures and segements in a StreamGeometry, whereas you can with a PathGeometry. StreamGeometry is cheaper, as a result.)

You can get a StreamGeometry from the path text:

var streamGeometry = StreamGeometry.Parse(pathText);

And then if you want that in a Path just build it:

var p = new Path { Data = streamGeometry };

Depending on what your exact goal is, there may be more efficient ways to do this. (StreamGeometry is a more efficient representation because it ends up creating far fewer objects. But the route by which my code gets to that representation is not very efficient, so you might be better off just stopping with the PathGeometry that the very first code snippet produces. Alternatively, if your goal is really just to get the path text, then the PathGeometry is also sufficient for your needs.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文