The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.

# Thread: You Good at Vectors?

1. 42
posts
Registered User

## You Good at Vectors?

I got this project. I'm working with rotated rectangles a lot. I'm drawing them (using the graphics API) so that their X and Y are centered withing the rectangle.

I'm often needing to figure out these regtangles' corner coordinates. I'd like to make some getters that spit out the coordinates of a corner such as
Code:
`num = rect.upperLeft`
. Anyone have the Vector skills to show me the pattern to getting these values? I'm decent with vectors, but apparently not good enough. My Vector class has functions to set angle, length, rotate(), plus, minus, normal, etc, so feel free to use those in your example.

Thanks,
Eric

2. Use a matrix to transform the points.

if you have the points
(-10,-10), (10,-10), (10,10),(-10,10)
then to get the points you apply a matrix to it that has the transformation
translate(rotationPointX, rotationPointY); //not really needed if you declare the points around the (0,0)
translate(posX, posY);

Flash has a Matrix class in flash.geom.Matrix;

3. (sorry - posting in the wrong thread - oops)
Last edited by DiamondDog; June 11th, 2008 at 03:18 PM.

4. 42
posts
Registered User
Thanks Sirisian, I've applied the usage that you gave me, but I don't see any explanation of how to retrieve the corner points of a rotated rectangle. It seems that you showed me how to rotate it (and translate it) but not how to get coordiantes for corners (unless I'm missing something).

The code here occurs every time the mouse is clicked and dragged. The point at which it is clicked gives us the _startPoint Vector. Using the difference between the the mouse's start point and it's current point will give me the length and rotation of the rectangle (The height is fixed). The rectangle is named "_lov".

Code:
```private function onMouseMove(event:MouseEvent):void
{
//create the object the first time the mouse moves
if (!_lov)
{
_lov = new LevelObjectView();
_lov.explicitHeight = 10;
}
var distance:Vector = new Vector(levelView.mouseX - _startPoint.x, levelView.mouseY - _startPoint.y);
_lov.explicitWidth = distance.length;
var matrix:Matrix = new Matrix();
matrix.rotate(distance.angle);
//below here is all code for setting the x, y location of the rotate object.
var center:Vector = _startPoint.plus(distance.scale(.5));
var vectToCenter:Vector = distance.normal;
vectToCenter.length = _lov.explicitHeight / 2;
center.plusEquals(vectToCenter);
matrix.translate(center.x, center.y);
_lov.transform.matrix = matrix;
}```
What I'd like to do is be able to access that rectangle's corner points any time. All the info that I have about it is its explicit width, height, x, y, and rotation.

Any help would be GREATLY appreciated. Thanks,

Eric

5. 42
posts
Registered User
I figured out something that works. Let me know if there's a better way:

Code:
`var point:Point = transform.matrix.transformPoint(new Point(-explicitWidth / 2, explicitHeight / 2));`
However the strange thing is that if I try to access this value every mouse move, the very first two times that it traces out (after the matrix has been made and assigned to the object's tranform property), I get this strange value:

[-107374182.4, -107374187.4]

Any clue why? Thanks.