我有一個DrawingVisual元素rappresents其幾何形狀由這個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類,它提供命中測試功能:
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; }
}
}
當我畫我的(紅色)路徑是這樣的結果:
其中網格單元的尺寸50×50是。現在我嘗試在此區域獲取視覺效果:
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不在結果中,爲什麼?我該如何正確進行命中測試? 謝謝。
你在HitTestCallback(設置斷點)時是否檢查路徑是否全部命中?如果是,IntersectionDetail有什麼價值? – Clemens
無論如何,我編輯的問題(我忘記了轉換):IntersectionDetail.Intersect – gliderkite
@Clemens我可能已經找到[東西](http://stackoverflow.com/questions/10453095/rendertransform-vs-pushtransform)。 – gliderkite