Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Aspect Ratio Try 0
- Chris M. Thomasson
- _________________________________________________________*/
- /* Our global 2d plane
- _________________________________________________________*/
- float g_radius = 3; // master zoom
- ct_axes_2d g_axes = new ct_axes_2d(0, 0, g_radius);
- ct_plane_2d g_plane = null;
- // Runtime setup, pre-main...
- void setup()
- {
- // setup our global plane
- size(960, 540);
- g_plane = new ct_plane_2d(g_axes, width, height);
- // Initial state
- noFill();
- noLoop();
- background(0);
- colorMode(RGB, 255);
- strokeWeight(2);
- stroke(255, 255, 255);
- // Call main!
- ct_main();
- }
- /* Main
- _________________________________________________________*/
- void ct_main()
- {
- // white lines and circle
- stroke(255, 255, 255);
- ct_circle(0, 0, 1);
- ct_rect_center(0, 0, 1, 1);
- ct_line(-1, 1, -2, 2);
- ct_line(1, 1, 2, 2);
- ct_line(1, -1, 2, -2);
- ct_line(-1, -1, -2, -2);
- // purple circles and lines
- stroke(255, 255, 0);
- ct_circle(0, 0, 2);
- ct_rect_center(0, 0, 2, 2);
- stroke(255, 0, 255);
- ct_circle(-1.5, 0, .5);
- ct_circle(1.5, 0, .5);
- ct_circle(0, -1.5, .5);
- ct_circle(0, 1.5, .5);
- // axes green as x and red as y.
- float oradius = (sqrt(8) - 2) / 2;
- stroke(0, 255, 0);
- ct_line(-oradius * 2 - 2, 0, oradius * 2 + 2, 0);
- stroke(255, 0, 0);
- ct_line(0, -oradius * 2 - 2, 0, oradius * 2 + 2);
- // Cyan circles
- stroke(0, 255, 255);
- ct_circle(0, 0, sqrt(8));
- ct_circle(-2 - oradius, 0, oradius);
- ct_circle(2 + oradius, 0, oradius);
- ct_circle(0, -2 - oradius, oradius);
- ct_circle(0, 2 + oradius, oradius);
- saveFrame("ct_vector_field.jpg");
- }
- /* 2d Projected Drawing Tools
- _________________________________________________________*/
- void ct_circle(float x, float y, float r)
- {
- float x_proj = g_plane.project_x(x);
- float y_proj = g_plane.project_y(y);
- float r_proj = g_plane.project_h(r) * 2;
- ellipse(x_proj, y_proj, r_proj, r_proj);
- }
- void ct_rect_center(float x, float y, float w, float h)
- {
- ct_rect(x - w, y + h, w, h);
- }
- void ct_rect(float x, float y, float w, float h)
- {
- float x_proj = g_plane.project_x(x);
- float y_proj = g_plane.project_y(y);
- float w_proj = g_plane.project_w(w) * 2;
- float h_proj = g_plane.project_h(h) * 2;
- rect(x_proj, y_proj, w_proj, h_proj);
- }
- void ct_line(float x0, float y0, float x1, float y1)
- {
- float x0_proj = g_plane.project_x(x0);
- float y0_proj = g_plane.project_y(y0);
- float x1_proj = g_plane.project_x(x1);
- float y1_proj = g_plane.project_y(y1);
- line(x0_proj, y0_proj, x1_proj, y1_proj);
- }
- void ct_point(float x, float y)
- {
- float x_proj = g_plane.project_x(x);
- float y_proj = g_plane.project_y(y);
- point(x_proj, y_proj);
- }
- /* 2d Axes
- _________________________________________________________*/
- class ct_axes_2d
- {
- float m_xmin;
- float m_xmax;
- float m_ymin;
- float m_ymax;
- ct_axes_2d(float xmin, float xmax, float ymin, float ymax)
- {
- m_xmin = xmin;
- m_xmax = xmax;
- m_ymin = ymin;
- m_ymax = ymax;
- }
- ct_axes_2d(ct_axes_2d axes)
- {
- m_xmin = axes.m_xmin;
- m_xmax = axes.m_xmax;
- m_ymin = axes.m_ymin;
- m_ymax = axes.m_ymax;
- }
- ct_axes_2d(float x, float y, float r)
- {
- m_xmin = x - r;
- m_xmax = x + r;
- m_ymin = y - r;
- m_ymax = y + r;
- }
- ct_axes_2d copy_to(ct_axes_2d dest)
- {
- dest.m_xmin = m_xmin;
- dest.m_xmax = m_xmax;
- dest.m_ymin = m_ymin;
- dest.m_ymax = m_ymax;
- return dest;
- }
- float width() { return m_xmax - m_xmin; }
- float height() { return m_ymax - m_ymin; }
- };
- /* 2d Plane
- _________________________________________________________*/
- class ct_plane_2d
- {
- ct_axes_2d m_axes;
- int m_width;
- int m_height;
- float m_aratio;
- float m_xstep;
- float m_ystep;
- ct_plane_2d(ct_axes_2d axes, int width_, int height_)
- {
- m_axes = new ct_axes_2d(axes); // copy
- m_width = width_;
- m_height = height_;
- float daspect = abs((float)height_ / width_);
- float waspect = abs(m_axes.height() / m_axes.width());
- if (daspect > waspect)
- {
- float excess = m_axes.height() * (daspect / waspect - 1);
- m_axes.m_ymax += excess / 2;
- m_axes.m_ymin -= excess / 2;
- }
- else if (daspect < waspect)
- {
- float excess = m_axes.width() * (waspect / daspect - 1);
- m_axes.m_xmax += excess / 2;
- m_axes.m_xmin -= excess / 2;
- }
- m_xstep = m_axes.width() / ((m_width > 1) ? (m_width - 1) : m_width);
- m_ystep = m_axes.height() / ((m_height > 1) ? (m_height - 1) : m_height);
- }
- float project_x(float x)
- {
- return floor((x - m_axes.m_xmin) / m_xstep);
- }
- float project_y(float y)
- {
- return floor((m_axes.m_ymax - y) / m_ystep);
- }
- float project_w(float w)
- {
- return round(w / m_xstep);
- }
- float project_h(float h)
- {
- return round(h / m_ystep);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement