Wednesday 11 December 2013

c# GDI rotation transform


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace rotation
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            const float offset = 80f;
            LinearGradientBrush lbrush = new LinearGradientBrush(this.ClientRectangle, Color.Yellow, Color.Lime, LinearGradientMode.BackwardDiagonal);
            e.Graphics.FillRectangle(lbrush, this.ClientRectangle);

            GraphicsPath gp = new GraphicsPath();
            gp.AddBezier(110, 110, 110, 115, 120, 115, 125, 130);
            gp.AddEllipse(125, 130, 8, 8);
            GraphicsPath gpold = new GraphicsPath();
            gpold = (GraphicsPath)gp.Clone();

            Matrix atransform = new Matrix(1, 0, 0, 1, 0, 0);
            Matrix translationtransform = new Matrix(1, 0, 0, 1, 0, 0);
            PointF rotationpoint = new PointF(110f, 110f);

            for (int i = 0; i < 4; i++)
            {
                for (float f = 0f; f < 359f; f += 45f)
                {
                    gp.Transform(translationtransform);
                    atransform.RotateAt(f, rotationpoint);
                    gp.Transform(atransform);
                    e.Graphics.FillPath(Brushes.Red, gp);
                    e.Graphics.DrawPath(Pens.Black, gp);
                    atransform.Dispose();
                    atransform = new Matrix(1, 0, 0, 1, 0, 0);
                    gp = (GraphicsPath)gpold.Clone();
                }
                translationtransform.Translate(offset, 0);
                rotationpoint.X = rotationpoint.X + offset;
            }

            e.Graphics.DrawString("translations and rotation transforms in gdi+", new Font("Times New Roman", 14), Brushes.DarkBlue, 20, 20, new StringFormat());
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //this.Refresh();
        }
    }
}

No comments:

Post a Comment