Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if ! defined (CT_G2D_HPP)
- #define CT_G2D_HPP 1
- #define CT_PI 3.1415926535897932384626433832795
- #define CT_PI2 6.283185307179586476925286766559
- #include <complex>
- // graphics base
- #include "EasyBMP.h"
- #include "EasyBMP_Geometry.h"
- // common
- typedef double ct_float;
- typedef std::complex<ct_float> ct_complex;
- #define CT_RGBA(mp_r, mp_g, mp_b, mp_a) \
- RGBApixel { ((unsigned char)(mp_b)), \
- ((unsigned char)(mp_g)), \
- ((unsigned char)(mp_r)), \
- ((unsigned char)(mp_a)) }
- #define CT_RGB(mp_r, mp_g, mp_b) \
- CT_RGBA(mp_r, mp_g, mp_b, 0)
- #define CT_RGBAF(mp_r, mp_g, mp_b, mp_a) \
- CT_RGBA(((mp_r) * 255), ((mp_g) * 255), ((mp_b) * 255), ((mp_a) * 255))
- #define CT_RGBF(mp_r, mp_g, mp_b) \
- CT_RGBAF(mp_r, mp_g, mp_b, 0)
- namespace ct
- {
- struct axes2d
- {
- ct_float xmin;
- ct_float xmax;
- ct_float ymin;
- ct_float ymax;
- axes2d(ct_float xmin_, ct_float xmax_, ct_float ymin_, ct_float ymax_)
- : xmin(xmin_),
- xmax(xmax_),
- ymin(ymin_),
- ymax(ymax_)
- {
- }
- axes2d(ct_complex const& p0, ct_float r)
- : xmin(p0.real() - r),
- xmax(p0.real() + r),
- ymin(p0.imag() - r),
- ymax(p0.imag() + r)
- {
- }
- ct_float width() const { return xmax - xmin; }
- ct_float height() const { return ymax - ymin; }
- };
- struct plane2d
- {
- axes2d m_axes;
- unsigned int m_width;
- unsigned int m_height;
- ct_float m_aratio;
- ct_float m_xstep;
- ct_float m_ystep;
- plane2d(axes2d const& axes, unsigned int width, unsigned int height, bool aratio = true)
- : m_axes(axes),
- m_width(width),
- m_height(height),
- m_aratio((ct_float)height / width),
- m_xstep((axes.width() / (width - 1))),
- m_ystep((axes.height() / (height - 1)))
- {
- if (!aratio)
- {
- m_xstep /= m_aratio;
- return;
- }
- ct_float daspect = std::abs((ct_float)height / width);
- ct_float waspect = std::abs(m_axes.height() / m_axes.width());
- if (daspect > waspect)
- {
- ct_float excess = m_axes.height() * (daspect / waspect - 1);
- m_axes.ymax += excess / 2;
- m_axes.ymin -= excess / 2;
- }
- else if (daspect < waspect)
- {
- ct_float excess = m_axes.width() * (waspect / daspect - 1);
- m_axes.xmax += excess / 2;
- m_axes.xmin -= excess / 2;
- }
- m_xstep = m_axes.width() / ((width > 1) ? (width - 1) : width);
- m_ystep = m_axes.height() / ((height > 1) ? (height - 1) : height);
- }
- ct_float project_to(ct_float p) const
- {
- return std::floor(p / m_ystep);
- }
- ct_complex project_toxxx(ct_complex const& p) const
- {
- return ct_complex(
- std::floor((p.real() - m_axes.xmin) / m_xstep),
- std::floor((m_axes.ymax - p.imag()) / m_ystep)
- );
- }
- ct_complex project_to(ct_complex const& p) const
- {
- return ct_complex(
- std::floor((p.real() - m_axes.xmin) / m_xstep),
- std::floor((m_axes.ymax - p.imag()) / m_ystep)
- );
- }
- ct_complex project_to(unsigned int x, unsigned int y) const
- {
- return ct_complex(
- m_axes.xmin + x * m_xstep,
- m_axes.ymax - y * m_ystep
- );
- }
- };
- struct plot2d
- {
- unsigned int m_width;
- unsigned int m_height;
- ct::plane2d m_plane;
- BMP m_bmp;
- plot2d(ct::axes2d const& axes, unsigned int width, unsigned int height, bool aratio)
- : m_width(width),
- m_height(height),
- m_plane(axes, width, height, aratio)
- {
- m_bmp.SetSize(m_width, m_height);
- m_bmp.SetBitDepth(24);
- }
- void clear(RGBApixel const& color)
- {
- for (unsigned int x = 0; x < m_width; ++x)
- {
- for (unsigned int y = 0; y < m_height; ++y)
- {
- set_pixel(x, y, color);
- }
- }
- }
- void circle(ct_complex const& c, ct_float r, RGBApixel const& color)
- {
- ct_complex pc = m_plane.project_to(c);
- ct_float pr = m_plane.project_to(r);
- if (pr > m_height) return;
- DrawArc(m_bmp, pc.real(), pc.imag(), pr, 0, 6.28, color);
- //DrawArc(m_bmp, pc.real(), pc.imag(), pr + 1, 0, 6.28, color);
- }
- void line(ct_complex const& p0, ct_complex const& p1, RGBApixel const& color)
- {
- ct_complex pp0 = m_plane.project_to(p0);
- ct_complex pp1 = m_plane.project_to(p1);
- DrawLine(m_bmp, pp0.real(), pp0.imag(), pp1.real(), pp1.imag(), color);
- /*
- {
- RGBApixel xc = color;
- xc.Red /= 1.5;
- xc.Green /= 1.5;
- xc.Blue /= 1.5;
- DrawLine(m_bmp, pp0.real() + 1, pp0.imag() + 1, pp1.real() + 1, pp1.imag() + 1, xc);
- }
- {
- RGBApixel xc = color;
- xc.Red /= 2;
- xc.Green /= 2;
- xc.Blue /= 2;
- DrawLine(m_bmp, pp0.real() + 2, pp0.imag() + 2, pp1.real() + 2, pp1.imag() + 2, xc);
- }
- */
- }
- void arc(ct_complex const& c, ct_float r, ct_float origina, ct_float desta, RGBApixel const& color)
- {
- ct_complex pc = m_plane.project_to(c);
- ct_float pr = m_plane.project_to(r);
- //DrawArc(m_bmp, pc.real(), pc.imag(), pr, -toa, froma, color);
- DrawArc(m_bmp, pc.real(), pc.imag(), pr, (CT_PI2 - desta), (CT_PI2 - origina), color);
- }
- void set_pixelf(ct_complex const& p, RGBApixel const& color)
- {
- ct_complex pp = m_plane.project_to(p);
- if (pp.real() >= m_width || pp.real() < 0 ||
- pp.imag() >= m_height || pp.imag() < 0) return;
- set_pixelx(pp.real(), pp.imag(), color);
- //set_pixelx(pp.real() - 1, pp.imag() - 0, color);
- // set_pixelx(pp.real() - 0, pp.imag() - 1, color);
- // set_pixelx(pp.real() + 1, pp.imag() + 0, color);
- //set_pixelx(pp.real() + 0, pp.imag() + 1, color);
- }
- bool get_pixelf(RGBApixel& color, ct_complex const& p)
- {
- ct_complex pp = m_plane.project_to(p);
- if (pp.real() >= m_width || pp.real() < 0 ||
- pp.imag() >= m_height || pp.imag() < 0) return false;
- color = m_bmp.GetPixel(pp.real(), pp.imag());
- return true;
- }
- void set_pixelx(unsigned int x, unsigned int y, RGBApixel const& color)
- {
- if (x >= m_width || y >= m_height) return;
- RGBApixel pcolor = m_bmp.GetPixel(x, y);
- RGBApixel pcolorx = pcolor;
- pcolor.Red = pcolor.Red + color.Red;
- pcolor.Green = pcolor.Green + color.Green;
- pcolor.Blue = pcolor.Blue + color.Blue;
- if (pcolor.Red < pcolorx.Red) pcolor.Red = pcolorx.Red;
- if (pcolor.Green < pcolorx.Green) pcolor.Green = pcolorx.Green;
- if (pcolor.Blue < pcolorx.Red) pcolor.Blue = pcolorx.Blue;
- m_bmp.SetPixel(x, y, pcolor);
- }
- void set_pixel(unsigned int x, unsigned int y, RGBApixel const& color)
- {
- if (x >= m_width || y >= m_height) return;
- m_bmp.SetPixel(x, y, color);
- }
- bool get_pixel(RGBApixel& color, unsigned int x, unsigned int y)
- {
- if (x >= m_width || y >= m_height) return false;
- color = m_bmp.GetPixel(x, y);
- return true;
- }
- // RGBApixel color = plot.m_bmp.GetPixel(pp.real(), pp.imag());
- void save(char const* fname)
- {
- m_bmp.WriteToFile(fname);
- }
- };
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment