Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Check intersection of rectangles and inflate rectangle using Drawing.RectangleF class vb.net – C#.net.

Author: Shehzad Hemani
Download Source Code : 1038_source.zip

In this simple article you will learn that how we can check that rectangles intersect and how to inflate rectangle using Drawing.RectangleF class in VB and C# .net.

 

Intersection:
If you want to check that two rectangles intersect with each other or not, for this purpose rectanglef class provides us a function named IntersectsWith(). This function takes an object of rectanglef class and returns a Boolean value. It checks intersection between function calling object and parameter object. If both intersect then it returns true else it returns false.
To demonstrate make a window application. Drag one button on form.
Now write the following code on Button click event:
C#
private void btn_intersect_Click(object sender, EventArgs e)
        {
            System.Drawing.RectangleF r = new RectangleF(20f, 20f, 20f, 20f);
            System.Drawing.RectangleF r1 = new RectangleF(21f, 21f, 21f, 21f);
            bool b;
            b = r.IntersectsWith(r1);
            if (b == true)
                MessageBox.Show("rectangles intersect with each other");
            else
                MessageBox.Show("rectangles do not intersect with each other");           
        }
VB
Private Sub btn_intersect_Click(ByVal sender As ObjectByVal As EventArgs)
        
Dim As System.Drawing.RectangleF = New RectangleF(20!, 20!, 20!, 20!)
        
Dim r1 As System.Drawing.RectangleF = New RectangleF(21!, 21!, 21!, 21!)
        
Dim As Boolean
        
r.IntersectsWith(r1)
        
If (b = trueThen
            
MessageBox.Show("rectangles intersect with each other")
        
Else
            
MessageBox.Show("rectangles do not intersect with each other")
        
End If
    End Sub
This is simple code to check intersections between two rectangles.
Inflate:
If you want to inflate a rectangle by given values of height and width, for this purpose rectanglef class provides us a function named inflate. This function has two overloaded methods. One takes object of sizef class and second takes two float values. This function returns nothing.
To demonstrate make a window application. Drag two text boxes and one button on form.
Now write the following code on Button click event:
C#
private void btn_inflate_Click(object sender, EventArgs e)
        {
            System.Drawing.RectangleF r = new RectangleF(20f, 20f, 20f, 20f);
            if (txt_height.Text != string.Empty && txt_width.Text != string.Empty)
            {
                float w = 0f;
                float h = 0f;
                string str = txt_width.Text;
                w = (float)Convert.ToSingle(str);
                 str = txt_height.Text;
                h = (float)Convert.ToSingle(str);
                SizeF sf = new SizeF(w, h);
                r.Inflate(sf);
                MessageBox.Show("Inflated");
            }
            else
                MessageBox.Show("please enter values");
            }
VB 
    
Private Sub btn_inflate_Click(ByVal sender As ObjectByVal As EventArgs)
        
Dim As System.Drawing.RectangleF = New RectangleF(20!, 20!, 20!, 20!)
        
If ((txt_height.Text <> string.Empty)  _
                    
AndAlso (txt_width.Text <> string.Empty)) Then
            Dim 
As Single = 0!
            
Dim As Single = 0!
            
Dim str As String = txt_width.Text
            w 
= CType(Convert.ToSingle(str),Single)
            
str = txt_height.Text
            h 
= CType(Convert.ToSingle(str),Single)
            
Dim sf As SizeF = New SizeF(w, h)
            r.Inflate(sf)
            MessageBox.Show(
"Inflated")
        
Else
            
MessageBox.Show("please enter values")
        
End If
    End Sub
This is simple code to inflate rectangle.
Now write the following code on FORM LOAD event:
C#
private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "Devasp Rectangle Application";             
        }
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        Me.Text = "Devasp Rectangle Application" 
    End Sub
This simple article tells that how we can check that rectangles intersect or not and how to inflate rectangle using Drawing.RectangleF class in VB and C# .net.
   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Take union and adjust location of rectangles using Drawing.RectangleF class vb.net – C#.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net