Applied upstream as r3028 r3025 r3024
[PyX/mjg.git] / test / experimental / quadrilateral.py
blobe74ad4f103b0c4297158ed8a671a8a76dfd9a64f
1 # this example is adapted from a MetaPost example by Hans Hagen
3 import sys; sys.path.insert(0, "../..")
4 from pyx import *
5 from solve import scalar, vector, solver
7 # define the inner quadrilateral
8 p = vector([0, 0])
9 q = vector([2, 5])
10 r = vector([8, 6])
11 s = vector([5, -1])
13 # some points of the outer squares are equal to points of the inner quadrilateral
14 z11 = p
15 z12 = q
16 z21 = q
17 z22 = r
18 z31 = r
19 z32 = s
20 z41 = s
21 z42 = p
23 # complete the definition of the outer squares
24 z1diff = z12 - z11
25 z2diff = z22 - z21
26 z3diff = z32 - z31
27 z4diff = z42 - z41
29 z13 = vector([z12.x-z1diff.y, z12.y+z1diff.x])
30 z14 = vector([z11.x-z1diff.y, z11.y+z1diff.x])
31 z23 = vector([z22.x-z2diff.y, z22.y+z2diff.x])
32 z24 = vector([z21.x-z2diff.y, z21.y+z2diff.x])
33 z33 = vector([z32.x-z3diff.y, z32.y+z3diff.x])
34 z34 = vector([z31.x-z3diff.y, z31.y+z3diff.x])
35 z43 = vector([z42.x-z4diff.y, z42.y+z4diff.x])
36 z44 = vector([z41.x-z4diff.y, z41.y+z4diff.x])
38 # define the centers of the outer squares
39 z1 = 0.5*(z11 + z13)
40 z2 = 0.5*(z21 + z23)
41 z3 = 0.5*(z31 + z33)
42 z4 = 0.5*(z41 + z43)
44 # define the crossing point by some equations
45 z0 = vector(2)
46 solver.eq(z0, z1 + scalar()*(z3-z1))
47 solver.eq(z0, z2 + scalar()*(z4-z2))
49 # finally draw the result
51 def line(p1, p2):
52 return path.line(float(p1.x), float(p1.y), float(p2.x), float(p2.y))
54 def square(p1, p2, p3, p4):
55 return path.path(path.moveto(float(p1.x), float(p1.y)),
56 path.lineto(float(p2.x), float(p2.y)),
57 path.lineto(float(p3.x), float(p3.y)),
58 path.lineto(float(p4.x), float(p4.y)),
59 path.closepath())
61 c = canvas.canvas()
63 c.stroke(square(z11, z12, z13, z14))
64 c.stroke(square(z21, z22, z23, z24))
65 c.stroke(square(z31, z32, z33, z34))
66 c.stroke(square(z41, z42, z43, z44))
68 c.stroke(line(z11, z13), [style.linestyle.dashed, style.linewidth.Thin])
69 c.stroke(line(z12, z14), [style.linestyle.dashed, style.linewidth.Thin])
70 c.stroke(line(z21, z23), [style.linestyle.dashed, style.linewidth.Thin])
71 c.stroke(line(z22, z24), [style.linestyle.dashed, style.linewidth.Thin])
72 c.stroke(line(z31, z33), [style.linestyle.dashed, style.linewidth.Thin])
73 c.stroke(line(z32, z34), [style.linestyle.dashed, style.linewidth.Thin])
74 c.stroke(line(z41, z43), [style.linestyle.dashed, style.linewidth.Thin])
75 c.stroke(line(z42, z44), [style.linestyle.dashed, style.linewidth.Thin])
77 c.stroke(square(p, q, r, s), [color.rgb.red, style.linewidth.THick])
79 c.stroke(line(z1, z3), [color.rgb.green])
80 c.stroke(line(z2, z4), [color.rgb.green])
82 c.fill(path.circle(float(z0.x), float(z0.y), 0.1), [color.rgb.blue])
84 c.writeEPSfile("quadrilateral")