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.


Results 1 to 6 of 6

Thread: You Good at Vectors?

  1. #1

    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
    http://blueflamedev.com
    Blueflame Development

  2. #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)
    rotate(angleInRadians);
    translate(posX, posY);

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

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

  4. #4

  5. #5
    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();
    				levelView.addChild(_lov);
    				_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
    http://blueflamedev.com
    Blueflame Development

  6. #6
    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.
    http://blueflamedev.com
    Blueflame Development

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012