Monday, June 9, 2014

Rotate Lines


namespace Rotate_Line
{
    public partial class Form1 : Form
    {
        Bitmap bmap;
        Graphics g;

        public Form1()
        {
            InitializeComponent();
            bmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            g = Graphics.FromImage(bmap);
        }

        int angle = 0;
        Point p1;
        Point p2;
        int r;
        Pen pen;

        private void Form1_Load(object sender, EventArgs e)
        {
            p1 = new Point(bmap.Width / 2, bmap.Height / 2);
            r = Math.Min(bmap.Width / 2, bmap.Height / 2);
            p2 = new Point(bmap.Width, bmap.Height / 2);
        }

        //Give command by clicking on button
        private void btnRotateLine_Click(object sender, EventArgs e)
        {
            pen = new Pen(Color.Green, 1);
            g.DrawLine(pen, p1, p2);
           
            angle++;
            if (angle > 360)
                angle = 0;
            p2.X = (int)(r * Math.Cos(Math.PI * angle / 180));
            p2.Y = (int)(r * Math.Sin(Math.PI * angle / 180));
            pictureBox1.Image = bmap;
        }

        //To run automatically by timer
        //Note: Set "Enable: True" in Timer's Properties
        private void timer1_Tick(object sender, EventArgs e)
        {
            pen = new Pen(Color.Gold, 1);
            g.DrawLine(pen, p1, p2);
           
            angle++;
            if (angle > 360)
            {
                angle = 0;
                pen = new Pen(Color.Blue, 1);
            }
            p2.X = (int)(r * Math.Cos(Math.PI * angle / 180));
            p2.Y = (int)(r * Math.Sin(Math.PI * angle / 180));

            pictureBox1.Image = bmap;
        }
    }
}

No comments:

Post a Comment