Software & Finance





Visual C++ - Crystal Drawing Animation





I have given here the source code drawing and rotating a crystal. Note that even if it looks like a 3D animation, I have not used any of 3D rotating algorithms.

 


Source Code


int rangle;

 

void DrawCrystal2(CPaintDC &dc, int left, int top, int right, int bottom, double rang, int h, COLORREF color)

{

    static const double PI = 3.1415926535;

    

    CPen pen;

    pen.CreatePen(PS_SOLID, 1, color);

    dc.SelectObject(pen);

 

    double cx = (left + right) / 2;

    double cy = (top + bottom) / 2;

 

    dc.SetPixel(cx, cy, RGB(0,0,255));

 

    double radius = (right - left) / 2;

 

    double yr = radius / 2;

 

    //dc.Ellipse(cx - radius, cy - radius / 2, cx + radius, cy + radius / 2);

 

    {

        double tempangle = rang;

        double a = (radius - (tempangle / 90 * yr)) * cos(tempangle * PI / 180);

        double b = (radius - (tempangle / 90 * yr)) * sin(tempangle * PI / 180);

       

        double x1 = cx + a;

        double y1 = cy + b;

        double x2 = cx - a;

        double y2 = cy - b;

        dc.SetPixel(cx + a, cy + b, RGB(255, 0, 0));

        dc.SetPixel(cx - a, cy - b, RGB(255, 0, 0));

 

        tempangle += 90;

        a = ((radius - yr) + ((tempangle - 90) / 90 * yr)) * cos(tempangle * PI / 180);

        b = ((radius - yr) + ((tempangle - 90) / 90 * yr)) * sin(tempangle * PI / 180);

        double x3 = cx + a;

        double y3 = cy + b;

        double x4 = cx - a;

        double y4 = cy - b;

       

        dc.MoveTo(x1, y1);

        dc.LineTo(x3, y3);

        dc.LineTo(x2, y2);

        dc.LineTo(x4, y4);

        dc.LineTo(x1, y1);

       

        dc.MoveTo(x1, y1);

        dc.LineTo(cx, top);

 

        dc.MoveTo(x3, y3);

        dc.LineTo(cx, top);

 

        dc.MoveTo(x2, y2);

        dc.LineTo(cx, top);

 

        dc.MoveTo(x4, y4);

        dc.LineTo(cx, top);

 

        dc.MoveTo(x1, y1);

        dc.LineTo(cx, bottom);

 

        dc.MoveTo(x3, y3);

        dc.LineTo(cx, bottom);

 

        dc.MoveTo(x2, y2);

        dc.LineTo(cx, bottom);

 

        dc.MoveTo(x4, y4);

        dc.LineTo(cx, bottom);

    }

}

 

 

// If you add a minimize button to your dialog, you will need the code below

//  to draw the icon.  For MFC applications using the document/view model,

//  this is automatically done for you by the framework.

 

void CCrystalDrawDlg::OnPaint()

{

      if (IsIconic())

      {

            CPaintDC dc(this); // device context for painting

 

            SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

 

            // Center icon in client rectangle

            int cxIcon = GetSystemMetrics(SM_CXICON);

            int cyIcon = GetSystemMetrics(SM_CYICON);

            CRect rect;

            GetClientRect(&rect);

            int x = (rect.Width() - cxIcon + 1) / 2;

            int y = (rect.Height() - cyIcon + 1) / 2;

 

            // Draw the icon

            dc.DrawIcon(x, y, m_hIcon);

      }

      else

      {

        CPaintDC dc(this); // device context for painting

       

            DrawCrystal2(dc, 200,50, 300, 300, rangle, 150, RGB(255,0,0));

            CDialog::OnPaint();

      }

}

 

// The system calls this function to obtain the cursor to display while the user drags

//  the minimized window.

HCURSOR CCrystalDrawDlg::OnQueryDragIcon()

{

      return static_cast<HCURSOR>(m_hIcon);

}

 

 

void CCrystalDrawDlg::OnTimer(UINT_PTR nIDEvent)

{

    int idx = nIDEvent;

    rangle += 3;

    if(rangle > 90)

        rangle = 0;

    Invalidate();

    CDialog::OnTimer(nIDEvent);

}

 

void CCrystalDrawDlg::OnBnClickedOk()

{

    // TODO: Add your control notification handler code here

    OnOK();

}


Click here to download the VS 2005 project and executable

 

Output