WPF PathGeometry - 界限错误?

发布于 2024-12-13 05:38:56 字数 1092 浏览 3 评论 0原文

我有一个相当简单的 PathGeometry:

M567764.539,5956314.087L567815.077,5956179.775L567821.625,5956182.314L567773.425,5956311.248L567858.513,5956349.923L567950.858,5956392.466L567949.039,5956399.843L567942.252,5956396.685L567873.018,5956364.467L567799.816,5956330.421L567771.226,5956317.186L567764.539,5956314.087

现在,当我查询此数据的 PathGeometry.Bounds 属性时,我得到以下边界:

567764.5625,5956180  567950.875,5956400

预期的边界是:

567764.539,5956179.775 567950.858,5956399.843

我的主要问题:边界小于几何图形,因此几何图形的一部分可能超出范围。

我创建 PathGeometry 并显示边界,如下所示:

PathGeometry geo = PathGeometry.CreateFromGeometry(Geometry.Parse("M567764.539,5956314.087L567815.077,5956179.775L567821.625,5956182.314L567773.425,5956311.248L567858.513,5956349.923L567950.858,5956392.466L567949.039,5956399.843L567942.252,5956396.685L567873.018,5956364.467L567799.816,5956330.421L567771.226,5956317.186L567764.539,5956314.087"));
System.Diagnostics.Trace.WriteLine(geo.Bounds);

我做错了什么?
更重要的是,如何获得 PathGeometry 的正确边界?

I've got a fairly simple PathGeometry:

M567764.539,5956314.087L567815.077,5956179.775L567821.625,5956182.314L567773.425,5956311.248L567858.513,5956349.923L567950.858,5956392.466L567949.039,5956399.843L567942.252,5956396.685L567873.018,5956364.467L567799.816,5956330.421L567771.226,5956317.186L567764.539,5956314.087

Now when I query the PathGeometry.Bounds attribute for this data I get the following bounds:

567764.5625,5956180  567950.875,5956400

The expected bounds would be:

567764.539,5956179.775 567950.858,5956399.843

My main problem: the bounds are smaller than the geometry, so parts of the geometry might be outside the bounds.

I create the PathGeometry and show the bounds like this:

PathGeometry geo = PathGeometry.CreateFromGeometry(Geometry.Parse("M567764.539,5956314.087L567815.077,5956179.775L567821.625,5956182.314L567773.425,5956311.248L567858.513,5956349.923L567950.858,5956392.466L567949.039,5956399.843L567942.252,5956396.685L567873.018,5956364.467L567799.816,5956330.421L567771.226,5956317.186L567764.539,5956314.087"));
System.Diagnostics.Trace.WriteLine(geo.Bounds);

What am I doing wrong?
And, more important, how do I get the right bounds for a PathGeometry?

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

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

发布评论

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

评论(2

一个人的旅程 2024-12-20 05:38:56

在某些时候,我认为 WPF 必须转换为单点进行渲染,并且我想知道 Bounds 的值是否基于渲染结果。在这种情况下,您可能会看到基于您使用的大量数字的精度限制。我注意到你的 Y 值比 X 大 10 倍,巧合的是,误差也比 X 误差大 10 倍。

如果可以在创建 PathGeometry 之前减去最小 X 和 Y,我认为你会得到更好的数字。假设您正在显示 PathGeometry,您可以将其放置在 Canvas 中并将 Canvas.Left/Top 应用于您的值以在屏幕上获得正确的偏移量。为了获得正确的边界,您可以将顶部/左侧偏移添加到边界结果中。

只是提醒一下,这个答案中有一些猜测。我还没有研究过 Bounds 的内部工作原理,但相对误差似乎表明与浮点数之间的转换。

At some point, I would think WPF has to convert to single point for rendering, and I wonder if the value of Bounds is based off of the rendered result. In this case, you're probably seeing a precision limitation based off of the large numbers you're using. I noticed that your Y values were a factor of 10 larger than X, and coincidentally the error was also a factor of 10 larger than the error in X.

If it's possible to subtract off the min X and Y before creating the PathGeometry, I think you'll get better numbers. Assuming you're displaying the PathGeometry, you could place it in a Canvas and apply Canvas.Left/Top to your values to get the right offset on screen. To get the correct bounds, you would then add the Top/Left offsets to the result of your Bounds.

Just a reminder that there's a bit of speculation in this answer. I haven't looked at the innerworkings of Bounds, but the relative error seems to point to a conversion to and from floats.

柳絮泡泡 2024-12-20 05:38:56

我认为您看到了不精确性,因为数字 PathGeometry 由大浮点数组成。

我不确定您是否能够获得所需的精度。

您可能必须使用可接受的容差来比较边界,例如:

bool isMatch = (Math.Abs(MyPath.Bounds.X - ExpectedBounds.X) < TOLERANCE);

您可以将 TOLERANCE 设置为 0.25 或其他值。

I think you're seeing the imprecision due fact that the numbers PathGeometry is made up of large floating point numbers.

I'm not sure if you'll be able to obtain the precision that you need.

You will probably have to compare the bounds using an acceptable tolerance, like:

bool isMatch = (Math.Abs(MyPath.Bounds.X - ExpectedBounds.X) < TOLERANCE);

where you can set the TOLERANCE to 0.25 or something.

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