Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
468 views
in Technique[技术] by (71.8m points)

c# - Drawing a polygon according to the input coordinates

How can i draw a polygon according to the input coordinates which are given in C#.

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You didn't show any code because based on those coordinate, you are applying some form of scaling to the image.

Using the Paint event of a PictureBox, here is an example using those coordinates on the screen. It fills in the polygon, then draws the border, then it loops through all the points to draw the red circle:

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));
    }
  }
}

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...