Attachment 'genimg.py'
Download 1 import math
2 from scipy import gplt
3
4 def dot(a,b):
5 l = len(a)
6 lb = len(b)
7 assert(l == lb)
8 r = 0.0
9 for i in range(l):
10 r += a[i]*b[i]
11 return r
12
13 def cross(a,b):
14 la = len(a)
15 lb = len(b)
16 assert(la == 3 and lb == 3)
17 r1 = a[1]*b[2] - a[2]*b[1]
18 r2 = a[0]*b[2] - a[2]*b[0]
19 r3 = a[0]*b[1] - a[1]*b[0]
20 return [r1, -r2, r3]
21
22 def p(lb, le, t):
23 l = len(lb)
24 r = list()
25 for i in range(l):
26 r.append(lb[i]*(1-t) + le[i]*t)
27 return r
28
29 def genxy(l , r, ep=0.005):
30 lb = l[0]
31 le = l[1]
32 t = 0.0
33 rx = list()
34 ry = list()
35 while t<=1.0:
36 tp = p(lb, le, t)
37 x = dot(tp,r)
38 s = cross(tp,r)
39 y = d(s)
40 rx.append(x)
41 ry.append(y)
42 t += ep
43 return rx,ry
44
45 def d(a):
46 s = 0.0
47 for x in a:
48 s +=x**2
49 return math.sqrt(s)
50
51 def unit(a):
52 dd = d(a)
53 r = list()
54 for x in a:
55 r.append(x/dd)
56 return r
57
58 def elementp(a,b):
59 l = len(a)
60 r = list()
61 for i in range(l):
62 r.append(a[i]*b[i])
63 return r
64 def genlines(a, b, c):
65 lines = list()
66 trans = [[1, 1, 1],
67 [-1, -1, 1],
68 [1, -1, -1],
69 [-1, 1, -1]
70 ]
71 for t in trans:
72 [ta, tb, tc] = elementp([a,b,c],t)
73 tlines = [
74 [[ ta, tb, tc], [-ta, tb, tc]],
75 [[ ta, tb, tc], [ ta,-tb, tc]],
76 [[ ta, tb, tc], [ ta, tb,-tc]]
77 ]
78 lines = lines + tlines
79 return lines
80
81 import sys
82 def plotl(a, b, c, ep=0.005):
83 r = unit([a,b,c])
84 lines = genlines(r[0],r[1],r[2])
85 lis = [(0,0),(0,1)]
86 for l in lines:
87 rx, ry = genxy(l, r, ep)
88 lis.append(rx)
89 lis.append(ry)
90 apply(gplt.plot,lis)
91 gplt.title("l(t)-- a=%f, b=%f, c=%f"%(r[0],r[1],r[2]))
92 gplt.xaxis([-1.0,1.0])
93 gplt.yaxis([0,1])
94 sys.stderr.write("press RETURN to continue:")
95 raw_input()
96
97 def main():
98 plotl(1, 1, 1)
99 plotl(1, 0.5, 0.5)
100 plotl(1, 1.5, 1.5)
101 plotl(1, 2, 3)
102 plotl(1, 1.5, 2)
103 if __name__ == "__main__":
104 main()
105
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.