绘制“Cantor集合”图像:如下图所示,将一条线段3等分,将中间线段删掉,留下来的2条线段重复上述操作,经过无限次操作后,所得到的点集即为“Cantor集合”。
时间: 2024-03-10 13:46:29 浏览: 9
以下是MATLAB代码实现:
```matlab
% 设置迭代次数
n = 6;
% 初始化Cantor集合
x = [0 1];
y = [0 0];
% 迭代生成Cantor集合
for i = 1:n
% 计算Cantor集合的新点
x_new = [];
y_new = [];
for j = 1:length(x)-1
x1 = x(j);
x2 = x(j+1);
x_new = [x_new x1 (x1+x2)/3 (2*x1+x2)/3 x2];
y_new = [y_new 0 0 0];
end
x = x_new;
y = y_new;
end
% 绘制Cantor集合
plot(x,y,'k');
axis off;
```
此代码将生成一个线段,每次迭代将每个线段分成三个相等的部分,并在中间删除中间那一段。通过多次迭代,最终生成Cantor集合的形状。您可以尝试更改迭代次数,以查看生成的形状变化。
相关问题
使用MFC编程,在窗口中一次绘制n=0~5的所有Cantor图
Cantor图是一种分形图形,可以使用递归的方式来绘制。这里给出一个使用MFC编程,在窗口中一次绘制n=0~5的所有Cantor图的示例代码。
首先,需要在窗口的OnPaint函数中进行绘制。代码如下:
```
void CCantorView::OnPaint()
{
CPaintDC dc(this);
DrawCantor(dc, 0, 0, GetClientRect().Width(), 0, 5);
}
```
其中,DrawCantor是递归绘制Cantor图的函数。这个函数的代码如下:
```
void CCantorView::DrawCantor(CDC& dc, int x1, int y1, int x2, int y2, int n)
{
if (n == 0)
{
dc.MoveTo(x1, y1);
dc.LineTo(x2, y2);
}
else
{
int y = y1 + 20;
int x = (x1 + x2) / 3;
DrawCantor(dc, x1, y1, x, y, n - 1);
DrawCantor(dc, x2, y2, x, y, n - 1);
}
}
```
这个函数接收5个参数:绘制的起点和终点的坐标,以及递归的深度n。当n=0时,绘制一条线段;否则,递归绘制两个子Cantor图形,分别位于当前线段的左侧和右侧。
最后,需要在窗口的OnSize函数中进行重绘,以保证窗口大小改变时也能正确显示Cantor图形。代码如下:
```
void CCantorView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
Invalidate();
}
```
这里的Invalidate函数会触发窗口的重绘事件,从而调用OnPaint函数进行绘制。
完整的代码如下:
```
void CCantorView::OnPaint()
{
CPaintDC dc(this);
DrawCantor(dc, 0, 0, GetClientRect().Width(), 0, 5);
}
void CCantorView::DrawCantor(CDC& dc, int x1, int y1, int x2, int y2, int n)
{
if (n == 0)
{
dc.MoveTo(x1, y1);
dc.LineTo(x2, y2);
}
else
{
int y = y1 + 20;
int x = (x1 + x2) / 3;
DrawCantor(dc, x1, y1, x, y, n - 1);
DrawCantor(dc, x2, y2, x, y, n - 1);
}
}
void CCantorView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
Invalidate();
}
```
使用VC++6.0如何使用MFC编程,在窗口中一次绘制n=0~5的所有Cantor图
在VC++6.0中使用MFC编程,可以使用CDC类实现绘制。下面是一个示例程序,可以一次绘制0~5级的Cantor图:
```cpp
#include "stdafx.h"
#include "Cantor.h"
#include <vector>
#define MAX_LEVEL 5
using namespace std;
vector<CRect> rects;
Cantor::Cantor()
{
Create(NULL, _T("Cantor Set"), WS_OVERLAPPEDWINDOW, CRect(0, 0, 800, 600));
ShowWindow(SW_SHOW);
}
Cantor::~Cantor()
{
}
void Cantor::OnDraw(CDC* pDC)
{
for (int i = 0; i <= MAX_LEVEL; i++)
{
rects.clear();
DrawCantor(pDC, i, CRect(0, 0, 800, 600));
}
}
void Cantor::DrawCantor(CDC* pDC, int level, CRect rect)
{
if (level == 0)
{
rects.push_back(rect);
return;
}
int height = rect.Height() / 3;
CRect left(rect.left, rect.top, rect.right / 3, rect.top + height);
CRect right(rect.right * 2 / 3, rect.top, rect.right, rect.top + height);
DrawCantor(pDC, level - 1, left);
DrawCantor(pDC, level - 1, right);
for (int i = 0; i < rects.size(); i++)
{
pDC->FillSolidRect(rects[i], RGB(0, 0, 0));
}
}
BEGIN_MESSAGE_MAP(Cantor, CFrameWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()
void Cantor::OnPaint()
{
CPaintDC dc(this);
OnDraw(&dc);
}
```
在这个程序中,我们定义了一个Cantor类,并在其中重载了OnDraw和OnPaint方法。OnDraw方法用于绘制所有的Cantor图,而OnPaint方法则调用OnDraw方法进行绘制。
在DrawCantor方法中,我们使用递归的方式绘制Cantor图。对于每一级Cantor图,我们将其分成左右两个部分,并递归地绘制它们。当递归到最后一级时,我们将得到一个矩形列表rects,其中包含了所有需要绘制的矩形。我们遍历这个列表,使用FillSolidRect方法填充矩形,绘制出Cantor图。
在主函数中,我们创建了一个Cantor对象,并将其显示出来。这样就可以看到0~5级的Cantor图了。