根据输入坐标绘制的多边形(Drawing a polygon according to the in

2019-06-27 08:03发布

我怎么能根据输入坐标,其在C#中给出绘制多边形。

Answer 1:

你没有表现出任何的代码,因为根据这些坐标,你申请某种形式扩展到图像。

使用图片框的Paint事件,这里是使用屏幕上的坐标的例子。 它填补了多边形,然后绘制边框,然后它遍历所有的点来绘制红色圆圈:

void pictureBox1_Paint(object sender, PaintEventArgs e) {
  e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  e.Graphics.Clear(Color.White);

  // draw the shading background:
  List<Point> shadePoints = new List<Point>();
  shadePoints.Add(new Point(0, pictureBox1.ClientSize.Height));
  shadePoints.Add(new Point(pictureBox1.ClientSize.Width, 0));
  shadePoints.Add(new Point(pictureBox1.ClientSize.Width,
                            pictureBox1.ClientSize.Height));
  e.Graphics.FillPolygon(Brushes.LightGray, shadePoints.ToArray());

  // scale the drawing larger:
  using (Matrix m = new Matrix()) {
    m.Scale(4, 4);
    e.Graphics.Transform = m;

    List<Point> polyPoints = new List<Point>();
    polyPoints.Add(new Point(10, 10));
    polyPoints.Add(new Point(12, 35));
    polyPoints.Add(new Point(22, 35));
    polyPoints.Add(new Point(24, 22));

    // use a semi-transparent background brush:
    using (SolidBrush br = new SolidBrush(Color.FromArgb(100, Color.Yellow))) {
      e.Graphics.FillPolygon(br, polyPoints.ToArray());
    }
    e.Graphics.DrawPolygon(Pens.DarkBlue, polyPoints.ToArray());

    foreach (Point p in polyPoints) {
      e.Graphics.FillEllipse(Brushes.Red, 
                             new Rectangle(p.X - 2, p.Y - 2, 4, 4));
    }
  }
}



Answer 2:

您可以使用Graphics.DrawPolygon 。 您可以存储坐标点的数组,然后可以传递给DrawPolygon方法。 你可能想看看:

使用C#与图形绘制中的WinForms

private System.Drawing.Graphics g;
System.Drawing.Point[] p = new System.Drawing.Point[6];
p[0].X = 0;
p[0].Y = 0;
p[1].X = 53;
p[1].Y = 111;
p[2].X = 114;
p[2].Y = 86;
p[3].X = 34;
p[3].Y = 34;
p[4].X = 165;
p[4].Y = 7;
g = PictureBox1.CreateGraphics();
g.DrawPolygon(pen1, p);


文章来源: Drawing a polygon according to the input coordinates
标签: c# draw polygon