File: devguide-7.html

package info (click to toggle)
sketch 0.6.13-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,284 kB
  • ctags: 8,453
  • sloc: python: 34,711; ansic: 16,543; makefile: 83; sh: 26
file content (220 lines) | stat: -rw-r--r-- 7,572 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<html>
<head>
<title>Developer's Guide: Transformation Objects
</title>
</head>
<body bgcolor=white text=black link=blue vlink=navy alink=red>
<TABLE WIDTH="100%">
<TR>
<TH ALIGN="left" WIDTH="33%"><img SRC="Images/arrow-left.png" WIDTH="16" HEIGHT="16" ALIGN="top" ALT="Prev"></TH>
<TH ALIGN="center" WIDTH="33%"><img SRC="Images/arrow-up.png" WIDTH="16" HEIGHT="16" ALIGN="top" ALT="Up"></TH>
<TH ALIGN="right" WIDTH="33%"><img SRC="Images/arrow-right.png" WIDTH="16" HEIGHT="16" ALIGN="top" ALT="Next"></TH>
</TR>
<TR>
<TD ALIGN="left"><A HREF="devguide-6.html">Rect Objects
</A></TD>
<TD ALIGN="center"><A HREF="devguide-4.html">Coordinate Systems
</A></TD>
<TD ALIGN="right"><A HREF="devguide-8.html">Curve Objects
</A></TD>
</TR>
</TABLE>
<HR NOSHADE>
<H2><FONT face="Helvetica,Arial"><A NAME="N1"></A>Transformation Objects
</font></H2>

<P>A transformation object, a trafo object or trafo for short, represents
an affine 2D transformation. Such a transformation is given by 6
numbers: <i>m11</i>, <i>m21</i>, <i>m21</i>, <i>m22</i>, <i>v1</i> and
<i>v2</i>. These coefficients are given in the same order as for a
PostScript transformation matrix.</P>
<P>To apply a trafo to an object you can simple call the trafo with that
object as argument. For details, see the <A HREF="#N9">section
``Operators''</A> below.</P>
<P>Trafo objects are immutable.</P>
<P>If such a transformation <i>T</i> is applied to the point (<i>x</i>,
<i>y</i>) you get</P>
<P>
<PRE>
            / x \   / m11 m12 \ / x \   / v1 \
        T * |   | = |         | |   | + |    |
            \ y /   \ m21 m22 / \ y /   \ v2 /
</PRE>

or, in homogeneous coordinates:</P>
<P>
<PRE>
            / x \   / m11 m12 v1 \ / x \
            |   |   |            | |   |
        T * | y | = | m21 m22 v2 | | y |
            |   |   |            | |   |
            \ 1 /   \ 0   0   1  / \ 1 /
</PRE>
</P>


<H3><FONT face="Helvetica,Arial"><A NAME="N2"></A>Constructors</font></H3>

<P>The following functions create trafo objects:
<DL>

<DT><B><A NAME="N3"></A><tt>Trafo(<i>m11</i>, <i>m21</i>, <i>m21</i>, <i>m22</i>, <i>v1</i>, <i>v2</i>)</tt></B><DD>
<P>The generic constructor: return the transformation given by the
coordinates <i>m11</i>, <i>m21</i>, <i>m21</i>, <i>m22</i>, <i>v1</i>
and <i>v2</i>. These coefficients are given in the same order as
for a PostScript transformation matrix.</P>


<DT><B><A NAME="N4"></A><tt>Scale(<i>factorx</i>[, <i>factory</i>])</tt></B><DD>
<P>Return a trafo object for a scaling by the factors <i>factorx</i>
(in x-direction) and <i>factory</i> (in y-direction). If omitted,
<i>factory</i> defaults to the value of <i>factorx</i>.</P>
<P>Equivalent to <CODE>Trafo(<i>factorx</i>, 0, 0, <i>factory</i>, 0, 0)</CODE>.</P>


<DT><B><A NAME="N5"></A><tt>Translation(<i>offset</i>)</tt></B><DD>

<DT><B><tt>Translation(<i>offset_x</i>, <i>offset_y</i>)</tt></B><DD>
<P>Return a trafo object for a translation by <i>offset</i> (a <A HREF="devguide-5.html#N11">PointSpec</A>) or (<i>offset_x</i>, <i>offset_y</i>).
Equivalent to <CODE>Trafo(1, 0, 0, 1, <i>offset_x</i>,
<i>offset_y</i>)</CODE>.</P>


<DT><B><A NAME="N6"></A><tt>Rotation(<i>angle</i>[, <i>center</i>])</tt></B><DD>

<DT><B><tt>Rotation(<i>angle</i>[, <i>center_x</i>, <i>center_y</i>])</tt></B><DD>
<P>Return a trafo object for a rotation through the angle
<i>angle</i> (counter clockwise in radians) about the center
point <i>center</i> or (<i>center_x</i>, <i>center_y</i>).
<i>center</i> must be a <A HREF="devguide-5.html#N11">PointSpec</A>. If
<i>center</i> is omitted it defaults to the origin.</P>
<P>Equivalent to
<PRE>
	s = sin(angle);
	c = cos(angle);
	offx = cx - c * center_x + s * center_y;
	offy = cy - s * center_x - c * center_y;
	Trafo(c, s, -s, c, offx, offy);
	
