stud

study spacejunk
Log | Files | Refs | LICENSE

draw.py (3147B) - Raw


      1 #!/usr/bin/env python3
      2 from math import atan, pi
      3 import numpy as np
      4 import matplotlib.pyplot as plt
      5 
      6 from collections import namedtuple
      7 from shapely.geometry import LineString, asPolygon, Point as sPoint, asLineString
      8 from descartes import PolygonPatch
      9 
     10 from measure import *
     11 
     12 # nubraižytos kelio linijos ir jų nubrėžti offset'ai iš dešinės į kairę.
     13 kelias_l = namedtuple('kelias_l', ['line', 'offsets'])
     14 
     15 N, E, S, W = (0,10), (10,0), (0,-10), (-10,0)
     16 point_annotations = {
     17          1: S,  2: E,  3: N,  4: W,
     18          5: N,  6: S,  7: S,  8: N,
     19          9: E, 10: N, 11: S, 12: N,
     20         13: S, 14: W, 15: N, 16: N,
     21         17: N, 18: E, 19: N, 20: S,
     22         21: N, 22: S, 23: E, 24: N,
     23 }
     24 road_annotations = {'A-08':W, 'A-05':N, 'A-03':N, 'G-11':E}
     25 
     26 # implementacija
     27 fig, ax = plt.subplots()
     28 ax.set_aspect('equal')
     29 plt.grid(True)
     30 
     31 # taškų anotacijos
     32 for v in vertices:
     33     ax.annotate(v.point, xy=v.xy, zorder=KAT0, textcoords='offset points',
     34             fontsize='small', xytext=point_annotations[v.point])
     35 
     36 keliai_l = {}
     37 # kelių piešimas
     38 for id, kelias in keliai.items():
     39     # ašis
     40     kelias_line = LineString([Points[i].xy for i in kelias.virsunes])
     41     ax.plot(*kelias_line.xy, linewidth=2, dashes=kelias.dashes, color=kelias.spalva, zorder=kelias.kat)
     42     # offset'ai
     43     offset_multiplier = kelias.plotis/(kelias.juostos[0].plotis + kelias.juostos[-1].plotis)
     44     offset_lines = []
     45     for offset in kelias.juostos:
     46         l = kelias_line.parallel_offset(offset.plotis*offset_multiplier, offset.kryptis, join_style=2)
     47         offset_lines.append(l)
     48         ax.plot(*l.xy, linewidth=.5, dashes=offset.dashes, color=offset.spalva, zorder=kelias.kat)
     49     # kelio poligonas su plotu
     50     kelias_poly = np.vstack((offset_lines[0].coords, offset_lines[-1].coords))
     51     ax.add_patch(PolygonPatch(asPolygon(kelias_poly), fc='white', zorder=kelias.kat, linewidth=0))
     52     keliai_l[id] = kelias_l(line=kelias_line, offsets=offset_lines)
     53 
     54 # kelių anotacijos
     55 for id, kelias in keliai_l.items():
     56     linestart, lineend = np.array(kelias.offsets[-1].coords)[0:2]
     57     delta = lineend - linestart
     58     angle = atan(delta[1]/delta[0])*180/pi
     59     offset = road_annotations[id]
     60     ax.annotate(id, kelias.offsets[-1].coords[0], zorder=KAT0, textcoords='offset points',
     61             fontsize='small', xytext=offset, rotation=angle)
     62 
     63 # septynkampis
     64 prev_dirang = float(K1)*pi/180
     65 step = 5/7*pi
     66 heptagon = [np.array(Points[6].xy)]
     67 for i in range(1, 7):
     68     dxy = np.array([float(D1)*cos(prev_dirang), float(D1)*sin(prev_dirang)])
     69     heptagon.append(heptagon[i-1] + dxy)
     70     prev_dirang += pi - step
     71 ax.add_patch(PolygonPatch(asPolygon(heptagon), linewidth=2, fc='xkcd:white', ec='xkcd:magenta'))
     72 
     73 # septynkampio centras
     74 x0, y0 = Points[6].xy
     75 x = x0 + float(D1)/(2*sin(pi/7))*sin(pi/7-float(K1)*pi/180)
     76 y = y0 + float(D1)/(2*sin(pi/7))*cos(pi/7-float(K1)*pi/180)
     77 center = sPoint(x, y)
     78 
     79 # užlieta erdvė apskritimas
     80 radius = float(D1)/2/sin(pi/7)-float(A1)
     81 angles = np.linspace(0, 2*pi, num=360)
     82 circle_y = y + np.sin(angles) * radius
     83 circle_x = x + np.cos(angles) * radius
     84 ax.plot(circle_x, circle_y)
     85 
     86 if __name__ == '__main__':
     87     plt.show()