## "Fossies" - the Fresh Open Source Software Archive

### Member "sk1-2.0rc4/src/uc2/libgeom/trafo.py" (25 May 2019, 2480 Bytes) of package /linux/misc/sk1-2.0rc4.tar.gz:

As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "trafo.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.0rc3_vs_2.0rc4.

```    1 # -*- coding: utf-8 -*-
2 #
3 #  Copyright (C) 2015-2018 by Igor E. Novikov
4 #
5 #  This program is free software: you can redistribute it and/or modify
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9 #
10 #  This program is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU General Public License for more details.
14 #
15 #  You should have received a copy of the GNU General Public License
16 #  along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
18 import math
19
20 import cwrap
21
22 NORMAL_TRAFO = [1.0, 0.0, 0.0, 1.0, 0.0, 0.0]
23
24
25 def trafo_rotate(angle, cx=0.0, cy=0.0):
26     m21 = math.sin(angle)
27     m11 = m22 = math.cos(angle)
28     m12 = -m21
29     dx = cx - m11 * cx + m21 * cy
30     dy = cy - m21 * cx - m11 * cy
31     return [m11, m21, m12, m22, dx, dy]
32
33
35     angle = math.pi * grad / 180.0
36     return trafo_rotate(angle, cx, cy)
37
38
39 def _apply_trafo_to_point(point, trafo):
40     x0, y0 = point
41     m11, m21, m12, m22, dx, dy = trafo
42     x1 = m11 * x0 + m12 * y0 + dx
43     y1 = m21 * x0 + m22 * y0 + dy
44     return [x1, y1]
45
46
47 def apply_trafo_to_point(point, trafo):
48     if len(point) == 2:
49         return _apply_trafo_to_point(point, trafo)
50     else:
51         return [_apply_trafo_to_point(point[0], trafo),
52                 _apply_trafo_to_point(point[1], trafo),
53                 _apply_trafo_to_point(point[2], trafo), point[3]]
54
55
56 def apply_trafo_to_points(points, trafo):
57     return [apply_trafo_to_point(point, trafo) for point in points]
58
59
60 def apply_trafo_to_path(path, trafo):
61     return [apply_trafo_to_point(path[0], trafo),
62             [apply_trafo_to_point(point, trafo) for point in path[1]],
63             path[2]]
64
65
66 def apply_trafo_to_paths(paths, trafo):
67     return [apply_trafo_to_path(path, trafo) for path in paths]
68
69
70 def apply_trafo_to_bbox(bbox, trafo):
71     p0, p1 = apply_trafo_to_points([bbox[:2], bbox[2:]], trafo)
72     return p0 + p1
73
74
75 def get_transformed_paths(obj):
76     if obj.is_curve:
77         return apply_trafo_to_paths(obj.paths, obj.trafo)
78     elif obj.is_text:
79         return obj.get_transformed_paths()
80     elif obj.cache_paths:
81         return apply_trafo_to_paths(obj.cache_paths, obj.trafo)
82     else:
83         return cwrap.get_transformed_path(obj)
```