Creating a fixed thickness Rectangle between two points

J

John

Hi there,

I need to create a rectangle between two points so that I can check what
is inside it using Contains(). The problem I am having is how to make the
rectangle be able to cope with the angle between the two points, see the
image in the link for what I mean.

http://homepage.ntlworld.com/andrew.baldock/bloodbowl/select.png

I am not interested in displaying anything in the rectangle, I just need
it for Contains(). The start and end points can be anywhere on the grid
and the rectangle is always the same thickness.

Any help would be appreciated.

Cheers,
John
 
P

Peter Duniho

John said:
Hi there,

I need to create a rectangle between two points so that I can check what
is inside it using Contains(). The problem I am having is how to make
the rectangle be able to cope with the angle between the two points, see
the image in the link for what I mean.

http://homepage.ntlworld.com/andrew.baldock/bloodbowl/select.png

I am not interested in displaying anything in the rectangle, I just need
it for Contains(). The start and end points can be anywhere on the grid
and the rectangle is always the same thickness.

Any help would be appreciated.

You may find the Matrix class useful. With it, you can define
coordinate system transformations, and translate points or vectors. In
this case, I would set up a Matrix that will translate from the
coordinate system in which the point to be check exists, to a coordinate
system in which your rectangle is in fact a non-rotated rectangle. Then
you can easily use the Rectangle.Contains() method to check for containment.

You can use the Math class functions to derive an angle based on the
points defining the ends of your rectangle, then use that angle to
initialize the Matrix.

Finally, you'd use the Matrix to translate the points defining the
rectangle, as well as the point to be tested, then test for containment
using those translated values.

Pete
 
B

Ben Voigt [C++ MVP]

John said:
Hi there,

I need to create a rectangle between two points so that I can check what
is inside it using Contains(). The problem I am having is how to make the
rectangle be able to cope with the angle between the two points, see the
image in the link for what I mean.

http://homepage.ntlworld.com/andrew.baldock/bloodbowl/select.png

I am not interested in displaying anything in the rectangle, I just need
it for Contains(). The start and end points can be anywhere on the grid
and the rectangle is always the same thickness.

To test a point P:

Call O the midpoint of AB. Find the projection of OP onto AB:

2 * len(AB) * parallel_distance = (2 * P.X - A.X - B.X) * (B.X - A.X) + (2 *
P.Y - A.Y - B.Y) * (B.Y - A.Y)

Test if |parallel_distance| > len(AB) / 2 means test if |2 * len(AB) *
parallel_distance| > len(AB) * len(AB)

Find the projection of OP onto the normal:

len(AB) * perpendicular_distance = (P.X - A.X) * (B.Y - A.Y) - (P.Y - A.Y) *
(B.X - A.X)

Test if |perpendicular_distance| > thickness/2



Compute only once per rectangle:

abX = B.X - A.X
abY = B.Y - A.Y
lenABsquared = (B.X - A.X) * (B.X - A.X) + (B.Y - A.Y) * (B.Y - A.Y)
scaledThickness = thickness * Math.Sqrt(lenABsquared) / 2

The point P falls outside if and only if:

outside = (Math.Abs((2 * P.X - A.X - B.X) * abX + (2 * P.Y - A.Y - B.Y) *
abY) > lenABsquared) || (Math.Abs((P.X - A.X) * abX - (P.Y - A.Y) * abY) >
scaledThickness)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top