</PRE>
</P>

</DL>
</P>


<H3><FONT face="Helvetica,Arial"><A NAME="N7"></A>Attributes</font></H3>

<P>A trafo object has six (read only) attributes:
<DL>
<DT><B><CODE>m11</CODE>, <CODE>m21</CODE>, <CODE>m12</CODE>, <CODE>m22</CODE></B><DD>

<DT><B><CODE>v1</CODE>, <CODE>v2</CODE></B><DD>
<P>The coefficients of the trafo as python floats.</P>
</DL>
</P>


<H3><FONT face="Helvetica,Arial"><A NAME="N8"></A>Methods</font></H3>

<P>
<DL>

<DT><B><tt>DocToWin(<i>point</i>)</tt></B><DD>

<DT><B><tt>DocToWin(<i>x</i>, <i>y</i>)</tt></B><DD>
<P>Transform the point and return the result as a tuple of ints.
<i>point</i> is a <A HREF="devguide-5.html#N11">PointSpec</A>. This function
is used to transform document coordinates to window coordinates,
hence the name. (Normally, if you want to transform a point, you
<A HREF="#N9">call the trafo object directly</A>)</P>


<DT><B><tt>DTransform(<i>point</i>)</tt></B><DD>

<DT><B><tt>DTransform(<i>x</i>, <i>y</i>)</tt></B><DD>
<P>Treat the point as a vector and transform it, that is, don't add
<CODE>v1</CODE> and <CODE>v2</CODE>. <i>point</i> is a <A HREF="devguide-5.html#N11">PointSpec</A>.</P>


<DT><B><tt>inverse()</tt></B><DD>
<P>Return the inverse transformation of self. If self cannot be
inverted raise the <CODE>SingularMatrix</CODE> exception.</P>

<DT><B><tt>coeff()</tt></B><DD>
<P>Return the coefficients of the trafo as a tuple (<i>m11</i>,
<i>m21</i>, <i>m21</i>, <i>m22</i>, <i>v1</i>, <i>v2</i>).</P>

<DT><B><tt>matrix()</tt></B><DD>
<P>Return the matrix coefficients of the trafo (<CODE>m11</CODE>, ...,
<CODE>m22</CODE>) as a tuple. The same as <CODE>coeff()[:4]</CODE>.</P>


<DT><B><tt>offset()</tt></B><DD>
<P>Return the translation coefficients as a point object.</P>
</DL>
</P>

<H3><FONT face="Helvetica,Arial"><A NAME="N9"></A>Operators
</font></H3>

<P>Since a transformation is a mapping in the mathematical sense, it is
convenient to make trafo objects callable, so you can treat them like
functions or methods. Depending on the type of the argument the results
are different:
<DL>

<DT><B><CODE>trafo(<i>point</i>)</CODE></B><DD>

<DT><B><CODE>trafo(<i>x</i>, <i>y</i>)</CODE></B><DD>
<P>Return the transformed point. Point can be any <A HREF="devguide-5.html#N11">PointSpec</A>.</P>

<DT><B><CODE>trafo(<i>othertrafo</i>)</CODE></B><DD>
<P>Return the composition of both trafos.</P>

<DT><B><CODE>trafo(<i>rect</i>)</CODE></B><DD>
<P>Return the smallest <A HREF="devguide-6.html">rect object</A> that
contains the transformed corners of the rect object <i>rect</i>.</P>
<P><CODE>InfinityRect</CODE> and <CODE>EmptyRect</CODE> are returned unchanged.</P>
</DL>
</P>

<H3><FONT face="Helvetica,Arial"><A NAME="N10"></A>Constants</font></H3>

<P>
<DL>
<DT><B><CODE>Identity</CODE></B><DD>
<P>The identity transformation (<CODE>Trafo(1, 0, 0, 1, 0, 0)</CODE>)</P>

<DT><B><CODE>IdentityMatrix</CODE></B><DD>
<P>A tuple containing the coefficients of the identity matrix. The
same as <CODE>Identity.matrix()</CODE></P>

<DT><B><CODE>TrafoType</CODE></B><DD>
<P>The trafo type object.</P>

<DT><B><CODE>SingularMatrix</CODE></B><DD>
<P>Exception raised by the <CODE>inverse()</CODE> method.</P>

</DL>

</P>

<HR NOSHADE>
<TABLE WIDTH="100%">
<TR>
<TD ALIGN="left"><A HREF="devguide-6.html">Rect Objects
</A></TD>
<TD ALIGN="center"><A HREF="devguide-4.html">Coordinate Systems
</A></TD>
<TD ALIGN="right"><A HREF="devguide-8.html">Curve Objects
</A></TD>
</TR>
<TR>
<TH ALIGN="left" WIDTH="33%"><img SRC="Images/arrow-left.png" WIDTH="16" HEIGHT="16" ALIGN="top" ALT="Prev"></TH>
<TH ALIGN="center" WIDTH="33%"><img SRC="Images/arrow-up.png" WIDTH="16" HEIGHT="16" ALIGN="top" ALT="Up"></TH>
<TH ALIGN="right" WIDTH="33%"><img SRC="Images/arrow-right.png" WIDTH="16" HEIGHT="16" ALIGN="top" ALT="Next"></TH>
</TR>
</TABLE>
</body>
</html>