Hello,
I'm wondering if anyone knows how to get the center point of a rectangle after it's been rotated. I've Changed the transform settings to "Preserved" in document properties so it always saves the starting x,y,width,height and then I just have to deal with the matrix. What I'm basically trying to do is use Inkscape as a level editor for a game I'm working on. I have everything setup but rotated rectangles. (I am using the rectangle data w/ tranformation matrix to get my collision data for a physics engine). I believe if I could get the center point of the rectangle I would be all set but I'm not sure how to get that from the original position or using the transform since the width of the rectangle changes from the scaling of the matrix.
so basically if I had the following.
start X = 0
start Y = 0
start width = 100
start height = 100
transform = matrix(0.91158676,0.41110775,-0.41110775,0.91158676,24.97605,-16.134726)
is there a way to extract the center point after the transform is applied?
I have tried looking around and reading up on matrices but I am still having a hard time with this. my game uses origin as it's x,y coordinates which I can get with non rotated rectangles but when there is a matrix I have no Idea how to get that.
I really appreciate any help/advice on the matter.
Thanks,
Artimus
Center point after rotation
Re: Center point after rotation
How to find the new co-ordinates from a transform matrix is given in the SVG Spec, esspecially this image:
(link)
I put the example you gave on my wiki talk page here: http://en.wikipedia.org/wiki/User_talk: ... sformation . It goes through step by step how to calculate the new centre of a rectangle.
If you don't understand matrix maths, have a look here.
(link)
I put the example you gave on my wiki talk page here: http://en.wikipedia.org/wiki/User_talk: ... sformation . It goes through step by step how to calculate the new centre of a rectangle.
If you don't understand matrix maths, have a look here.
Last edited by tomh on Sun Jun 27, 2010 11:21 am, edited 2 times in total.
Re: Center point after rotation
Oh, here is the svg code from my example after the transformation matrix is applied. The top-left corner of the red rectangle is at centre of the grey rectangle.
Basically the origin in SVG is the top-left corner, but when you change the page size in Inkscape, it adjusts the page border from the bottom left corner, without updating the svg origin, so the Inkscape and SVG origins can differ after changing the page size! See the image below (in Inkscape) for what I mean:
Code: Select all
<rect style="color:#000000;fill:#999999;stroke:none;stroke-width:6;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate" id="rect4889" width="200" height="100" x="0" y="0" transform="matrix(1,0.5,-0.5,1,25,-20)" />
<rect style="color:#000000;fill:#ff0000;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:6;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate" id="rect4893" width="31.428572" height="31.428572" x="100" y="80" 80="150" />
Off topic:
And here is a Gottua, which screws up all the above if you aren't careful: Inkscape's non stranded origin. Basically the origin in SVG is the top-left corner, but when you change the page size in Inkscape, it adjusts the page border from the bottom left corner, without updating the svg origin, so the Inkscape and SVG origins can differ after changing the page size! See the image below (in Inkscape) for what I mean:
Re: Center point after rotation
Thank you very much. This is exactly what I needed.
question about width and height after rotation
I thought I'd try and figure this out on my own but I've been reading around at different websites trying to figure this out and now I am rather confused. Kind of coincides with the same thing.
Here's the situation given the same example:
start X = 0
start Y = 0
start width = 100
start height = 100
say I scaled the rectangle first by 2 along X:
scale(2,1)
Now I want to rotate it by a certain degree say 25 which gives me the following transform:
matrix(1.8126156,-0.84523652,0.42261826,0.90630779,-11.761692,46.946437)
since the scale number changes since we rotated the rectangle how do I get the final width/height out of the transformation matrix. Sorry these are probably pretty noobish questions but I've never done matrix math and am pretty confused about what happens after a rotation occurs. I know how to handle them without the rotation but the rotation really screws it up for me
Thanks,
Artimus
Here's the situation given the same example:
start X = 0
start Y = 0
start width = 100
start height = 100
say I scaled the rectangle first by 2 along X:
scale(2,1)
Now I want to rotate it by a certain degree say 25 which gives me the following transform:
matrix(1.8126156,-0.84523652,0.42261826,0.90630779,-11.761692,46.946437)
since the scale number changes since we rotated the rectangle how do I get the final width/height out of the transformation matrix. Sorry these are probably pretty noobish questions but I've never done matrix math and am pretty confused about what happens after a rotation occurs. I know how to handle them without the rotation but the rotation really screws it up for me
Thanks,
Artimus
Re: Center point after rotation
Never mind, I figured it out pretty easy actually I just wasn't thinking.
I just created vertices off of the first x,y and used your same formula then caluclated the distance accordingly.
I just created vertices off of the first x,y and used your same formula then caluclated the distance accordingly.