How to use Rectangle in XNA


In this mini-article I will be showing how to use Rectangle.

Lets take a look at Rectangle's Structure first:

Creating a new Instance(null) of a Rectangle:

1.gif
 
Creating a new Rectangle with 4-int values:

2.gif
 
Setting Bottom Value of a Rectangle:

3.gif
 
Setting Center Value of a Rectangle:

4.gif
 
Know whether the Rectangle has a specific Point.

5.gif
 
Know whether Rectangle has another Rectangle value:

6.gif 

Know whether Rectangle has 2-int values(Point):

7.gif
 
Know whether Rectangle has a reference Point and the result:

8.gif
 
Know whether Rectangle has a reference Rectangle and the result:

9.gif
 
Setting the Height value of Rectangle:

10.gif
 
Pushing the edges of Rectangle out:

11.gif
 
Know whether Rectangle Intersects another Rectangle:
 
12.gif

Know whether Rectangle intersects a reference Rectangle with its result value:

13.gif 

Getting value whether Rectangle is empty or not:

14.gif
 
Getting the Left value of the Rectangle:

15.gif
 
Get or Set Location of the Rectangle:

16.gif
 
Set Position of the Rectangle with a Point value:

17.gif
 
Set Position of the Rectangle with 2-int values:

18.gif
 
Getting Right value of the Rectangle:

19.gif
 
Getting Top value of the Rectangle:

20.gif
 
Setting the Width value of the Rectangle:

21.gif
 
Setting the X value of the Rectangle:

22.gif
 
Setting the Y value of the Rectangle:

23.gif 

So we now know the Structure of the Rectangle,lets use them in a sample:

Rectangle rt = new Rectangle(); //null instance
Rectangle rt2 = new Rectangle(100, 200, 200, 200); //4-int instance
int bottomval = rt.Bottom; //get Bottom value
Point centerval = rt.Center; //get Center value
Point r=new Point(30,50);
if (rt.Contains(r)) //if rt has a r point
{
}
else if (rt.Contains(rt2)) //if rt has another rectangle named rt2
{
}
else if (rt.Contains(10, 30)) //if rt has 2-int value which is a Point
{
}
rt.Height = 100; //set height
rt.Inflate(100, 200); //inflate the rectangle with given integers
if (rt.Intersects(rt2)) //if rt intersects another
{
}
if (rt.IsEmpty) //if rt is Empty
{
}
int leftval = rt.Left; //get left value of rectangle
Point Locationval = rt.Location; //get location of rectangle
Point changepos = new Point(200, 300); //position variable
rt.Offset(changepos); //change position with a point variable
rt.Offset(200, 300);  //change position with a 2-int variable
int rightval, topval; //right and top variables
rightval = rt.Right;  //get right variable
topval = rt.Top;      //get top variable
rt.Width = 300;       //set width
rt.X = 100;           //set X
rt.Y = 100;           //set Y

I have tried to explain how to use rectangle in XNA.

Hope it helps!

Up Next
    Ebook Download
    View all
    Learn
    View all
    Araf Global is a software consultancy company founded in 2016 focusing on cutting edge technologies.