作业帮 > 综合 > 作业

C# 从原点出发画射线

来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/04/27 18:04:34
C# 从原点出发画射线

我想用代码实现从坐标原点(0,0),绘制一定角度的射线,如何去做?
    public partial class Form1 : Form
    {
        private const int cnt = 15;//数量
        private float angle = (float)(2 * Math.PI / cnt);
        private const int R = 100;//长度
        public Form1()
        {
            InitializeComponent();
            this.Paint += this.Form1_Paint;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            for (int n = 0; n < cnt; n++)
            {
                float a = n * angle;
                Point ps = new Point(0, 0);
                Point pe = new Point((int)(R * Math.Cos(a)), (int)(R * Math.Sin(a)));
                Change(ref ps);
                Change(ref pe);
                e.Graphics.DrawLine(new Pen(Color.Red), ps, pe);
            }
        }

        //转换坐标系
        private void Change(ref Point p)
        {
            Size sz = this.ClientSize;
            p.X += sz.Width / 2;
            p.Y = sz.Height / 2 - p.Y;
        }
    }