dodoria1992 dodoria1992 - 1 year ago 338
C# Question

Polylines extended on canvas while overdrawing with use of PointCollection C# WPF

I am creating autocad plugin which get points from existing geometry, pass it to another windows and create object with same geometry on canvas as polyline.
Autocad object is polyline so some points (vertices) must be overdrawn.

Points in autocad

I gather points from autocad, length of vertices and transform that to center object in real coordinates on canvas. Then when I draw it, i get this:

enter image description here

The points that i collect are correct, they are transformed simetrically. And the code for drawing on canvas is here:

private void drawOnCanvas(List<Point> points)
// Create a black Brush
SolidColorBrush blackBrush = new SolidColorBrush();
blackBrush.Color = Colors.Black;

// Create a polyline
Polyline poly = new Polyline();
poly.Stroke = blackBrush;
poly.StrokeThickness = 4;

// Create a collection of points for a polyline
PointCollection polygonPoints = new PointCollection();
for (int i = 0; i < points.Count-1; i++)

// Set Polyline.Points properties
poly.Points = polygonPoints;

// Add polyline to the page

When i enter debugger, points are displayed like this:

enter image description here

As you see, points are defined in the right way.

private void canvas_MouseMove(object sender, MouseEventArgs e)
Point p = Mouse.GetPosition(canvas);
coords.Content = p.ToString();

When I read coordinates with mouseMove, extended side is longer for about half of length (50/2).

Why does this happen and how to fix this problem?

Answer Source

I think it's because when you turn from the top right point to the top middle point the drawing of the polygon is extended beyound the actual point to make a kind of sharp vertex. But I think you draw in the window with y-axis downwards so you'll have to think my top right as bottom rignt :-) It means it's the short sides that are right.

The solution is to use distinct lines or a pathgeometry:

<Path Data="M10 5 L60 5 M35 5 L35 65 M10 65 L60 65" Stroke="Black" StrokeThickness="5"  />


With reference to the first drawing above, it is actually drawn in this sequence: 5-6-4-3-1-2. (because y-axis is downwards).

If you insert the two shapes below into a Canvas, you'll see the difference between the rendering of a polyline and a path. If you change the value 20 to another value in the Polyline you'll see the effect of the (per design) rendering of a Polyline - compared to the Path. My points in both polyline and path are arbitrary, you should of cause use your ACad points instead.

<Polyline Name="Poly" Points="10 5, 60 5, 35, 20 35, 65, 10, 65 60, 65" Stroke="Blue" StrokeThickness="5">

<Path Name="MyPath" Data="M10 5 L60 5 M35 5 L35 65 M10 65 L60 65" Stroke="Black" StrokeThickness="5">
    <TranslateTransform X="0" Y="100" />

If you want more information about Path please look here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download