Chris_M_Thomasson

Field Points on a Line using von Koch

Sep 29th, 2021
1,320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. void line(
  2.     ct::plot::cairo::plot_2d& plot,
  3.     ct::vector_spice::field_vector& field,
  4.     glm::vec2 p0,
  5.     glm::vec2 p1,
  6.     ct::plot::cairo::pixel color,
  7.     unsigned long n
  8. ) {
  9.     glm::vec2 dif = p1 - p0;
  10.  
  11.     float normal_base = 1.f / (n - 1);
  12.  
  13.     //plot.line(p0, p1, color);
  14.  
  15.     for (unsigned long i = 0; i < n; ++i)
  16.     {
  17.         float normal = normal_base * i;
  18.  
  19.         glm::vec2 pn0 = p0 + dif * normal;
  20.  
  21.         //plot.circle_filled(pn0, .05, color);
  22.  
  23.         field.push_back({ { pn0, 0 }, -1 });
  24.     }
  25. }
  26.  
  27.  
  28. void koch(
  29.     ct::plot::cairo::plot_2d& plot,
  30.     ct::vector_spice::field_vector& field,
  31.     unsigned long ri,
  32.     unsigned long rn,
  33.     glm::vec2 p0,
  34.     glm::vec2 p1,
  35.     unsigned long n
  36. ) {
  37.     if (ri >= rn) return;
  38.  
  39.  
  40.     glm::vec2 dif = p1 - p0;
  41.     glm::vec2 dif_perp = { -dif.y, dif.x };
  42.     glm::vec2 dif_mid = p0 + dif * 1.f / 2.f;
  43.  
  44.     float scale = 1.f / 3.f;
  45.  
  46.     glm::vec2 k0 = p0;
  47.     glm::vec2 k1 = k0 + dif * scale;
  48.     glm::vec2 k2 = dif_mid + dif_perp * scale;
  49.     glm::vec2 k3 = p1 - dif * scale;
  50.     glm::vec2 k4 = p1;
  51.  
  52.  
  53.     /*
  54.     plot.line(k0, k1, CT_RGBF(1, 0, 0));
  55.     plot.line(k1, k2, CT_RGBF(0, 1, 0));
  56.     plot.line(k2, k3, CT_RGBF(0, 0, 1));
  57.     plot.line(k3, k4, CT_RGBF(0, 1, 1));
  58.     */
  59.  
  60.     unsigned long line_n = 5;
  61.  
  62.     line(plot, field, k0, k1, CT_RGBF(1, 0, 0), line_n);
  63.     line(plot, field, k1, k2, CT_RGBF(0, 1, 0), line_n);
  64.     line(plot, field, k2, k3, CT_RGBF(0, 0, 1), line_n);
  65.     line(plot, field, k3, k4, CT_RGBF(0, 1, 1), line_n);
  66.  
  67.  
  68.     koch(plot, field, ri + 1, rn, k0, k1, n);
  69.     koch(plot, field, ri + 1, rn, k1, k2, n);
  70.     koch(plot, field, ri + 1, rn, k2, k3, n);
  71.     koch(plot, field, ri + 1, rn, k3, k4, n);
  72.  
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment