Node:Applying Transforms to Points Intro, Next:, Previous:Transforms, Up:Transforms



Applying Transforms to Points

A Transform t is applied to a Point P using the binary *= operation (Point::operator*=(const Transform&)) which performs matrix multiplication of P.transform by t. See Point Reference; Operators.

     Point P(0, 1);
     Transform t;
     t.rotate(90);
     t.show("t:");
     -| t:
        1       0       0       0
        0       0      -1       0
        0       1       0       0
        0       0       0       1
     P *= t;
     P.show_transform("P:");
     -| P:
     Transform:
        1       0       0       0
        0       0      -1       0
        0       1       0       0
        0       0       0       1
     P.show("P:");
     -| P: (0, 0, -1)
     

In the example above, there is no real need to use a Transform, since P.rotate(90) could have been called directly. As constructions become more complex, the power of Transforms becomes clear:

     1. Point p0(0, 0, 0);
     2. Point p1(10, 5, 10);
     3. Point p2(16, 14, 32);
     4. Point p3(25, 50, 99);
     5. Point p4(12, 6, 88);
     6. Transform a;
     7. a.shift(2, 3, 4);
     8. a.scale(1, 3, 1);
     9. p2 *= p3 *= a;
     10. a.rotate(p0, p1, 75);
     11. p4 *= a;
     12. p2.show("p2:");
        -| p2: (18, 51, 36)
     13. p3.show("p3:");
        -| p3: (27, 159, 103)
     14. p4.show("p4:");
        -| p4: (24.4647, -46.2869, 81.5353)
     

In this example, a is shifted and scaled, and a is applied to both in line 9. This works, because the binary operation operator*=(const Transform& t) returns t, making it possible to chain invocations of *=. Following this, a is rotated 75 degrees

about the line through p_0 and p_1. Finally, all three transformations, which are stored in a, are applied to p_4.