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:
Creating a new Rectangle with 4-int values:
Setting Bottom Value of a Rectangle:
Setting Center Value of a Rectangle:
Know whether the Rectangle has a specific Point.
Know whether Rectangle has another Rectangle value:
Know whether Rectangle has 2-int values(Point):
Know whether Rectangle has a reference Point and the result:
Know whether Rectangle has a reference Rectangle and the result:
Setting the Height value of Rectangle:
Pushing the edges of Rectangle out:
Know whether Rectangle Intersects another Rectangle:
Know whether Rectangle intersects a reference Rectangle with its result value:
Getting value whether Rectangle is empty or not:
Getting the Left value of the Rectangle:
Get or Set Location of the Rectangle:
Set Position of the Rectangle with a Point value:
Set Position of the Rectangle with 2-int values:
Getting Right value of the Rectangle:
Getting Top value of the Rectangle:
Setting the Width value of the Rectangle:
Setting the X value of the Rectangle:
Setting the Y value of the Rectangle:
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!