Is Region finite

image_pdfimage_print


   


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

  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

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

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Graphics g = e.Graphics;
      g.FillRectangle(Brushes.White, this.ClientRectangle);

      Region r = new Region(new Rectangle(30, 30, 30, 60));
      r.Exclude(new Rectangle(40, 40, 10, 10));
      g.FillRegion(Brushes.Orange, r);

      Console.WriteLine(r.IsInfinite(g) ? " - is infinite"
                        : " - is finite");

      r.Dispose();
    }

    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }


           
          


Region Excludes

image_pdfimage_print


   



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

  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

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

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Graphics g = e.Graphics;
      g.FillRectangle(Brushes.White, this.ClientRectangle);

      Region r = new Region(new Rectangle(30, 30, 30, 60));
      r.Exclude(new Rectangle(40, 40, 10, 10));
      g.FillRegion(Brushes.Orange, r);

      r.Dispose();
    }

    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }

           
          


Fill a Region

image_pdfimage_print


   


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

  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

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

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Graphics g = e.Graphics;
      g.FillRectangle(Brushes.White, this.ClientRectangle);

      GraphicsPath gp = new GraphicsPath();

      gp.AddLine(10, 10, 10, 50);
      gp.AddBezier(10, 50, 120, 55, 25, 70, 30, 70);
      gp.AddLine(30, 70, 60, 70);
      gp.AddBezier(60, 70, 305, 70, 90, 55, 90, 50);
      gp.AddLine(90, 50, 90, 30);

      gp.StartFigure();
      gp.AddLine(60, 110, 40, 160);
      gp.AddLine(40, 160, 60, 180);

      // Create a Region whose boundary is the above GraphicsPath 
      Region reg = new Region(gp);

      g.FillRegion(Brushes.Green, reg);
      reg.Dispose();
      gp.Dispose();
    }

    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }


           
          


Create a Region whose boundary is the GraphicsPath

image_pdfimage_print


   



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

  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

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

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Graphics g = e.Graphics;
      g.FillRectangle(Brushes.White, this.ClientRectangle);

      GraphicsPath gp = new GraphicsPath();

      // Create an open figure
      gp.AddLine(10, 10, 10, 50);
      gp.AddLine(10, 50, 50, 50);

      // Start a new figure
      gp.StartFigure();
      gp.AddLine(60, 10, 60, 50);
      gp.AddLine(60, 50, 100, 50);
      gp.CloseFigure();

      gp.AddEllipse(new Rectangle(110, 10, 40, 40));

      // Create a Region whose boundary is the above GraphicsPath 
      Region reg = new Region(gp);

      g.FillRegion(Brushes.Green, reg);
      reg.Dispose();
      gp.Dispose();
    }

    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }

           
          


Add size and point

image_pdfimage_print


   


using System;
using System.Drawing;

class Class1 {
    static void Main(string[] args) {
        Point topLeft = new Point(10,10);
        Size rectangleSize = new Size(50,50);
        Point bottomRight = topLeft + rectangleSize;
        Console.WriteLine("topLeft = " + topLeft);
        Console.WriteLine("bottomRight = " + bottomRight);
        Console.WriteLine("Size = " + rectangleSize);
    }
}

           
          


Draw Rectangle

image_pdfimage_print

/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds

Publisher: Apress
ISBN: 159059035X
*/
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace AllCornerRect
{
public class AllCornerRect : System.Windows.Forms.Form
{

#region Class Local Variables

RealRect MyRect;

#endregion

private System.ComponentModel.Container components = null;

public AllCornerRect() {
InitializeComponent();

this.SetStyle( ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle( ControlStyles.DoubleBuffer, true);

this.MouseDown += new MouseEventHandler(this.M_down);
this.MouseUp += new MouseEventHandler(this.M_up);
this.MouseMove += new MouseEventHandler(this.M_move);

MyRect = new RealRect();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///

/// Required method for Designer support – do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
//
// AllCornerRect
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(320, 301);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = “AllCornerRect”;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = “AllCornerRect”;
this.Load += new System.EventHandler(this.AllCornerRect_Load);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new AllCornerRect());
}

private void AllCornerRect_Load(object sender, System.EventArgs e)
{
}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);

Graphics G = e.Graphics;

G.DrawRectangle(Pens.Red, MyRect.Rect);
}

#region Squeek

private void M_up(object sender, MouseEventArgs e)
{
Invalidate();
}

private void M_down(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
return;

MyRect = new RealRect(e.X, e.Y);
}

private void M_move(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
return;

MyRect.EndX = e.X;
MyRect.EndY = e.Y;
Invalidate();
}

#endregion
}

