使用System.Drawing绘制基本几何图形

1.使用System.Drawing绘制一个正方形

using System;
using System.Drawing;
using System.Windows.Forms;

public partial class MyForm : Form
{
    public MyForm()
    {
        // 你可以在这里设置Form的双缓冲,以避免绘制时出现的闪烁  
        this.DoubleBuffered = true;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        // 创建一个Pen对象,用于绘制正方形  
        using (Pen pen = new Pen(Color.Black, 2)) // 你可以更改颜色和线条宽度  
        {
            // 设置正方形的位置和大小  
            // 在这个例子中,我们从(50, 50)开始,大小为100x100  
            Rectangle rect = new Rectangle(50, 50, 100, 100);

            // 使用Graphics对象的DrawRectangle方法来绘制正方形  
            e.Graphics.DrawRectangle(pen, rect);
        }
    }
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyForm());
    }
}

 

2. 使用System.Drawing绘制一个长方形

using System;
using System.Drawing;
using System.Windows.Forms;

public partial class MyForm : Form
{
    public MyForm()
    {
        // 你可以在这里设置Form的双缓冲,以避免绘制时出现的闪烁  
        this.DoubleBuffered = true;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        // 创建一个Pen对象,用于绘制长方形  
        using (Pen pen = new Pen(Color.Black, 2)) // 你可以更改颜色和线条宽度  
        {
            // 设置长方形的位置和大小  
            // 在这个例子中,我们从(50, 50)开始,宽度为200,高度为100  
            Rectangle rect = new Rectangle(50, 50, 200, 100);

            // 使用Graphics对象的DrawRectangle方法来绘制长方形  
            e.Graphics.DrawRectangle(pen, rect);
        }
    }
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyForm());
    }
}

3.使用System.Drawing绘制一个圆形

using System;
using System.Drawing;
using System.Windows.Forms;

public partial class MyForm : Form
{
    public MyForm()
    {
        // 你可以在这里设置Form的双缓冲,以避免绘制时出现的闪烁  
        this.DoubleBuffered = true;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        // 创建一个Pen对象,用于绘制圆形的边框  
        using (Pen pen = new Pen(Color.Black, 2)) // 你可以更改颜色和线条宽度  
        {
            // 创建一个Brush对象,用于填充圆形(如果需要的话)  
            using (Brush brush = new SolidBrush(Color.LightBlue)) // 你可以更改填充颜色  
            {
                // 设置圆形的位置和大小  
                // 在这个例子中,圆心在(100, 100),半径为50  
                int centerX = 100;
                int centerY = 100;
                int radius = 50;

                // 绘制圆形的边框(使用DrawEllipse方法)  
                e.Graphics.DrawEllipse(pen, centerX - radius, centerY - radius, 2 * radius, 2 * radius);

                // 如果你想要填充圆形,可以使用FillEllipse方法  
                // e.Graphics.FillEllipse(brush, centerX - radius, centerY - radius, 2 * radius, 2 * radius);  
            }
        }
    }
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyForm());
    }
}

 

4.使用System.Drawing绘制一个三角形 

using System;
using System.Drawing;
using System.Windows.Forms;

public partial class MyForm : Form
{
    public MyForm()
    {
        // 你可以在这里设置Form的双缓冲,以避免绘制时出现的闪烁  
        this.DoubleBuffered = true;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        // 创建一个Pen对象,用于绘制三角形的边  
        using (Pen pen = new Pen(Color.Black, 2)) // 你可以更改颜色和线条宽度  
        {
            // 设置三角形的三个顶点  
            Point point1 = new Point(50, 50);
            Point point2 = new Point(150, 50);
            Point point3 = new Point(100, 150);

            // 绘制三条线以构成三角形  
            e.Graphics.DrawLine(pen, point1, point2);
            e.Graphics.DrawLine(pen, point2, point3);
            e.Graphics.DrawLine(pen, point3, point1);
        }
    }
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyForm());
    }
}

 

5.使用System.Drawing绘制一个五角星 

using System;
using System.Drawing;
using System.Windows.Forms;

