Pergunta

I have a bit of code that takes a number of Points and creates multiple LineSegments to build up a Path.

System.Windows.Shapes.Path pathSegment = new System.Windows.Shapes.Path();
PathFigure pathFig = new PathFigure();
PathGeometry pathGeo = new PathGeometry();
pathFig.StartPoint = new Point(pointData[0].X, pointData[0].Y);
for (int loop = 1; loop < pointData.Count; loop++)
{
    LineSegment ls = new LineSegment();        
    ls.Point = new Point(pointData[loop].X, pointData[loop].Y);
    pathFig.Segments.Add(ls);
}

pathGeo.Figures.Add(pathFig);
pathSegment.Data = pathGeo;
pathSegment.Stroke = brush;
pathSegment.StrokeThickness = 22;

This creates my line with a width of 22px (roughly). Now if you look at the actual Data for this you can only see the LineSegement elements, which essentially gives you an output like this, where the real line is in black and the actual displayed line is in grey (excuse the dodgy mspaint sketch):

enter image description here

Now I want to perform a StrokeContains on the Geometry to see if a specified Point is within the entire pathSegment above (grey area). What it actually does though is check against the LineSegments (the black line).

Is there a better way to build up the Path? or is there a way of converting the pathSegment, including the StrokeWidth to a new Path?

Foi útil?

Solução

It should work if you use the proper Pen Thickness in the StrokeContains call:

Point point = ...
Pen pen = new Pen { Thickness = pathSegment.StrokeThickness };
bool contains = pathSegment.Data.StrokeContains(pen, point);

Alternatively you could simply do a hit test on the Path:

bool contains = pathSegment.InputHitTest(point) != null;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top