0

I'm quite new in C#, please give some advice. In a Windows Form app I have several points on my canvas, linked with lines (e.g. a polygon). I want to calculate every middle point of the lines and link the dots with a button click so I can smooth the polygon.

I wrote the following code but it doesn't put the midpoints onto the screen, and I have no idea why. Where I did I make a mistake?

P is the original list with the points. NewPoints is my new list with the new points.

private void button1_Click(object sender, EventArgs e)
{
    NewPoints.Add(P[0]);

    for (int i = 0; i < P.Count; i++)
    {
        NewPoints.Add(new Point(  (P[i + 1].X+P[i].X)/2, (P[i + 1].Y + P[i].Y) / 2));
    }

    NewPoints.Add(P[P.Count - 1]);

    P.Clear();

    for (int j = 0; j < NewPoints.Count; j++)
        P.Add(NewPoints[j]);

    NewPoints.Clear();
    canvas.Refresh();
}
vorbis
  • 13
  • 2
  • 1
    What do you think `P[i + 1]` does when `P.Count` is `5` and `i` is 4? – mjwills Jun 15 '18 at 11:17
  • `public static PointF PointBetween(PointF p1, PointF p2, float percent) { float px = (p2.X - p1.X) / percent; float py = (p2.Y - p1.Y) / percent; return new PointF( p1.X + px, p1.Y + py); }` – TaW Jun 15 '18 at 11:21

0 Answers0