Draw closed curve

image_pdfimage_print


   


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;

public class Form1 : System.Windows.Forms.Form{
   private Point[] thePoints;
   private int counter;

   public Form1() {
      Console.WriteLine("click on the form to draw closed curve.");
    
    
      InitializeComponent();
      thePoints = new Point[1000];
      for (int i=0;i<thePoints.Length; i++)
         thePoints&#91;i&#93; = new Point(0,0);
      counter =0;
   }
   protected override void OnPaint (PaintEventArgs e) {
      if (counter==0) 
         return;

      Pen redPen   = new Pen(Color.Red, 3);
      Pen greenPen = new Pen(Color.Red, 4);
      Point&#91;&#93; curvePoints = new Point&#91;counter&#93;;
      
      for (int i=0; i<curvePoints.Length;i++)
      {
         curvePoints&#91;i&#93; = new Point(0,0);
         curvePoints&#91;i&#93;.X = thePoints&#91;i&#93;.X;
         curvePoints&#91;i&#93;.Y = thePoints&#91;i&#93;.Y;
      }
        
      e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
      e.Graphics.DrawClosedCurve(greenPen, curvePoints);
   }

   private void InitializeComponent() {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Name = "Form1";
      this.Text = "Form1";
      this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
   }

   static void Main() {
      Application.Run(new Form1());
   }

   private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
      thePoints&#91;counter&#93;.X = e.X;
      thePoints&#91;counter&#93;.Y = e.Y;
      Label l = new Label();
      this.SuspendLayout();

      counter++;
      if (counter>4)
        this.Refresh();
  }
}
           
          


This entry was posted in 2D Graphics. Bookmark the permalink.