Home > Software design >  How do I rotate a Regular polygon in C#
How do I rotate a Regular polygon in C#

Time:05-31

I need to rotate the regular polygon around a set center by a given degree can somebody help? Im using this code the generate the regular polygon

private static void DrawRegularPolygon(PointF center, // center Coordinates of circle
     int vertexes, // Number of vertices
     float radius, // Radius
     Graphics graphics)
    {
        Pen pen;
        var angle = Math.PI * 2 / vertexes;
        var Rotationangle = (45/180) * Math.PI;

        var points = Enumerable.Range(0, vertexes)
        .Select(i => PointF.Add(center, new SizeF((float)Math.Sin(i * angle) * radius, (float)Math.Cos(i * angle) * radius )));

        if (vertexes%2 == 0)
        {
            pen = new Pen(Color.Red);
        }
        else
        {
            pen = new Pen(Color.Black);
        }

        graphics.DrawPolygon(pen, points.ToArray());
        //graphics.DrawEllipse(Pens.Aqua, new RectangleF(PointF.Subtract(center, new SizeF(radius, radius)), new SizeF(radius * 2, radius * 2)));
    }

CodePudding user response:

Try these lines :

graphics.TranslateTransform(center.X, center.Y);
graphics.RotateTransform(180f);
graphics.TranslateTransform(-center.X, -center.Y);
graphics.DrawPolygon(pen, points.ToArray());

I can rotate the polygon 180 degrees, before drawing it.

  • Related