C#:用鼠标画多边形,接着通过提交输入框内的值,生成与之平行的内外多边形

.Net技术 码拜 8年前 (2016-09-24) 1219次浏览
首先,从屏幕上通过鼠标画一个多变形,要求没有自相交的部分
接着,通过输入框输入外垂距A和内垂距B(输入线的距离为已输入的垂距),来生成与多变形平行的二多边形
解决方案

40

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        List<PointF> dat = new List<PointF>();
        List<PointF> datA = new List<PointF>();
        List<PointF> datB = new List<PointF>();
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            var g = e.Graphics;
            if (dat.Count > 2)
            {
                g.DrawPolygon(Pens.Black, dat.ToArray());
                if (datA.Count > 2) g.DrawPolygon(Pens.Red, datA.ToArray());
                if (datB.Count > 2) g.DrawPolygon(Pens.Green, datB.ToArray());
            }
            else
            {
                foreach (var p in dat)
                {
                    g.DrawArc(Pens.Black, new RectangleF(p.X - 2, p.Y - 2, 5, 5), 0, 360);
                }
            }
        }
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            dat.Add(new Point(e.X, e.Y));
            pictureBox1.Invalidate();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            var A = int.Parse("0" + textBox1.Text);
            var B = int.Parse("0" + textBox2.Text);
            if (A > 0) datA = Polygon(dat, A);
            if (B > 0) datB = Polygon(dat, -B);
            pictureBox1.Invalidate();
            
        }
        List<PointF> Polygon(List<PointF> dat, double h)
        {
            var t = new List<Line>();
            for (var i = 0; i < dat.Count - 1; i++)
            {
                if (i == 0) t.Add(new Line(dat[dat.Count - 1], dat[i]).ParallelLines(h));
                t.Add(new Line(dat[i], dat[i + 1]).ParallelLines(h));
            }
            var res = new List<PointF>();
            for (var i = 0; i < t.Count - 1; i++)
            {
                if (i == 0) res.Add(t[t.Count - 1].Corss(t[i]));
                res.Add(t[i].Corss(t[i + 1]));
            }
            return res;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            dat.Clear();
            datA.Clear();
            datB.Clear();
            pictureBox1.Invalidate();
        }
    }
    public class Line
    {
        public PointF Start;
        public PointF End;
        public PointF Increment;
        public float Slope;
        public Line(PointF s, PointF e)
        {
            Start = s;
            End = e;
            Increment = new PointF(End.X - Start.X, End.Y - Start.Y);
            Slope = Increment.Y / Increment.X;
        }
        public Line ParallelLines(double h)
        {
            var r = (float)(h / Math.Sin(Math.Atan2(Increment.Y, Increment.X)));
            return new Line(
                new PointF(Start.X + r - Increment.X, Start.Y - Increment.Y),
                new PointF(End.X + r + Increment.X, End.Y + Increment.Y));
        }
        public PointF Corss(Line t)
        {
            if (Slope == t.Slope) return PointF.Empty;
            var x = (Slope * Start.X - t.Slope * t.Start.X - Start.Y + t.Start.Y) / (Slope - t.Slope);
            var y = Slope * x - Slope * Start.X + Start.Y;
            return new PointF(x, y); 
        }
    }

C#:用鼠标画多边形,接着通过提交输入框内的值,生成与之平行的内外多边形


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C#:用鼠标画多边形,接着通过提交输入框内的值,生成与之平行的内外多边形
喜欢 (0)
[1034331897@qq.com]
分享 (0)