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

namespace WindowsFormsApplication1
{
   
    public partial class Form1 : Form
    {
        Bitmap myBitmap;
        Graphics g;
        SolidBrush fill = new SolidBrush(Color.White);//цвет заливки 
        SolidBrush fon = new SolidBrush(Color.Black);//цвет фона 
        int  alpfa=0, t, tmp1=0, tmp=0;
        Point[] myPointConst, myPoint;
        Point[] pointTrConstT, pointTrConstR, pointTrConstD, pointTrConstL;
        Point[]  pointTrR, pointTrD, pointTrL, pointTrTop;

        Point rotate(Point p, double a)
        {
        a = Math.PI*a/180;

        double x = (double)p.X * Math.Cos(a) - (double)p.Y * Math.Sin(a);
        double y = (double)p.X * Math.Sin(a) + (double)p.Y * Math.Cos(a);
        p.X = (int)x; p.Y = (int)y;
            return p;
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e){
            myPointConst = new Point[]{
                new Point(0, -141),new Point(141, 0),new Point(0, 141),new Point(-141, 0)
            };
            myPoint = new Point[4]; t = 4;
            pointTrConstT = new Point[]{ new Point(-70, -70*t),new Point(0, -70*t-71),new Point(70, -70*t) };
            pointTrConstR = new Point[]{ new Point(70*t, -70),new Point(70*t+71, 0),new Point(70*t, 70) };
            pointTrConstD = new Point[]{ new Point(70, 70*t),new Point(0, 70*t+71),new Point(-70, 70*t) };
            pointTrConstL = new Point[]{ new Point(-70*t, 70),new Point(-70*t-71, 0),new Point(-70*t, -70) };

            pointTrR = new Point[3]; pointTrD = new Point[3]; pointTrL = new Point[3]; pointTrTop = new Point[3];

            timer1.Enabled = true;
        }//end ff clic

        private void draw()//функция изменения фигуры
        {
            myBitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);//создаем битмап 
            g = Graphics.FromImage(myBitmap);//и графику  
            g.SmoothingMode = SmoothingMode.AntiAlias;

            g.FillRectangle(fon, 0, 0, 500, 400);//заливаем фон
            //смещаем начало координат в центр
            g.TranslateTransform((float)pictureBox1.Width / 2, (float)pictureBox1.Height / 2);


            g.FillPolygon(fill, myPoint);

            if (tmp > 20)
            {
                if (alpfa < 45)
                {
                    alpfa += 1; tmp1 = 0;
                    myPoint[0] = rotate(myPointConst[0], alpfa); myPoint[1] = rotate(myPointConst[1], alpfa);
                    myPoint[2] = rotate(myPointConst[2], alpfa); myPoint[3] = rotate(myPointConst[3], alpfa);
                }
                else
                {
                    if (tmp1 > 40)
                    {
                        if (myPoint[0].X > 70)
                        {
                            t = 1;
                            myPoint[0].X -= t; myPoint[0].Y += t;
                            myPoint[1].X -= t; myPoint[1].Y -= t;
                            myPoint[2].X += t; myPoint[2].Y -= t;
                            myPoint[3].X += t; myPoint[3].Y += t;

                            pointTrTop[0] = pointTrConstT[0]; pointTrTop[1] = pointTrConstT[1]; pointTrTop[2] = pointTrConstT[2];
                            pointTrR[0] = pointTrConstR[0]; pointTrR[1] = pointTrConstR[1]; pointTrR[2] = pointTrConstR[2];
                            pointTrD[0] = pointTrConstD[0]; pointTrD[1] = pointTrConstD[1]; pointTrD[2] = pointTrConstD[2];
                            pointTrL[0] = pointTrConstL[0]; pointTrL[1] = pointTrConstL[1]; pointTrL[2] = pointTrConstL[2];
                        }
                        else
                        {
                            t = 4;
                            g.FillPolygon(fill, pointTrTop); g.FillPolygon(fill, pointTrR); g.FillPolygon(fill, pointTrD); g.FillPolygon(fill, pointTrL);

                            if (pointTrR[0].X > 73)
                            {
                                pointTrTop[0].Y += t; pointTrTop[1].Y += t; pointTrTop[2].Y += t;
                                pointTrR[0].X -= t; pointTrR[1].X -= t; pointTrR[2].X -= t;
                                pointTrD[0].Y -= t; pointTrD[1].Y -= t; pointTrD[2].Y -= t;
                                pointTrL[0].X += t; pointTrL[1].X += t; pointTrL[2].X += t;
                            }
                            else
                            {
                                myPoint[0] = myPointConst[0]; myPoint[1] = myPointConst[1]; myPoint[2] = myPointConst[2]; myPoint[3] = myPointConst[3];
                                tmp = 0; alpfa = 0;
                            }
                        }
                    }
                    else tmp1++;
                }
            }
            else tmp++;
            pictureBox1.BackgroundImage = myBitmap;//и отображаем Bitmap на pictureBox

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            draw();
        }//end ff timer
    }
}
