Advertisement
Chris_M_Thomasson

Aspect Ratio Try 0...

Aug 15th, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. /* Aspect Ratio Try 0
  2. Chris M. Thomasson
  3. _________________________________________________________*/
  4.  
  5.  
  6.  
  7. /* Our global 2d plane
  8. _________________________________________________________*/
  9. float g_radius = 3; // master zoom
  10. ct_axes_2d g_axes = new ct_axes_2d(0, 0, g_radius);
  11. ct_plane_2d g_plane = null;
  12.  
  13.  
  14. // Runtime setup, pre-main...
  15. void setup()
  16. {
  17. // setup our global plane
  18. size(960, 540);
  19. g_plane = new ct_plane_2d(g_axes, width, height);
  20.  
  21. // Initial state
  22. noFill();
  23. noLoop();
  24. background(0);
  25. colorMode(RGB, 255);
  26. strokeWeight(2);
  27. stroke(255, 255, 255);
  28.  
  29. // Call main!
  30. ct_main();
  31. }
  32.  
  33.  
  34. /* Main
  35. _________________________________________________________*/
  36. void ct_main()
  37. {
  38. // white lines and circle
  39. stroke(255, 255, 255);
  40. ct_circle(0, 0, 1);
  41. ct_rect_center(0, 0, 1, 1);
  42. ct_line(-1, 1, -2, 2);
  43. ct_line(1, 1, 2, 2);
  44. ct_line(1, -1, 2, -2);
  45. ct_line(-1, -1, -2, -2);
  46.  
  47. // purple circles and lines
  48. stroke(255, 255, 0);
  49. ct_circle(0, 0, 2);
  50. ct_rect_center(0, 0, 2, 2);
  51. stroke(255, 0, 255);
  52. ct_circle(-1.5, 0, .5);
  53. ct_circle(1.5, 0, .5);
  54. ct_circle(0, -1.5, .5);
  55. ct_circle(0, 1.5, .5);
  56.  
  57. // axes green as x and red as y.
  58. float oradius = (sqrt(8) - 2) / 2;
  59. stroke(0, 255, 0);
  60. ct_line(-oradius * 2 - 2, 0, oradius * 2 + 2, 0);
  61. stroke(255, 0, 0);
  62. ct_line(0, -oradius * 2 - 2, 0, oradius * 2 + 2);
  63.  
  64. // Cyan circles
  65. stroke(0, 255, 255);
  66. ct_circle(0, 0, sqrt(8));
  67. ct_circle(-2 - oradius, 0, oradius);
  68. ct_circle(2 + oradius, 0, oradius);
  69. ct_circle(0, -2 - oradius, oradius);
  70. ct_circle(0, 2 + oradius, oradius);
  71.  
  72. saveFrame("ct_vector_field.jpg");
  73. }
  74.  
  75.  
  76. /* 2d Projected Drawing Tools
  77. _________________________________________________________*/
  78. void ct_circle(float x, float y, float r)
  79. {
  80. float x_proj = g_plane.project_x(x);
  81. float y_proj = g_plane.project_y(y);
  82. float r_proj = g_plane.project_h(r) * 2;
  83.  
  84. ellipse(x_proj, y_proj, r_proj, r_proj);
  85. }
  86.  
  87.  
  88. void ct_rect_center(float x, float y, float w, float h)
  89. {
  90. ct_rect(x - w, y + h, w, h);
  91. }
  92.  
  93.  
  94. void ct_rect(float x, float y, float w, float h)
  95. {
  96. float x_proj = g_plane.project_x(x);
  97. float y_proj = g_plane.project_y(y);
  98. float w_proj = g_plane.project_w(w) * 2;
  99. float h_proj = g_plane.project_h(h) * 2;
  100.  
  101. rect(x_proj, y_proj, w_proj, h_proj);
  102. }
  103.  
  104.  
  105. void ct_line(float x0, float y0, float x1, float y1)
  106. {
  107. float x0_proj = g_plane.project_x(x0);
  108. float y0_proj = g_plane.project_y(y0);
  109. float x1_proj = g_plane.project_x(x1);
  110. float y1_proj = g_plane.project_y(y1);
  111.  
  112. line(x0_proj, y0_proj, x1_proj, y1_proj);
  113. }
  114.  
  115. void ct_point(float x, float y)
  116. {
  117. float x_proj = g_plane.project_x(x);
  118. float y_proj = g_plane.project_y(y);
  119.  
  120. point(x_proj, y_proj);
  121. }
  122.  
  123.  
  124. /* 2d Axes
  125. _________________________________________________________*/
  126. class ct_axes_2d
  127. {
  128. float m_xmin;
  129. float m_xmax;
  130. float m_ymin;
  131. float m_ymax;
  132.  
  133. ct_axes_2d(float xmin, float xmax, float ymin, float ymax)
  134. {
  135. m_xmin = xmin;
  136. m_xmax = xmax;
  137. m_ymin = ymin;
  138. m_ymax = ymax;
  139. }
  140.  
  141. ct_axes_2d(ct_axes_2d axes)
  142. {
  143. m_xmin = axes.m_xmin;
  144. m_xmax = axes.m_xmax;
  145. m_ymin = axes.m_ymin;
  146. m_ymax = axes.m_ymax;
  147. }
  148.  
  149. ct_axes_2d(float x, float y, float r)
  150. {
  151. m_xmin = x - r;
  152. m_xmax = x + r;
  153. m_ymin = y - r;
  154. m_ymax = y + r;
  155. }
  156.  
  157. ct_axes_2d copy_to(ct_axes_2d dest)
  158. {
  159. dest.m_xmin = m_xmin;
  160. dest.m_xmax = m_xmax;
  161. dest.m_ymin = m_ymin;
  162. dest.m_ymax = m_ymax;
  163.  
  164. return dest;
  165. }
  166.  
  167. float width() { return m_xmax - m_xmin; }
  168. float height() { return m_ymax - m_ymin; }
  169. };
  170.  
  171.  
  172. /* 2d Plane
  173. _________________________________________________________*/
  174. class ct_plane_2d
  175. {
  176. ct_axes_2d m_axes;
  177. int m_width;
  178. int m_height;
  179. float m_aratio;
  180. float m_xstep;
  181. float m_ystep;
  182.  
  183. ct_plane_2d(ct_axes_2d axes, int width_, int height_)
  184. {
  185. m_axes = new ct_axes_2d(axes); // copy
  186. m_width = width_;
  187. m_height = height_;
  188.  
  189. float daspect = abs((float)height_ / width_);
  190. float waspect = abs(m_axes.height() / m_axes.width());
  191.  
  192. if (daspect > waspect)
  193. {
  194. float excess = m_axes.height() * (daspect / waspect - 1);
  195. m_axes.m_ymax += excess / 2;
  196. m_axes.m_ymin -= excess / 2;
  197. }
  198.  
  199. else if (daspect < waspect)
  200. {
  201. float excess = m_axes.width() * (waspect / daspect - 1);
  202. m_axes.m_xmax += excess / 2;
  203. m_axes.m_xmin -= excess / 2;
  204. }
  205.  
  206. m_xstep = m_axes.width() / ((m_width > 1) ? (m_width - 1) : m_width);
  207. m_ystep = m_axes.height() / ((m_height > 1) ? (m_height - 1) : m_height);
  208. }
  209.  
  210. float project_x(float x)
  211. {
  212. return floor((x - m_axes.m_xmin) / m_xstep);
  213. }
  214.  
  215. float project_y(float y)
  216. {
  217. return floor((m_axes.m_ymax - y) / m_ystep);
  218. }
  219.  
  220. float project_w(float w)
  221. {
  222. return round(w / m_xstep);
  223. }
  224.  
  225. float project_h(float h)
  226. {
  227. return round(h / m_ystep);
  228. }
  229. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement