If I have a SVG rectangle with
[x1,y1,x2,y2] = [ 456.079, 402.645, 514.841, 527.925 ]
and its SVG matrix
[0, 1, -1, 0, 58.7617, 0]
How can I calculate x, y, w, h
of the rectangle?
If I have a SVG rectangle with
[x1,y1,x2,y2] = [ 456.079, 402.645, 514.841, 527.925 ]
and its SVG matrix
[0, 1, -1, 0, 58.7617, 0]
How can I calculate x, y, w, h
of the rectangle?
The matrix is direct 2D homogenuous 3x3 transform matrix where the last 3 missing elements are implicitly (0,0,1)
as there is no projection in SVG. Its basically a 2D version of this.
So you need to implement 3x3 matrix * 3D vector multiplication V' = M*V where matrix M
is your 6 values + (0,0,1)
and 3D vector V
is (x,y,w=1)
the result is (x',y',w) where you can ignore the w'
. So when written as code (taken from mine C++ SVG decoder/encoder I wrote few years ago) to transform vertexes just do this:
x'=M[4]+(M[0]*x)+(M[2]*y);
y'=M[5]+(M[1]*x)+(M[3]*y);
If you have also scale present then:
x'=M[4]+scalex*((M[0]*x)+(M[2]*y));
y'=M[5]+scaley*((M[1]*x)+(M[3]*y));
Where:
M[6]={ 0, 1, -1, 0, 58.7617, 0 };
Also if you want to transform vectors (or sizes like width,height,radius) instead of vertexes then the w=0
so the code ignores offset:
x'=(M[0]*x)+(M[2]*y);
y'=(M[1]*x)+(M[3]*y);
If you have also scale present then:
x'=scalex*((M[0]*x)+(M[2]*y));
y'=scaley*((M[1]*x)+(M[3]*y));
so transform position (x1,y1)
with offset and (x2,y2)
without (as its the width,height).
x=M[4]+(M[0]*x1)+(M[2]*y1);
y=M[5]+(M[1]*x1)+(M[3]*y1);
w= (M[0]*x2)+(M[2]*y2);
h= (M[1]*x2)+(M[3]*y2);
In case your SVG object parents has more matrices you need to apply them in order too.