using android.graphics;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestDraw.View
{
    public partial class DrawCirControl : UserControl
    {

        public DrawCirControl()
        {
            InitializeComponent();
            timer1.Enabled = true;
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
        }

        //绘制多边形
        private void DrawPolygon(Graphics graphics, Pen pen, Point[] points)
        {
            Brush brush = new SolidBrush(Color.FromArgb(pen.Color.A, pen.Color.R, pen.Color.G, pen.Color.B));
            graphics.FillPolygon(brush, points);
        }

        private void DrawCircle(Graphics graphics, Pen pen, Point center, int radius, int sides)
        {
            Point[] points = new Point[sides];
            double angle = 360.0 / sides;

            for (int i = 0; i < sides; i++)
            {
                double theta = angle * i;
                double radian = Math.PI / 180 * theta;

                points[i] = new Point(center.X + (int)(radius * Math.Cos(radian)), center.Y + (int)(radius * Math.Sin(radian)));
            }

            DrawPolygon(graphics, pen, points);
        }

        // 定义一个方法,用于获取两个颜色之间的渐变色
        private Color GetColorRoad(Color startColor, Color endColor, int progress)
        {
            // 确保progress的取值在0到100之间

            progress = Math.Max(0, Math.Min(progress, 100));
            // 分别获取起始颜色的RGB值
            int startR = startColor.R;
            int startG = startColor.G;
            int startB = startColor.B;
            int startA = startColor.A;
            // 分别获取结束颜色的RGB值
            int endR = endColor.R;
            int endG = endColor.G;
            int endB = endColor.B;
            int endA = endColor.A;
            // 计算过渡颜色的RGB值
            int transitionR = (endR - startR) * progress / 100 + startR;
            int transitionG = (endG - startG) * progress / 100 + startG;
            int transitionB = (endB - startB) * progress / 100 + startB;
            int transitionA = (endA - startA) * progress / 100 + startA;
            // 构造并返回过渡颜色
            return Color.FromArgb(transitionA,transitionR, transitionG, transitionB);

        }

        //绘制圆
        private void DrawCircle2(Graphics graphics, Color startColor, Color endColor, Point center, int radius, int sides)
        {

            double angle = 360.0 / sides;

            for (int i = 0; i < sides; i++)
            {
                Point[] points = new Point[3];
                double theta = angle * i;
                double radian = Math.PI / 180 * theta;
                points[2] = new Point(center.X , center.Y );
                points[0] = new Point(center.X + (int)(radius * Math.Cos(radian)), center.Y + (int)(radius * Math.Sin(radian)));
                 theta = angle * i+ angle;
                 radian = Math.PI / 180 * theta;
                points[1] = new Point(center.X + (int)(radius * Math.Cos(radian)), center.Y + (int)(radius * Math.Sin(radian)));
                Pen pen2 = new Pen(GetColorRoad(startColor, endColor, i*100/sides));
                DrawPolygon(graphics, pen2, points);
            }
        }

        //绘制环形
        private void DrawCircle4(Graphics graphics, Color startColor, Color endColor, Point center, int radius, int radius2, int sides)
        {

            double angle = 360.0 / sides;

            for (int i = 0; i < sides; i++)
            {
                Point[] points = new Point[4];
                double theta = angle * i;
                double radian = Math.PI / 180 * theta;
                points[0] = new Point(center.X + (int)(radius2 * Math.Cos(radian)), center.Y + (int)(radius2 * Math.Sin(radian)));
                points[1] = new Point(center.X + (int)(radius * Math.Cos(radian)), center.Y + (int)(radius * Math.Sin(radian)));
                theta = angle * i + angle;
                radian = Math.PI / 180 * theta;
                points[2] = new Point(center.X + (int)(radius * Math.Cos(radian)), center.Y + (int)(radius * Math.Sin(radian)));
                points[3] = new Point(center.X + (int)(radius2 * Math.Cos(radian)), center.Y + (int)(radius2 * Math.Sin(radian)));
                Pen pen2 = new Pen(GetColorRoad(startColor, endColor, i * 100 / sides));
                DrawPolygon(graphics, pen2, points);
            }
        }

        //绘制渐变弧
        private void DrawCircle5(Graphics graphics, Color startColor, Color endColor,int startR, int endR, Point center, int radius, int radius2, int sides)
        {

            double angle = 360.0 / sides;

            int ii = 0;
            int range = (int)(endR / angle - startR / angle);
            for (int i = (int)(startR/angle); i < endR/angle; i++)
            {
                Point[] points = new Point[4];
                double theta = angle * i;
                double radian = Math.PI / 180 * theta;
                points[0] = new Point(center.X + (int)(radius2 * Math.Cos(radian)), center.Y + (int)(radius2 * Math.Sin(radian)));
                points[1] = new Point(center.X + (int)(radius * Math.Cos(radian)), center.Y + (int)(radius * Math.Sin(radian)));
                theta = angle * i + angle;
                radian = Math.PI / 180 * theta;
                points[2] = new Point(center.X + (int)(radius * Math.Cos(radian)), center.Y + (int)(radius * Math.Sin(radian)));
                points[3] = new Point(center.X + (int)(radius2 * Math.Cos(radian)), center.Y + (int)(radius2 * Math.Sin(radian)));
                Pen pen2 = new Pen(GetColorRoad(startColor, endColor, ii * 100/range));
                ii++;
                DrawPolygon(graphics, pen2, points);
            }
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            Canvas canvas = new Canvas(pe.Graphics);
            Pen pen = new Pen(Color.Red);

            DrawCircle5(pe.Graphics,Color.Transparent,  Color.Red,load, load+180, new Point(Width / 2, Height / 2), 48,32, 80);
        }
        int load = 0;

        private void OnTimerTick(object sender, EventArgs e)
        {

            load+=10;
            if (load > 360)
            {
                load = load - 360;
            }
            Invalidate();
        }
    }
}

发表评论

邮箱地址不会被公开。 必填项已用*标注