Petal compiler, specification, and tools

genlogo.d 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. module genlogo;
  2. import std.experimental.all;
  3. void main()
  4. {
  5. foreach (i; 1..20)
  6. {
  7. draw(i);
  8. }
  9. }
  10. void draw(int numPetals)
  11. {
  12. enum pi = 3.1415927;
  13. auto dim = 256;
  14. auto center = 196;
  15. auto o = File("petal%s.svg".format(numPetals), "w");
  16. o.writefln(`<svg width="%s" height="%s">
  17. <defs>
  18. <radialGradient id="pinky">
  19. <stop stop-color="#b3879d" offset="0%%"/>
  20. <stop stop-color="#d8a4be" offset="50%%"/>
  21. <stop stop-color="#e2bace" offset="90%%"/>
  22. </radialGradient>
  23. </defs>
  24. <polygon stroke="#3c2631" fill="url(#pinky)" points="%s,%s`, center * 2, center * 2, center, center);
  25. // We have five petals. Their points are balanced at the five directions: top, top-right,
  26. // low-right, low-left, top-left.
  27. auto radius = (dim / 2) - (dim / 30);
  28. // 90% of the circle is divided between the petals, and there are five petals.
  29. auto innerRadius = dim / 9;
  30. auto midRadius = dim * 0.75;
  31. auto petalCoverage = 0.95;
  32. auto margin = (1 - petalCoverage) / numPetals * 2 * pi;
  33. auto perPetal = petalCoverage / numPetals;
  34. auto petalWidth = 2 * pi / numPetals;
  35. void writePoint(double radians, double radius)
  36. {
  37. auto x = sin(radians) * radius;
  38. auto y = cos(radians) * radius;
  39. o.writefln(" %s,%s", x + center, y + center);
  40. }
  41. foreach (i; 0 .. numPetals)
  42. {
  43. auto d1 = petalWidth * i + margin;
  44. writefln("%s: %s", i, d1);
  45. auto d2 = d1 + petalWidth - 2 * margin;
  46. auto d3 = d1 + (d2 - d1) / 2;
  47. writePoint(d3, innerRadius);
  48. writePoint(d1, radius);
  49. writePoint(d3, midRadius);
  50. writePoint(d2, radius);
  51. writePoint(d3, innerRadius);
  52. }
  53. o.writefln(` %1$s,%1$s" />
  54. <circle fill="#ac7ba8" stroke="#3c2631" cx="%1$s" cy="%1$s" r="%2$s" />`, center, dim / 6);
  55. o.writefln(`<circle fill="#665683" cx="%s" cy="%s" r="%s" />
  56. </svg>`, center, center, dim / 10);
  57. o.flush;
  58. o.close;
  59. }