Pregunta

I need to parse SVG (just the simples things) and only thing left to do is to properly extract position and angle from the matrix transformation. I know this question has been asked many times and I believe I have went through many of the answers, documents etc. but still cannot handle it proporly. Here's the simplest example I managed to prepare:

I have created 1000x1000 document (all numbers in px) and put a rectangle of 100x100 size at 100,100 position. It has generated the following piece of SVG file (I have removed style attrib. and parent tags). There's no other transformation anywhere in the file:

    <rect
       width="100"
       height="100"
       x="100"
       y="100" />

Then I have rotated the rectangle by 33deg (with the 'transform' inkscape tool). The SVG code looks this:

   <rect
       width="100"
       height="100"
       x="-5.8952699"
       y="157.49644"
       transform="matrix(0.83867057,-0.54463904,0.54463904,0.83867057,0,0)" />

Now, my goal is to extract the position and angle from the matrix, so basically I'd like to get back the following values: x:100,y:100,angle:33. In order to do it, I have assumed the following formulas:

sx=sqrt(a^2+b^2)
sy=sqrt(c^2+d^2)

t=atan(c/d) OR t=atan(-b/a)
t=acos(a) if MATRIX is PURE

x' = tx + sx*(COS(t)*x-SIN(t)*y)
y' = ty + sy*(SIN(t)*x+COS(t)*y)

the result is: t = 0.575958787 (which is 33deg) - PERFECTLY FINE

however x'=-90.72230563 and y'=128.8770646 and this is exactly what totally confuses me - why it's not 100,100 ?

¿Fue útil?

Solución

Here's how you can find out the coordinates. I'm using Python for my calculations here.

The matrix transform is applied with the centre of rotation being (0, 0). The coordinates (-5.895, 157.496) are where the square will need to be so that if you rotate the rectangle 33 degrees with the centre of rotation at (0, 0), it will end up rotated 33 degrees about its centre.

First, it seems you don't have a problem figuring out the angle of rotation. We'll just concentrate on how to figure out the position.

Let's start with what we know:

Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> c = math.cos(33 * math.pi / 180)
>>> s = math.sin(33 * math.pi / 180)
>>> c
0.838670567945424
>>> s
0.5446390350150271
>>> x0 = -5.8952699
>>> y0 = 157.49644

These are the coordinates of the top-left corner of the square, before it has been rotated. The square is rotated about its centre, and we want to find out where its centre is rotated to:

>>> x1 = x0 + 50
>>> y1 = y0 + 50
>>> x1
44.1047301
>>> y1
207.49644

Now, rotate the centre of the square:

>>> x2 = c * x1 + s * y1
>>> y2 = -s * x1 + c * y1
>>> x2
149.9999998927001
>>> y2
149.9999995401914

It's clear from here how we get to where the top-left corner of the square would be if the square wasn't rotated.

>>> x3 = x2 - 50
>>> y3 = y2 - 50
>>> x3
99.99999989270009
>>> y3
99.9999995401914
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top