Modify affine transformation matrix to match change in shape origin

999 Views Asked by At

Given I have a 2D affine transform described by the matrix:

[ a  c  tx ]
[ b  d  ty ]
[ 0  0  1  ]

Such that:

[ x ]   [ a  c  tx ] [ x ]   [ a * x + c * y + tx ]
[ y ] = [ b  d  ty ] [ y ] = [ b * x + d * y + ty ]
[ 1 ]   [ 0  0  1  ] [ 1 ]   [         1          ]

I have a shape that starts at [-(width/2), -(height/2)] so that it is centred about [0, 0].

I want to change the origin of the shape so that it starts at [0,0] but I want its transformed coordinates to be the same.

How do I update the matrix to map the objects new origin to the same end position?

I seem to be failing at the point where when x or y equals = 0 everything just cancels out and becomes:

[ tx ]
[ ty ]
[ 1  ]
1

There are 1 best solutions below

0
On

I realised the way to do this is to form a new matrix:

[ a  c  ox ]
[ b  d  oy ]
[ 0  0  1  ]

Where:

  • w = -(width / 2)
  • h = -(height / 2)
  • ox = tx + aw + ch
  • oy = ty + bw + dh