// main.cp
// A minimal MFC application.
#include "math.h"
bool first = true;
struct coord
{
double xmin,xmax;
double ymin,ymax;
coord* next;
};
coord* windw;
coord* thiss;
class CMainApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};
class CMyFrame : public CFrameWnd
{
public:
CMyFrame();
protected:
afx_msg void OnPaint();
afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
afx_msg void OnRButtonDblClk(UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP()
};
CMainApp gMainApp;
BOOL CMainApp::InitInstance() {
m_pMainWnd = new CMyFrame();
m_pMainWnd->ShowWindow( m_nCmdShow );
m_pMainWnd->UpdateWindow();
return TRUE;
}
BEGIN_MESSAGE_MAP( CMyFrame, CFrameWnd )
ON_WM_PAINT()
ON_WM_LBUTTONDBLCLK()
ON_WM_RBUTTONDBLCLK()
END_MESSAGE_MAP()
CMyFrame::CMyFrame()
{
Create(NULL, "Mandelbrot Zoomer");
windw = new coord;
windw->xmin = -3;
windw->xmax = 1;
windw->ymin = -1.5;
windw->ymax = 1.5;
windw->next = NULL;
thiss = windw;
}
void CMyFrame::OnPaint()
{
CPaintDC dc( this );
int count,i,j,r,g,b;
double x,y,x1,y1,cx,cy;
for(i = 0;i < 720;i++)
for(j = 0;j < 540;j++)
{
/* generate complex numbers in the window [-2,1] x [-1.2, 1+] to
locate picture on screen for optimal viewing */
cx = ((thiss->xmax - thiss->xmin)/720.0) * i - fabs(thiss->xmin);
cy = fabs(thiss->ymax) - ((thiss->ymax - thiss->ymin)/540.0) * j;
x = cx;
y = cy;
// find orbit to a max of 255 iterations or when orbit tends to get huge
count = 0;
while(count <= 255 && x*x + y*y < 100000000)
{
x1 = x*x - y*y + cx;
y1 = 2*x*y + cy;
x = x1;
y = y1;
count++;
}
r = 0;
b = 0;
g = 0;
if(count < 5)
r = 128;
else if(count < 10)
r = 255;
else if(count < 25)
{
r = 255;
g = 255;
}
else if(count < 50)
g = 255;
else if(count < 100)
{
g = 255;
b = 255;
}
else if(count < 250)
b = 255;
dc.SetPixel(i,j, RGB(r,g,b));
}
if(first)
{
dc.TextOut(0,0,"To zoom in, double-click left mouse button");
dc.TextOut(0,20,"on the place that you want to magnify.");
dc.TextOut(0,40,"To zoom out, double-click right mouse button.");
}
}
void CMyFrame :: OnLButtonDblClk(UINT nFlags, CPoint point)
{
double dx, dy;
double dx2, dy2;
dx = point.x - 100;
dy = point.y - 75;
dx2 = point.x + 100;
dy2 = point.y + 75;
dx = ((thiss->xmax - thiss->xmin)/720.0) * dx - fabs(thiss->xmin);
dy = fabs(thiss->ymax) - ((thiss->ymax - thiss->ymin)/540.0) * dy;
dx2 = ((thiss->xmax - thiss->xmin)/720.0) * dx2 - fabs(thiss->xmin);
dy2 = fabs(thiss->ymax) - ((thiss->ymax - thiss->ymin)/540.0) * dy2;
thiss->next = new coord;
thiss = thiss->next;
thiss->xmin = dx;
thiss->xmax = dx2;
thiss->ymin = dy2;
thiss->ymax = dy;
thiss->next = NULL;
InvalidateRect(NULL);
first = false;
}
void CMyFrame :: OnRButtonDblClk(UINT nFlags, CPoint point)
{
if(windw->next != NULL)
{
coord* temp;
temp = windw;
while(temp->next->next != NULL)
temp = temp->next;
delete thiss;
thiss = temp;
thiss->next = NULL;
InvalidateRect(NULL);
first = false;
}
}