Create a rectangle to cover a rotated Microsoft.XNA.Framework.Rectangle - c#

If I have a Rectangle class in C# how can I get a rectangle which encompasses it when it's rotated?
Basically I want to find the rectangle for a rotated rectangle.

I'm curious how do you keep your Rectangle as a data structure, I mean, a rectangle is 2D and XNA makes me think about 3D.
However, even in 2D and 3D, i think what you want is called AABB (axis aligned bounding box), which is very easy to find because it is defined by the two points formed by the minimums and, respective, maximums on each axis of each point of the original rectangle transformed with your rotation.
LATER EDIT:
For a Rectangle structure that contains X, Y, Width and Height, the rectangle has this two points:
(x1, y1) = (X, Y) and
(x2, y2) = (X + Width, Y + Width).
When you rotate the rectangle, you practically rotate this two points and obtain:
(xr1, yr1) = rotate(x1, y1)
(xr2, yr2) = rotate(x2, y2).
Now, the rectangle you want is defined by the points of these coordinates:
p1 = new Point(Min(xr1, xr2), Min(yr1, yr2))
p2 = new Point(Max(xr1, xr2), Max(yr1, yr2))
rotate is the method that rotates your rectangle by an angle around a certain point.

How are you rotating the rectangle to start with? to get the bounding box you need to look at the four resulting points and find the min and max for Y and X,

Related

Drawing an arc with negative rectangle c#

I'm trying to draw an arc of a circle in C#. The general code to do this is:
e.DrawArc(pen, x, y, d, d, startAngle, endAngle - startAngle);
Where x and y indicate the upper left corner of the Rectangle and d the width of the Rectangle (also the diameter of the arc).
The problem I'm facing is that sometimes I need to draw an arc whoose rectangle x and y values lie outside the bitmap I'm drawing onto (they may even be negative), and thus the arc is not being drawn at all.
Any ideas?
Thanks!
Okay, so I solved it using the Graphics.DrawCurve(pen, points[]) method. I calculated multiple points of the circle doing some math and plotted them as a curve. The result is pretty neat (around 20 points I can't tell that it's not an actual circle), and it works no matter where I want to draw the arc.

Find the angle between any point inside a circle and the up vector direction

I have a circle with the up vector direction is Vector(0, -1) and a point, B which could be anywhere on that circle. I know its exact position inside the circle. The (0,0) point is located on the top left of the image.
How can I find the angle x between the point B and the up vector, relative to center of the circle?
I am developing a game in XNA and C#.
Update: I do not understand why this question is marked as not about programming. Anyway here's what I have done so far.
I can find the radian between two vectors
private float radianBetweenVectors(Vector2 a, Vector2 b)
{
return (float)Math.Atan2(b.Y - a.Y, b.X - a.X);
}
But I do not want to know about the location of the up vector on the sprite image if possible. The location here is the point at which it starts at 0 degree on the circle's circumference .
Update 2:
Once I have the angle, I want to obtain the rotation matrix:
Matrix rotMatrix = Matrix.CreateRotationZ(angle);
Now I have this matrix containing the rotation, I can ask XNA to transform the Up vector with this rotation:
moveDirection = Vector2.Transform(up, rotMatrix);
This I hope will take the original up direction, transform it with the rotation around the Z axis. I am still trying to figure out if this is the correct approach.
Find relative x and y coordinates of point B from center of the circle. (You can do this if you know the radius of the circle). If the circle diameter is the width of the sprite, just divide width by two to get radius. Then use arc tangent.
I'll try to help you out since I started XNA when I was relatively young and before I had any experience with vectors and you might be in the same position I was in 5 years ago.
If you don't understand the math behind it, you just treat point B as a point on a triangle that is formed from the 3 lines. I'll use (0,0) as the center of the circle, you can calculate the relative position of the center of the circle from the absolute position given radius. (If you can't do this part, you'll need to pay more attention in math class).
(0,0) to (0, B.y) will be the vertical leg.
(0, B.y) to (B.x, B.y) will be the horizontal leg
(B.x, B.y) to (0,0) will be the hypotenuse.
Then you use tangent with the rule tangent = opposite/adjacent. So you will know the distance of the opposite leg from the angle X is the distance from (0, B.y) to (B.x, B.y), the horizontal leg. The adjacent leg distance is (0,0) to (0, B.y). No need for distance formula, just subtract the coordinates. Now you have the value of tangent and want to find the value of X, so we'll use arctan. Math.Atan2(opposite/adjacent). That will give you your answer in radians.
Edit: Oh and I remember you can draw the sprite so the center of the sprite will be rendered at the center of the sprite bitmap using the origin argument of draw. That way you don't have to worry about calcuating the center of the circle.
Speaking mathematically, if you have two vectors, lets supose A(0, -1) and B(222, 90) the angle X between the two vectors can be computed as cos(X)=(A.B)/(||A||.||B||).
A x B = (a1 x b1) + (a2 x b2) = 0x222 + (-1x90) = 0 - 90 = -90.
2.I. ||A|| = squareroot[0^2+(-1)^2] = sqrt[(-1)^2] = 1
2.II. ||B|| = squareroot[222^2+90^2] = sqrt(57384) ~= 239.55 (~= is approximately)
2.III. ||A||.||B|| = 1 x 239.55 ~= 239.55
Result: cos(X) ~= (-90)/(239.55) ~= X ~= cos^-1(X) ~= 1.955 radian.
For the vectors you gave above, the angle you looking for is approximately 1.955 radian.
Note: using a calculator, you will obtain the exact value, which is very close to the aproximate value.

