永发信息网

摇曳的递归分形树

答案:1  悬赏:30  手机版
解决时间 2021-05-14 09:28

该代码在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);
}
}

最佳答案
你好哦楼主~
很高兴看到你的问题。
但是又很遗憾到现在还没有人回答你的问题。也可能你现在已经在别的地方找到了答案,那就得恭喜你啦。
对于你的问题我爱莫能助!
可能是你问的问题有些专业了,或者别人没有遇到或者接触过你的问题,所以帮不了你。建议你去问题的相关论坛去求助,那里的人通常比较多,也会比较热心,能快点帮你解决问题。
快过年了,
最后祝您全家幸福健康快乐每一天!
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
"回头已是百年身"是谁说的?
推荐比较全面制作伴奏的软件
广州火车站到贵港的火车票
罗山县电脑之家这个地址怎么能查询到,着急去
东华理工大学怎么样,哈尔滨理工大学二本怎么
QQ飞车改装部件怎么得啊!!!!!!
宜阳县洛阳宜阳县卫生局在哪里啊,我有事要去
您好 我想给孩子改名
无线网卡是怎么
为什么我的冒险岛风灵接不了扎昆任务。。。
谁有索尼香水瓶MP3坏机
宁夏回族自治区的行政中心是?
快乐的语言名人名言,关于语言的魅力的格言
东西湖区武汉音乐学院哪位知道具体地址啊
每次运行使命召唤4一段时间后就卡死
推荐资讯
朋友要结婚了,我说恭喜,可她并没有兴奋的表
SS图表如何熄灭
长春劳伦斯美发学校使电话是多少啊
通山县金鸡谷地址在什么地方,想今天过去办事
古代唐家是中国的吗?
在成长中,酸一般是什么滋味?
什么是保值储蓄??或者有这项业务么
脸上生豆最好办法
挖掘机工资最高多少?
地下管线和物探哪个危险系数高?哪个更具发展
增加法术抗性宠物怎么学
淮阳区大唐翡翠怎么去啊,谁知道地址啊
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?