gpt4 book ai didi

.net - 几何 HitTest 中的错误

转载 作者:行者123 更新时间:2023-12-04 15:14:20 25 4
gpt4 key购买 nike

我有一个 DrawingVisual表示路径的元素,该路径的几何描述由此 syntax :

"m106,59.3c0-1.98,0,0-4.95,0.989-3.96,0.989-13.8,3.96-20.8,4.95-6.92,0-14.8-3.96-17.8-3.96-1.98,2.97,3.96,10.9,7.91,13.8,2.97,1.98,9.89,3.96,14.8,3.96,4.95-0.989,10.9-2.97,13.8-6.92,2.97-2.97,5.93-10.9,6.92-12.9z"



为了呈现视觉效果,我使用了 MyCanvas 类,它提供了 HitTest 功能:
public class MyCanvas : Panel
{
public List<Visual> Visuals = new List<Visual>();
private List<DrawingVisual> Hits = new List<DrawingVisual>();

public void AddVisual(Visual Visual)
{
this.Visuals.Add(Visual);
base.AddVisualChild(Visual);
base.AddLogicalChild(Visual);
}

public List<DrawingVisual> GetVisuals(Geometry Region)
{
GeometryHitTestParameters Parameters = new GeometryHitTestParameters(Region);
this.Hits.Clear();
HitTestResultCallback Callback = new HitTestResultCallback(this.HitTestCallBack);
VisualTreeHelper.HitTest(this, null, Callback, Parameters);

return this.Hits;
}

private HitTestResultBehavior HitTestCallBack(HitTestResult Result)
{
GeometryHitTestResult GeometryRes = (GeometryHitTestResult)Result;
DrawingVisual DVisual = Result.VisualHit as DrawingVisual;

if (DVisual != null && GeometryRes.IntersectionDetail == IntersectionDetail.FullyInside)
this.Hits.Add(DVisual);

return HitTestResultBehavior.Continue;
}

protected override Visual GetVisualChild(int Index)
{ return this.Visuals[Index]; }

protected override int VisualChildrenCount {
get { return this.Visuals.Count; }
}
}

当我绘制我的(红色)路径时,结果如下:



其中网格单元的大小为 50x50。现在我尝试在这个区域获得视觉效果:
MyCanvas my_canvas = new MyCanvas();
RectangleGeometry MyRegion = new RectangleGeometry(new Rect(50, 50, 250, 250));
DrawingVisual MyPath = new DrawingVisual();

using (DrawingContext context = MyPath.RenderOpen()) {
context.PushTransform(new TranslateTransform(50, 50));
context.PushTransform(new ScaleTransform(2, 2));
context.DrawGeometry(Brushes.Red, new Pen(), MyGeometry);
}

my_canvas.AddVisual(MyPath);
List<DrawingVisual> result = my_canvas.GetVisuals(MyRegion);

但是MyPath没有结果,为什么?我必须如何正确进行 HitTest ?
谢谢。

最佳答案

似乎 HitTest 考虑了应用 reverse order of transformations 的形状的位置。 .这将解释为什么我的路径只相交而不是 fully inside RectangleGeometry MyCanvas.GetVisuals 的论据方法。
等待更好的响应,我使用非 HitTest 方法实现了 HitTest ,现在是 MyCanvas 的一部分类(class):

public List<DrawingVisual> GetVisuals(Rect Area)
{
this.Hits.Clear();

foreach (DrawingVisual DVisual in this.Visuals) {
if (Area.Contains(DVisual.DescendantBounds))
this.Hits.Add(DVisual);
}

return this.Hits;
}
编辑:
正如 Mike Danes(MSDN 论坛的主持人)在 this 中解释的那样线:

"Is it really possible that this is a bug in geometry hit-testing?"

I'm 99% sure this is a bug. Drawing and hit testing should use the same transform order. The reason it works correctly with TransformGroup is because this way you only push only one transform in the drawing context and this avoids the wrong multiplication order in the hit test drawing context.Note that this has nothing to do with the fact that the order used in TranformGroup is different from the push order.

关于.net - 几何 HitTest 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10451066/

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