using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace StringRotation
    public partial class Form1 : Form
        public Form1()
        string RotaStr = "123";         // The string to rotate
        float RotationAngle = 0f;       // The "amount" of rotation
        Point loc = new Point(50, 50);  // Where we first start to draw the string

        private void DrawIt(Graphics G)
            const int cFontSize = 36;

            // Don't make the chars look edgy
                G.SmoothingMode = SmoothingMode.AntiAlias; 
            // Set up string stuff
                FontFamily family = new FontFamily("Arial");
                int fontStyle = (int)FontStyle.Bold;
                int emSize = family.GetEmHeight(FontStyle.Bold) / cFontSize;
                Point origin = loc;
                StringFormat format = StringFormat.GenericDefault;
                Size TxSz = TextRenderer.MeasureText(RotaStr, new Font(family, cFontSize));
            // Define rotation matrix
                Matrix RotationTransform = new Matrix(1, 0, 0, 1, 1, 1); 
            // Calculate rotation point
                PointF RotationPoint = new PointF(loc.X + TxSz.Width / 2, loc.Y + TxSz.Height / 2);          
            // Set up the path
                GraphicsPath gp = new GraphicsPath();                 
            // Add the string to the path.
                gp.AddString(RotaStr, family, fontStyle, emSize, origin, format);
            // Make the rotation transformation
                RotationTransform.RotateAt(RotationAngle, RotationPoint);
            // Color the path and fill it
                SolidBrush B = new SolidBrush(Color.OliveDrab);
                G.FillPath(B, gp);

        private void timer1_Tick(object sender, EventArgs e)
            RotationAngle += 20f; // Rotate by 20 degrees every tick
            if (RotationAngle == 360f) RotationAngle = 0f;

        private void Form1_Load(object sender, EventArgs e)
            this.timer1.Interval = 200;

        private void panel1_Paint(object sender, PaintEventArgs e)
7 Years
Discussion Span
Last Post by Diamonddrake

Fun stuff man! I don't know if it is illusion or what, but does the center (pivot point) move as it rotates?


Yes it does! Because it is as close I can get to the center with the calculations I'm using. String measurement is a strange beast as long as you not fully understand it, as I do:( Not that I find it of the uttermost importance but if you or anyone else could give me a clue of what is still wrong here, that would be greatly appreciated!
It has to do with coordinate systems in some way, but I don't seem to get a grasp of it.


that's really neat danny. Idk when i might ever need it. But I'll add it to my private code snippet library just in case. right next to that numbers only with 1 decimal point only textbox class.


Thanks for your comment Diamonddrake:)
In case you are wondering why I needed it, I'm working on a combinatorics calculator. To illustrate this dry math stuff I saw a picture once on the net and would like to imitate that in C#, it is a set of 3 numbered balls. I managed to do it, but to fake the impression that these balls were rolling, I needed rotated digits.
My app is still under contruction, but I can show you my result in the attachement. I just reworked my code a bit and turned it into a snippet.

Attachments ThreeBalls.jpg 5.79 KB
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.