Maximize Rectangle in an Area on the X, Y plane with a list of points representing the area

Okay, I need help maximizing the area of a rectangle in the sliver object that can be many different shapes. I've already done most of the work.
I'm working in C# with a kinect and the depth pixels.
This image is just the best representation of what I'm talking about that I could find.
I need to produce a rectangle in this area on the x, y plane that maximizes the area while having every edge be solid.
I already have the solid shape represented as a list of points on the x, y plane. So I have the shape on the 2D plane. Kind of like this:
To simplify the problem, I'm just going to produce a rectangle with edges that are parallel to the x and y lines.
I'm not looking for code. I just need some direction or an algorithm that I can read up on and attempt to implement.
If any clarification is needed, please let me know.
If you just needs bounding rectangle you can traverse the list of points. Store minimum and maximum of X and Y (like minX, minY, maxX, maxY). Go through the whole list, and do a 4 if check on each of points like:
if(point.X < minX) minX = point.X
if(point.Y < minY) minY = point.Y
if(point.X > maxX) maxX = point.X
if(point.Y > maxY) maxY = point.Y
This will give you the bounding rectangle. Min and Max are two points that represent, depending on your coordinate system, upper left and lower right point, which you can easily use to draw the rectangle.
Have a look at the Largest Empty Rectangle problem. Here is one article with an algorithm to compute its solutions.
More precisely, it is a problem that consists in searching the largest area that does not contain any point given a set of points. In your case you can apply it to some points that would describe the edge of your 2D shape, plus some points on the outside. Then the procedure of the article would compute the largest rectangle in the yellow area. I did apply such a procedure to find the largest part of an image that does not contain transparent pixels. It worked well but was a bit slow.
The solution applies to a set of points (i.e. no segments) but you can easily compute an approximate solution with an approximation of your shape.

Calculating the x and y of rotated image

When we rotate any object like Image then its x and y will also change according to the
roation angle.
I just want to know how we can calculate the outer rectangle x and y ( pointed with red
circle in the image ) while rotating the image. We have image width, height and
rotation angle ?
x - your x coordinate
height - height of your image
angle - rotation angle
Then you need (x - height * sin(angle))
One usually uses a Rotation Matrix for such a thing.
In linear algebra, a rotation matrix is a matrix that is used to
perform a rotation in Euclidean space
This web page contains a section on rotation which should be useful to explain the concept.

How to hide/not draw part of the model

I have an animated model that's spinning.
I want to hide/not draw any part of the model that's Y<0
what are the ways I can do it?
ideas:
1) draw a giant rectangular box right below y=0
2) tweak the camera matrix so that y<0 is outside of clipping plane (but i have no idea how)
can someone point me into the right direction? =)
A purely mathematical approach:
Don't draw the polygons whose y's are all less than 0.
Draw the polygons whose y's are all greater than or equal to 0.
Clip the rest of the polygons with the y=0 plane and draw them.
If the polygons making up the model are triangles, clipping them is pretty trivial. You need to clip the two sides intersecting with the y=0 plane and replace the original vertices whose y's are less than 0 with the intersection points of those two sides with the clipping plane.
Use the line equations:
(x-x1) = (x2-x1)*(y-y1)/(y2-y1)
(z-z1) = (z2-z1)*(y-y1)/(y2-y1)
where 1 and 2 are the vertices of the side being clipped by the y=0 plane. Substitute their coordinates (x1, y1, z1, x2, y2, z2) and y=0 into the equations to get x and z of the intersection point. Use this point's coordinates instead of vertex 1's or 2's (whichever has y < 0).
If the polygons are texture-mapped, you'll need to recalculate the texture coordinates for the vertices that you got from the clipping. You do that in the same fashion.
It sounds like you need to introduce MSDN Bounding Frustum
Here is a good tutorial from Nic's GameDev Site.

Categories