trunc_dodecahedron.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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_SIZE1 = 0.5 * cm
  9. MARGIN_SIZE2 = 0.8 * cm
  10. MARGIN_SIZE3 = 0.8 * cm
  11. MARGIN_ANGLE1 = np.pi * 40 / 180
  12. MARGIN_ANGLE2 = np.pi * 60 / 180
  13. MARGIN_ANGLE3 = np.pi * 50 / 180
  14. def part1(pdf, v1, v2, v3, partnum, pagenum):
  15. vs = craftmath.regular_polygon(3, v1, v2)
  16. vs = np.concatenate((vs, vs[0].reshape(1, 2)))
  17. craftdraw.polyline(pdf, vs)
  18. mvs = craftmath.margin(MARGIN_ANGLE1, MARGIN_SIZE1, vs[0], vs[2])
  19. craftdraw.polyline(pdf, mvs)
  20. vs2 = craftmath.regular_polygon(10, vs[2], vs[1])
  21. vs2 = np.concatenate((vs2, vs2[0].reshape(1, 2)))
  22. craftdraw.polyline(pdf, vs2)
  23. pdf.line(vs2[2, 0], vs2[2, 1], v3[0], v3[1])
  24. vs3 = craftmath.regular_polygon(3, vs2[5], vs2[4])
  25. vs3 = np.concatenate((vs3, vs3[0].reshape(1, 2)))
  26. craftdraw.polyline(pdf, vs3)
  27. if partnum % 2 == 0:
  28. mvs = craftmath.margin(MARGIN_ANGLE2, MARGIN_SIZE2, vs2[4], vs2[3])
  29. craftdraw.polyline(pdf, mvs)
  30. mvs = craftmath.margin(MARGIN_ANGLE2, MARGIN_SIZE2, vs3[2], vs3[1])
  31. craftdraw.polyline(pdf, mvs)
  32. if partnum in [0, 2]:
  33. mvs = craftmath.margin(MARGIN_ANGLE2, MARGIN_SIZE2, vs2[10], vs2[9])
  34. craftdraw.polyline(pdf, mvs)
  35. mvs = craftmath.margin(MARGIN_ANGLE3, MARGIN_SIZE3, vs2[9], vs2[8])
  36. craftdraw.polyline(pdf, mvs)
  37. if pagenum == 1:
  38. for i in range(3):
  39. if i == 1:
  40. ang = MARGIN_ANGLE3
  41. size = MARGIN_SIZE3
  42. else:
  43. ang = MARGIN_ANGLE2
  44. size = MARGIN_SIZE2
  45. mvs = craftmath.margin(ang, size, vs2[6 + i], vs2[5 + i])
  46. craftdraw.polyline(pdf, mvs)
  47. mvs = craftmath.margin(MARGIN_ANGLE2, MARGIN_SIZE2, vs3[0], vs3[2])
  48. craftdraw.polyline(pdf, mvs)
  49. def page(pdf, width, height, pagenum):
  50. v1 = np.array([width / 2 - EDGE_LENGTH / 2, height / 2])
  51. v2 = np.array([width / 2 + EDGE_LENGTH / 2, height / 2])
  52. vs = craftmath.regular_polygon(10, v1, v2)
  53. vs = np.concatenate((vs, vs[0].reshape(1, 2)))
  54. craftdraw.polyline(pdf, vs)
  55. for i in range(5):
  56. part1(pdf, vs[i * 2 + 1], vs[i * 2], vs[(i * 2 - 1) % 10], i, pagenum)
  57. def main():
  58. # Paper size (A4)
  59. width = 21.0 * cm
  60. height = 29.7 * cm
  61. pdf = canvas.Canvas("./trunc_dodecahedron.pdf")
  62. pdf.saveState()
  63. pdf.setAuthor("Kimikazu Kato")
  64. pdf.setTitle("Truncated Regular Icosahedron")
  65. page(pdf, width, height, 1)
  66. pdf.showPage()
  67. page(pdf, width, height, 2)
  68. pdf.saveState()
  69. pdf.save()
  70. if __name__ == '__main__':
  71. main()