public partial class MyForm : Form
{
    public MyForm()
    {
        // 你可以在这里设置Form的双缓冲,以避免绘制时出现的闪烁  
        this.DoubleBuffered = true;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        // 创建一个Pen对象,用于绘制五角星  
        using (Pen pen = new Pen(Color.Black, 2)) // 你可以更改颜色和线条宽度  
        {
            // 设定五角星的中心点  
            int centerX = 100;
            int centerY = 100;

            // 设定五角星的外接圆半径  
            int radius = 50;

            // 设定五角星的旋转角度(如果需要)  
            double rotateAngle = Math.PI / 2; // 从上顶点开始  

            // 计算五角星的五个顶点  
            PointF[] starPoints = CalculateStarPoints(centerX, centerY, radius, 5, rotateAngle);

            // 绘制五角星  
            for (int i = 0; i < starPoints.Length; i++)
            {
                int nextIndex = (i + 1) % starPoints.Length;
                e.Graphics.DrawLine(pen, starPoints[i], starPoints[nextIndex]);
            }
        }
    }

    // 计算五角星的顶点  
    private PointF[] CalculateStarPoints(float centerX, float centerY, float radius, int spikes, double rotation)
    {
        PointF[] result = new PointF[spikes];

        double outerRadius = radius; // 外接圆半径  
        double innerRadius = radius * 0.5f * Math.Sqrt(3); // 内接圆半径(五角星的特殊值)  

        double angle = Math.PI / 2 * 3 - spikes * Math.PI / spikes; // 第一个顶点的角度  

        for (int i = 0; i < spikes; i++)
        {
            result[i].X = (float)(centerX + Math.Cos(angle + rotation) * outerRadius);
            if (i % 2 == 0)
            {
                result[i].Y = (float)(centerY + Math.Sin(angle + rotation) * outerRadius);
            }
            else
            {
                result[i].Y = (float)(centerY + Math.Sin(angle + rotation) * innerRadius);
            }

            angle += Math.PI / spikes;
        }

        return result;
    }
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyForm());
    }
}

 

6.使用System.Drawing绘制一个圆外切三角形 

using System;
using System.Drawing;
using System.Windows.Forms;

public partial class MyForm : Form
{
    public MyForm()
    {
        // 你可以在这里设置Form的双缓冲,以避免绘制时出现的闪烁  
        this.DoubleBuffered = true;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        // 定义三角形的边长  
        int sideLength = 100;

        // 计算外接圆的半径  
        double radius = sideLength / (2 * Math.Sqrt(3));

        // 设定外接圆的中心点和三角形的中心点  
        int centerX = this.ClientSize.Width / 2;
        int centerY = this.ClientSize.Height / 2;

        // 计算三角形的顶点  
        PointF[] trianglePoints = CalculateEquilateralTrianglePoints(centerX, centerY, sideLength);

        // 绘制外接圆  
        using (Pen circlePen = new Pen(Color.Blue, 2))
        {
            e.Graphics.DrawEllipse(circlePen, centerX - (int)radius, centerY - (int)radius, (int)(radius * 2), (int)(radius * 2));
        }

        // 绘制三角形  
        using (Pen trianglePen = new Pen(Color.Black, 2))
        {
            e.Graphics.DrawLines(trianglePen, trianglePoints);
        }
    }

    // 计算等边三角形的三个顶点  
    private PointF[] CalculateEquilateralTrianglePoints(int centerX, int centerY, int sideLength)
    {
        PointF[] points = new PointF[3];

        double angle = Math.PI / 3; // 等边三角形内角的一半  

        // 第一个顶点(上方)  
        points[0] = new PointF(centerX, centerY - sideLength / 2);

        // 第二个顶点(右侧)  
        points[1] = new PointF(
            (float)(centerX + sideLength / 2 * Math.Cos(angle)),
            (float)(centerY + sideLength / 2 * Math.Sin(angle))
        );

        // 第三个顶点(左侧)  
        points[2] = new PointF(
            (float)(centerX + sideLength / 2 * Math.Cos(-angle)),
            (float)(centerY + sideLength / 2 * Math.Sin(-angle))
        );

        return points;
    }
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyForm());
    }
}

7.使用System.Drawing绘制一个圆内接三角形

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值