错误的几何点击测试 [英] Bug in geometry Hit-Testing

查看:131
本文介绍了错误的几何点击测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DrawingVisual 元素说唱presents其几何形状由本的语法

I have a DrawingVisual element that rappresents a path which geometry is described by this 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"

"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类,提供了命中测试功能:

To render the visual i use MyCanvas class, that provides hit-testing functionality:

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; }
   }
}

当我画我的(红色)的路径,这是结果:

When i draw my (red) path this is the result:

其中网格单元的大小为50×50。现在,我试图让视觉效果,例如在该地区:

Where the size of the grid cells is 50x50. Now i try to get visuals for example in this region:

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中没有结果,为什么?我怎么也得适当地做点击测试? 谢谢你。

But MyPath is not in result, why? How i have to properly do hit-test? Thanks.

推荐答案

好像的命中测试的审议形状的位置,施加一个的的stackoverflow.com/questions/10453095/rendertransform-vs-pushtransform">reverse秩序。这可以解释为什么我的道路只交叉,而不是完全进入中的 RectangleGeometry 参数 MyCanvas.GetVisu​​als 方法。

It seems like hit-testing considers the position of the shapes to which was applied a reverse order of transformations. This would explain why my path is intersected only and not fully inside the RectangleGeometry argument of MyCanvas.GetVisuals method.

等待一个更好的反应,我实现了的点击测试的用的没有命中测试的方法,现在的一部分 MyCanvas 类:

Waiting for a better response, i implemented the hit-test with a not hit-testing method, now part of 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;
}

编辑:

由于迈克丹麦人(主持人在M​​SDN论坛)解释的这个主题:

As Mike Danes (moderator on MSDN forum) explains in this thread:

是不是真的有可能,这是在几何命中测试中的错误?

我99%肯定这是一个错误。绘制和命中测试应该使用相同的变换顺序。下正常工作的TransformGroup的原因是因为这样一来,你只能在一个推变换在图形方面,这避免了点击测试绘图上下文错乘秩序。   请注意,此无关的事实,在TranformGroup使用的顺序是从推顺序不同。

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.

这篇关于错误的几何点击测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