trunc_icosahedron.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import craftmath
  2. import craftdraw
  3. from reportlab.pdfgen import canvas
  4. from reportlab.lib.units import cm
  5. import numpy as np
  6. EDGE_LENGTH = 2 * cm
  7. WITH_MERGIN = True
  8. MARGIN_SIZE = 0.6 * cm
  9. MARGIN_ANGLE = np.pi * 50 / 180
  10. def part1(pdf, v1, v2, pagenum):
  11. vs = craftmath.regular_polygon(5, v1, v2)
  12. vs = np.c_[vs[1:].T, vs[0]].T
  13. craftdraw.polyline(pdf, vs)
  14. mvs = craftmath.margin(MARGIN_ANGLE, MARGIN_SIZE, vs[1], vs[0])
  15. craftdraw.polyline(pdf, mvs)
  16. mvs = craftmath.margin(MARGIN_ANGLE, MARGIN_SIZE, vs[2], vs[1])
  17. craftdraw.polyline(pdf, mvs)
  18. mvs = craftmath.margin(MARGIN_ANGLE, MARGIN_SIZE, vs[4], vs[3])
  19. craftdraw.polyline(pdf, mvs)
  20. vs = craftmath.regular_polygon(6, vs[3], vs[2])
  21. vs = np.c_[vs[1:].T, vs[0]].T
  22. craftdraw.polyline(pdf, vs)
  23. for i in range(3):
  24. if pagenum != 2 or i != 2:
  25. mvs = craftmath.margin(MARGIN_ANGLE, MARGIN_SIZE, vs[1 + i], vs[i])
  26. craftdraw.polyline(pdf, mvs)
  27. vs2 = craftmath.regular_polygon(5, vs[4], vs[3])
  28. vs2 = np.c_[vs2[1:].T, vs2[0]].T
  29. craftdraw.polyline(pdf, vs2)
  30. vs3 = craftmath.regular_polygon(6, vs2[3], vs2[2])
  31. vs3 = np.c_[vs3[1:].T, vs3[0]].T
  32. craftdraw.polyline(pdf, vs3)
  33. for i in range(2):
  34. if pagenum != 1 or i != 0:
  35. mvs = craftmath.margin(
  36. MARGIN_ANGLE, MARGIN_SIZE, vs3[1 + i], vs3[i])
  37. craftdraw.polyline(pdf, mvs)
  38. vs4 = craftmath.regular_polygon(6, vs3[5], vs3[4])
  39. vs4 = np.c_[vs4[1:].T, vs4[0]].T
  40. craftdraw.polyline(pdf, vs4)
  41. mvs = craftmath.margin(MARGIN_ANGLE, MARGIN_SIZE, vs[5], vs[4])
  42. mvs[1, :] = vs4[4, :]
  43. craftdraw.polyline(pdf, mvs)
  44. v = 0.8 * vs4[4] + 0.2 * vs4[5]
  45. pdf.line(vs[4, 0], vs[4, 1], v[0], v[1])
  46. def page(pdf, width, height, pagenum):
  47. v1 = np.array([width / 2 - EDGE_LENGTH / 2, height / 2])
  48. v2 = np.array([width / 2 + EDGE_LENGTH / 2, height / 2])
  49. vs = craftmath.regular_polygon(6, v1, v2)
  50. vs = np.c_[vs.T, vs[0, :]].T
  51. craftdraw.polyline(pdf, vs)
  52. mvs = craftmath.margin(MARGIN_ANGLE, MARGIN_SIZE, vs[2], vs[1])
  53. craftdraw.polyline(pdf, mvs)
  54. mvs = craftmath.margin(MARGIN_ANGLE, MARGIN_SIZE, vs[4], vs[3])
  55. craftdraw.polyline(pdf, mvs)
  56. mvs = craftmath.margin(MARGIN_ANGLE, MARGIN_SIZE, vs[0], vs[5])
  57. craftdraw.polyline(pdf, mvs)
  58. part1(pdf, vs[1], vs[0], pagenum)
  59. part1(pdf, vs[3], vs[2], pagenum)
  60. part1(pdf, vs[5], vs[4], pagenum)
  61. def main():
  62. # Paper size (A4)
  63. width = 21.0 * cm
  64. height = 29.7 * cm
  65. pdf = canvas.Canvas("./trunc_icosahedron.pdf")
  66. pdf.saveState()
  67. pdf.setAuthor("Kimikazu Kato")
  68. pdf.setTitle("Truncated Regular Icosahedron")
  69. page(pdf, width, height, 1)
  70. pdf.showPage()
  71. page(pdf, width, height, 2)
  72. pdf.saveState()
  73. pdf.save()
  74. if __name__ == '__main__':
  75. main()