Sat, 23 Sep 2017 10:27:49 +0200
Added more default options
Code cleanup
16 | 1 | #!/usr/bin/env python |
2 | """ | |
3 | ffgeom.py | |
4 | Copyright (C) 2005 Aaron Cyril Spike, aaron@ekips.org | |
5 | ||
6 | This file is part of FretFind 2-D. | |
7 | ||
8 | FretFind 2-D is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | FretFind 2-D is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with FretFind 2-D; if not, write to the Free Software | |
20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | """ | |
22 | import math | |
23 | try: | |
24 | NaN = float('NaN') | |
25 | except ValueError: | |
26 | PosInf = 1e300000 | |
27 | NaN = PosInf/PosInf | |
28 | ||
29 | class Point: | |
30 | precision = 5 | |
31 | def __init__(self, x, y): | |
32 | self.__coordinates = {'x' : float(x), 'y' : float(y)} | |
33 | def __getitem__(self, key): | |
34 | return self.__coordinates[key] | |
35 | def __setitem__(self, key, value): | |
36 | self.__coordinates[key] = float(value) | |
37 | def __repr__(self): | |
38 | return '(%s, %s)' % (round(self['x'],self.precision),round(self['y'],self.precision)) | |
39 | def copy(self): | |
40 | return Point(self['x'],self['y']) | |
41 | def translate(self, x, y): | |
42 | self['x'] += x | |
43 | self['y'] += y | |
44 | def move(self, x, y): | |
45 | self['x'] = float(x) | |
46 | self['y'] = float(y) | |
47 | ||
48 | class Segment: | |
49 | def __init__(self, e0, e1): | |
50 | self.__endpoints = [e0, e1] | |
51 | def __getitem__(self, key): | |
52 | return self.__endpoints[key] | |
53 | def __setitem__(self, key, value): | |
54 | self.__endpoints[key] = value | |
55 | def __repr__(self): | |
56 | return repr(self.__endpoints) | |
57 | def copy(self): | |
58 | return Segment(self[0],self[1]) | |
59 | def translate(self, x, y): | |
60 | self[0].translate(x,y) | |
61 | self[1].translate(x,y) | |
62 | def move(self,e0,e1): | |
63 | self[0] = e0 | |
64 | self[1] = e1 | |
65 | def delta_x(self): | |
66 | return self[1]['x'] - self[0]['x'] | |
67 | def delta_y(self): | |
68 | return self[1]['y'] - self[0]['y'] | |
69 | #alias functions | |
70 | run = delta_x | |
71 | rise = delta_y | |
72 | def slope(self): | |
73 | if self.delta_x() != 0: | |
74 | return self.delta_x() / self.delta_y() | |
75 | return NaN | |
76 | def intercept(self): | |
77 | if self.delta_x() != 0: | |
78 | return self[1]['y'] - (self[0]['x'] * self.slope()) | |
79 | return NaN | |
80 | def distanceToPoint(self, p): | |
81 | s2 = Segment(self[0],p) | |
82 | c1 = dot(s2,self) | |
83 | if c1 <= 0: | |
84 | return Segment(p,self[0]).length() | |
85 | c2 = dot(self,self) | |
86 | if c2 <= c1: | |
87 | return Segment(p,self[1]).length() | |
88 | return self.perpDistanceToPoint(p) | |
89 | def perpDistanceToPoint(self, p): | |
90 | len = self.length() | |
91 | if len == 0: return NaN | |
92 | return math.fabs(((self[1]['x'] - self[0]['x']) * (self[0]['y'] - p['y'])) - \ | |
93 | ((self[0]['x'] - p['x']) * (self[1]['y'] - self[0]['y']))) / len | |
94 | def angle(self): | |
95 | return math.pi * (math.atan2(self.delta_y(), self.delta_x())) / 180 | |
96 | def length(self): | |
97 | return math.sqrt((self.delta_x() ** 2) + (self.delta_y() ** 2)) | |
98 | def pointAtLength(self, len): | |
99 | if self.length() == 0: return Point(NaN, NaN) | |
100 | ratio = len / self.length() | |
101 | x = self[0]['x'] + (ratio * self.delta_x()) | |
102 | y = self[0]['y'] + (ratio * self.delta_y()) | |
103 | return Point(x, y) | |
104 | def pointAtRatio(self, ratio): | |
105 | if self.length() == 0: return Point(NaN, NaN) | |
106 | x = self[0]['x'] + (ratio * self.delta_x()) | |
107 | y = self[0]['y'] + (ratio * self.delta_y()) | |
108 | return Point(x, y) | |
109 | def createParallel(self, p): | |
110 | return Segment(Point(p['x'] + self.delta_x(), p['y'] + self.delta_y()), p) | |
111 | def intersect(self, s): | |
112 | return intersectSegments(self, s) | |
113 | ||
114 | def intersectSegments(s1, s2): | |
115 | x1 = s1[0]['x'] | |
116 | x2 = s1[1]['x'] | |
117 | x3 = s2[0]['x'] | |
118 | x4 = s2[1]['x'] | |
119 | ||
120 | y1 = s1[0]['y'] | |
121 | y2 = s1[1]['y'] | |
122 | y3 = s2[0]['y'] | |
123 | y4 = s2[1]['y'] | |
124 | ||
125 | denom = ((y4 - y3) * (x2 - x1)) - ((x4 - x3) * (y2 - y1)) | |
126 | num1 = ((x4 - x3) * (y1 - y3)) - ((y4 - y3) * (x1 - x3)) | |
127 | num2 = ((x2 - x1) * (y1 - y3)) - ((y2 - y1) * (x1 - x3)) | |
128 | ||
129 | num = num1 | |
130 | ||
131 | if denom != 0: | |
132 | x = x1 + ((num / denom) * (x2 - x1)) | |
133 | y = y1 + ((num / denom) * (y2 - y1)) | |
134 | return Point(x, y) | |
135 | return Point(NaN, NaN) | |
136 | ||
137 | def dot(s1, s2): | |
138 | return s1.delta_x() * s2.delta_x() + s1.delta_y() * s2.delta_y() | |
139 | ||
140 | ||
141 | # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99 |