永发信息网

摇曳的递归分形树

答案:1  悬赏:30  手机版
解决时间 2021-05-14 09:28
  • 提问者网友:回忆在搜索
  • 2021-05-13 17:21

该代码在VC中实现绘制摇曳的递归分形树

请解释如何绘制树,如何用递归方法实现摇摆

// treeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "tree.h"
#include "treeDlg.h"
#include <math.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//定义PI
const double PI=3.11415926/180;

/////////////////////////////////////////////////////////////////////////////
// CTreeDlg dialog

CTreeDlg::CTreeDlg(CWnd* pParent )
: CDialog(CTreeDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTreeDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);


m_bRunning = FALSE;
m_Interval = 10;
m_bgColor = RGB(0, 0, 0);
m_fgColor = RGB(0, 255, 0);
m_C = 9;
m_dir = TRUE;

}

void CTreeDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTreeDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTreeDlg, CDialog)
//{{AFX_MSG_MAP(CTreeDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_LBUTTONDOWN()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTreeDlg message handlers

BOOL CTreeDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon


//获取窗口大小
GetClientRect(&m_Rect);
//得到物理画布
m_DC.Attach(::GetDC(*this));
//创建内存画布
m_MemDC.CreateCompatibleDC(&m_DC);
m_Bitmap.CreateCompatibleBitmap(&m_DC, m_Rect.Width(), m_Rect.Height());
m_MemDC.SelectObject(&m_Bitmap);
//设置画笔颜色
m_Pen.CreatePen(PS_SOLID, 1, m_fgColor);
m_MemDC.SelectObject(&m_Pen);
//填充背景颜色
m_MemDC.FillSolidRect(&m_Rect, m_bgColor);

return TRUE;
}

// 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 CTreeDlg::OnPaint()
{
AfxGetMainWnd()->SetWindowText("摇曳的分形树(分形频道:fractal.cn)2004 请单击窗体");
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (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
{
m_DC.BitBlt(0, 0, m_Rect.Width(), m_Rect.Height(),
&m_MemDC, 0, 0, SRCCOPY);

CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTreeDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

//单击暂停/恢复
void CTreeDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
if (m_bRunning) {
KillTimer(10);
m_bRunning = FALSE;
} else {
SetTimer(10, m_Interval, NULL);
m_bRunning = TRUE;
}

CDialog::OnLButtonDown(nFlags, point);
}

//定时器处理函数,在这个函数里绘制树
void CTreeDlg::OnTimer(UINT nIDEvent)
{
int x, y;

x = m_Rect.Width()/2;
y = m_Rect.Height()-50;

//在内存画布中清屏
m_MemDC.FillSolidRect(&m_Rect, m_bgColor);

//在内存画布中绘制树
drawLeaf(x, y, m_Rect.Height()/5, 270, 50, m_C);

if (m_dir) {
m_C += 0.5;
if (m_C > 18)
m_dir = FALSE;
} else {
m_C -= 0.5;
if (m_C < -18)
m_dir = TRUE;
}

//把树拷贝到物理画布中
m_DC.BitBlt(0, 0, m_Rect.Width(), m_Rect.Height(),
&m_MemDC, 0, 0, SRCCOPY);

CDialog::OnTimer(nIDEvent);
}

void CTreeDlg::drawLeaf(double x, double y, double L, double A, float B, float C)
{
double s1, s2, s3;
s1 = 2;
s2 = 3;
s3 = 1.3;
int x1,y1,x1L,y1L,x1R,y1R,x2,y2,x2R,y2R,x2L,y2L;
if(L > s1)
{
x2 = (int)(x + L * cos(A * PI));
y2 = (int)(y + L * sin(A * PI));
x2R = (int)(x2 + L / s2 * cos((A + B) * PI));
y2R = (int)(y2 + L / s2 * sin((A + B) * PI));
x2L = (int)(x2 + L / s2 * cos((A - B) * PI));
y2L = (int)(y2 + L / s2 * sin((A - B) * PI));

x1 = (int)(x + L / s2 * cos(A * PI));
y1 = (int)(y + L / s2 * sin(A * PI));
x1L = (int)(x1 + L / s2 * cos((A - B) * PI));
y1L = (int)(y1 + L / s2 * sin((A - B) * PI));
x1R = (int)(x1 + L / s2 * cos((A + B) * PI));
y1R = (int)(y1 + L / s2 * sin((A + B) * PI));


m_MemDC.MoveTo((int)x,(int)y);
m_MemDC.LineTo(x2,y2);
m_MemDC.LineTo(x2R,y2R);
m_MemDC.MoveTo(x2,y2);
m_MemDC.LineTo(x2L,y2L);
m_MemDC.MoveTo(x1,y1);
m_MemDC.LineTo(x1L,y1L);
m_MemDC.MoveTo(x1,y1);
m_MemDC.LineTo(x1R,y1R);

drawLeaf(x2, y2, L / s3, A + C, B, C);
drawLeaf(x2R, y2R, L / s2, A + B, B, C);
drawLeaf(x2L, y2L, L / s2, A - B, B, C);
drawLeaf(x1L, y1L, L / s2, A - B, B, C);
drawLeaf(x1R, y1R, L / s2, A + B, B, C);
}
}

最佳答案
  • 五星知识达人网友:醉吻情书
  • 2021-05-13 18:19
你好哦楼主~
很高兴看到你的问题。
但是又很遗憾到现在还没有人回答你的问题。也可能你现在已经在别的地方找到了答案,那就得恭喜你啦。
对于你的问题我爱莫能助!
可能是你问的问题有些专业了,或者别人没有遇到或者接触过你的问题,所以帮不了你。建议你去问题的相关论坛去求助,那里的人通常比较多,也会比较热心,能快点帮你解决问题。
快过年了,
最后祝您全家幸福健康快乐每一天!
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