1 USING: accessors arrays combinators combinators.short-circuit kernel math math.vectors
2 math.functions sequences ;
3 IN: math.affine-transforms
5 TUPLE: affine-transform x y origin ;
6 C: <affine-transform> affine-transform
8 CONSTANT: identity-transform T{ affine-transform f { 1.0 0.0 } { 0.0 1.0 } { 0.0 0.0 } }
11 [ [ x>> ] [ first ] bi* v*n ]
12 [ [ y>> ] [ second ] bi* v*n ]
13 [ drop origin>> ] 2tri
16 : <translation> ( origin -- a )
17 [ { 1.0 0.0 } { 0.0 1.0 } ] dip <affine-transform> ;
18 : <rotation> ( theta -- transform )
20 [ 2array ] [ neg swap 2array ] 2bi { 0.0 0.0 } <affine-transform> ;
21 : <scale> ( x y -- transform )
22 [ 0.0 2array ] [ 0.0 swap 2array ] bi* { 0.0 0.0 } <affine-transform> ;
24 : center-rotation ( transform center -- transform )
25 [ clone dup ] dip [ vneg a.v ] [ v+ ] bi >>origin ;
27 : flatten-transform ( transform -- array )
28 [ x>> ] [ y>> ] [ origin>> ] tri 3append ;
31 [ [ x>> first ] [ y>> second ] bi * ]
32 [ [ x>> second ] [ y>> first ] bi * ] bi - ;
34 : (inverted-axes) ( a -- x y )
35 [ [ y>> second ] [ x>> second neg ] bi 2array ]
36 [ [ y>> first neg ] [ x>> first ] bi 2array ]
40 : inverse-axes ( a -- a^-1 )
41 (inverted-axes) { 0.0 0.0 } <affine-transform> ;
43 : inverse-transform ( a -- a^-1 )
44 [ inverse-axes dup ] [ origin>> ] bi
47 : transpose-axes ( a -- a^T )
48 [ [ x>> first ] [ y>> first ] bi 2array ]
49 [ [ x>> second ] [ y>> second ] bi 2array ]
50 [ origin>> ] tri <affine-transform> ;
54 [ [ x>> ] [ x>> ] bi* v. ]
55 [ [ x>> ] [ y>> ] bi* v. ]
56 [ [ y>> ] [ x>> ] bi* v. ]
57 [ [ y>> ] [ y>> ] bi* v. ]
60 [ [ 2array ] 2bi@ ] dip <affine-transform> ;
62 : v~ ( a b epsilon -- ? )
65 : a~ ( a b epsilon -- ? )
67 [ [ [ x>> ] bi@ ] dip v~ ]
68 [ [ [ y>> ] bi@ ] dip v~ ]
69 [ [ [ origin>> ] bi@ ] dip v~ ]