C# .net窗体实战9:正6边形螺旋图案绘制

题目:

提示:

界面:

代码:

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;

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

        // 定义点对象
        public Point ps = new Point();  // 起点
        public Point p1 = new Point();  // 边界点1
        public Point p2 = new Point();  // 边界点2
        public Point p3 = new Point();  // 边界点3
        public Point p4 = new Point();  // 边界点4
        public Point p5 = new Point();  // 边界点5

        private void drawXY(Graphics g)
        {
            //struct是绿色的,类是墨绿色的
            int x = pictureBox1.Width / 2;
            int y = pictureBox1.Height / 2;
            Point mypoint = new Point(x,y);//中心点
            Pen mypenline = new Pen(Color.Black,2);//设置颜色和宽度
            g.TranslateTransform(x, y);  // 将原点移至pictureBox中心

            //绘制X、Y轴
            g.DrawLine(mypenline, -mypoint.X,0,mypoint.X,0);
            g.DrawLine(mypenline, mypoint.X - 10, -10, mypoint.X, 0);
            g.DrawLine(mypenline, mypoint.X - 10, 10, mypoint.X, 0);

            g.DrawLine(mypenline,0,-mypoint.Y,0,mypoint.Y);
            g.DrawLine(mypenline, -10, -mypoint.Y + 10, 0, -mypoint.Y);//箭头
            g.DrawLine(mypenline, 10, -mypoint.Y + 10, 0, -mypoint.Y);

            RectangleF mytangle = new RectangleF(-3f, -3f, 6f, 6f);
            //绘制原点
            g.FillEllipse(Brushes.Black, mytangle);
        }

        //计算各个边界的点的坐标
        public void DefinePoint(int r, double a)
        {
            ps.X = (int)(r * Math.Cos(Math.PI / 180 * a));
            ps.Y = -(int)(r * Math.Sin(Math.PI / 180 * a));

            p1.X = (int)(r * Math.Cos(Math.PI / 180 * (a + 60.0)));
            p1.Y = -(int)(r * Math.Sin(Math.PI / 180 * (a + 60.0)));

            p2.X = (int)(r * Math.Cos(Math.PI / 180 * (a + 120.0)));
            p2.Y = -(int)(r * Math.Sin(Math.PI / 180 * (a + 120.0)));

            p3.X = (int)(r * Math.Cos(Math.PI / 180 * (a + 180.0)));
            p3.Y = -(int)(r * Math.Sin(Math.PI / 180 * (a + 180.0)));

            p4.X = (int)(r * Math.Cos(Math.PI / 180 * (a + 240.0)));
            p4.Y = -(int)(r * Math.Sin(Math.PI / 180 * (a + 240.0)));

            p5.X = (int)(r * Math.Cos(Math.PI / 180 * (a + 300.0)));
            p5.Y = -(int)(r * Math.Sin(Math.PI / 180 * (a + 300.0)));
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Pen mypen= new Pen(Color.Black);
            Graphics g = e.Graphics;
            drawXY(g);

            int r =70;//半径
            double a = 0.0;//初始角度

            for (int i = 0; i < 50; i++) //只需要画一圈就行,转动角度1.2,60/1.2就是50
            {
                r += 3;
                a += 1.2;
                DefinePoint(r, a);
                Point[] pi = { ps, p1, p2, p3, p4, p5 };
                e.Graphics.DrawPolygon(mypen, pi);  // 绘制多边形
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }
    }
}

整体需要注意的地方就是如何进行循环然后画图,往年考过类似,现在一般都是四个题目了,一个窗体的,一个动态绘图的,一个静态绘图的,然后还有一个是数据库的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值