public class RealRect
{

#region Class Local Variables

private Point mStart;
private Point mEnd;
private Point mRealStart;
private Point mRealEnd;
private Size mRealSize;
private Rectangle mRect;

#endregion

public RealRect(int X, int Y)
{
mStart = Point.Empty;
mEnd = Point.Empty;
mRealStart = Point.Empty;
mRealEnd = Point.Empty;
mRealSize = Size.Empty;

mStart.X = X;
mStart.Y = Y;
mRealStart.X = X;
mRealStart.Y = Y;

mRect = Rectangle.Empty;

}

public RealRect()
{
mStart = Point.Empty;
mEnd = Point.Empty;
mRealStart = Point.Empty;
mRealEnd = Point.Empty;
mRealSize = Size.Empty;

mStart.X = 0;
mStart.Y = 0;
mRealStart.X = 0;
mRealStart.Y = 0;

mRect = Rectangle.Empty;
}

///

/// Ending X Value of rectangle
///

public int EndX
{
set{ mEnd.X = value; }
}

///

/// Ending Y Value of rectangle
///

public int EndY
{
set{ mEnd.Y = value; }
}

///

/// Get the corrected rectangle
///

public Rectangle Rect
{
get
{
MakeReal();
mRect.Location = mRealStart;
mRect.Size = mRealSize;
return mRect;
}
}

private void MakeReal()
{
//Started top left, ended bottom right
if (mEnd.X > mStart.X && mEnd.Y > mStart.Y)
{
mRealStart = mStart;
mRealEnd = mEnd;
mRealSize = new Size(mRealEnd.X-mRealStart.X, mRealEnd.Y-mRealStart.Y);
return;
}

//Started bottom right, ended top left
if (mEnd.X < mStart.X && mEnd.Y < mStart.Y) { mRealEnd = mStart; mRealStart = mEnd; mRealSize = new Size(mRealEnd.X-mRealStart.X, mRealEnd.Y-mRealStart.Y); return; } //Started top right left, ended bottom left if (mEnd.X < mStart.X && mEnd.Y > mStart.Y)
{
mRealStart.X = mEnd.X;
mRealStart.Y = mStart.Y;
mRealEnd.X = mStart.X;
mRealEnd.Y = mEnd.Y;
mRealSize = new Size(mRealEnd.X-mRealStart.X, mRealEnd.Y-mRealStart.Y);
return;
}

//Started bottom left, ended top right
if (mEnd.X > mStart.X && mEnd.Y < mStart.Y) { mRealStart.X = mStart.X; mRealStart.Y = mEnd.Y; mRealEnd.X = mEnd.X; mRealEnd.Y = mStart.Y; mRealSize = new Size(mRealEnd.X-mRealStart.X, mRealEnd.Y-mRealStart.Y); return; } } } } [/csharp]

Draw Square

image_pdfimage_print


   

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/

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

namespace GDI_Basics
{
    /// <summary>
    /// Summary description for DrawSquare.
    /// </summary>
    public class DrawSquare : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.StatusBar StatusBar1;
        internal System.Windows.Forms.StatusBarPanel pnlSquares;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public DrawSquare()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.StatusBar1 = new System.Windows.Forms.StatusBar();
            this.pnlSquares = new System.Windows.Forms.StatusBarPanel();
            ((System.ComponentModel.ISupportInitialize)(this.pnlSquares)).BeginInit();
            this.SuspendLayout();
            // 
            // StatusBar1
            // 
            this.StatusBar1.Location = new System.Drawing.Point(0, 244);
            this.StatusBar1.Name = "StatusBar1";
            this.StatusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
                                                                                          this.pnlSquares});
            this.StatusBar1.ShowPanels = true;
            this.StatusBar1.Size = new System.Drawing.Size(292, 22);
            this.StatusBar1.SizingGrip = false;
            this.StatusBar1.TabIndex = 1;
            this.StatusBar1.Text = "statusBar1";
            // 
            // pnlSquares
            // 
            this.pnlSquares.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
            this.pnlSquares.Width = 292;
            // 
            // DrawSquare
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.StatusBar1});
            this.Name = "DrawSquare";
            this.Text = "DrawSquare";
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.DrawSquare_MouseDown);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.DrawSquare_Paint);
            ((System.ComponentModel.ISupportInitialize)(this.pnlSquares)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion


        // Store the squares that are painted on the form.
        ArrayList squares = new ArrayList();

        private void DrawSquare_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                // Add a square and update the screen.
                Rectangle square = new Rectangle(e.X, e.Y, 20, 20);
                squares.Add(square);
                this.Invalidate(square);
            }
            else if (e.Button == MouseButtons.Right)
            {
                // Search  for the clicked square.
                int squareNumber = 0;
                foreach (Rectangle square in squares)
                {
                    squareNumber++;
                    if (square.Contains(e.X, e.Y))
                    {
                        MessageBox.Show("Point inside square #" +
                            squareNumber.ToString());
                    }
                }
            }

        }

        private void DrawSquare_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Pen drawingPen = new Pen(Color.Red, 10);
            foreach (Rectangle square in squares)
            {
                e.Graphics.DrawRectangle(drawingPen, square);
            }

            pnlSquares.Text = " " + squares.Count.ToString() + " squares";

        }


        [STAThread]
        static void Main() 
        {
            Application.Run(new DrawSquare());
        }
    }
}