分治画分形图 之 谢尔宾斯基三角形

谢尔宾斯基三角形(英语:Sierpinski triangle)是一种分形,由波兰数学家谢尔宾斯基在1915年提出。它是自相似集的例子。它的豪斯多夫维是log(3)/log(2) ≈ 1.585。

谢尔宾斯基三角形

今天我们来学学如何用代码生成任意第n图形

对于这个图形,显然我们知道它的每个子结构都是相同的,所以具有递归的性质

所以我们把握好递归边界,递归分治往下画就行了

代码:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <cctype>
#include <list>
#include <cmath>
#include <bitset>
#include <queue>
#include <stack>
#include <sstream>
#include <functional>
#include <cassert>
using namespace std;

#define tmax(a, b) if (b > a) a = b
#define tmin(a, b) if (b < a) a = b
char inc() { char _[10]; scanf("%s", _); return _[0]; }
int ini() { int _; scanf("%d", &_); return _; }
long long inll() { long long _; scanf("%I64d", &_); return _; }
double ind() { double _; scanf("%lf", &_); return _; }
string ins() { string _; cin >> _; return _; }
int inl(char _[]) { if (!fgets(_, (int)1e8, stdin)) return -1; int i = strlen(_); if (_[i - 1] == '\n') _[--i] = 0; return i; }

typedef pair<int, int> pii;
typedef pair<char, char> pcc;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const LL lnf = 0x3f3f3f3f3f3f3f3f;
const double pi = 3.14159265358979323846;
const double eps = 1e-8;
const int mod = 100007;
const int maxn = 1e3+ 10;
char mp[maxn][maxn];

void Draw(int n, int x, int y) {
    if (n == 1) {
        mp[x][y + 1] = mp[x][y + 2] = '_';
        mp[x][y] = mp[x - 1][y + 1] = '/';
        mp[x - 1][y + 2] = mp[x][y + 3] = '\\';
        return;
    }
    int w = 1 << n;
    int h = w >> 1;
    Draw(n - 1, x, y);
    Draw(n - 1, x, y + w);
    Draw(n - 1, x - h, y + w / 2);
}

int main() {
    int CAS = 0;
    //std::ios::sync_with_stdio(0);
    //std::cin.tie(0);
#ifdef NIGHT_13
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    int time_night_13 = clock();
#endif // NIGHT_13

    int n = ini();
    Draw(n, (1 << n) - 1 , 0);
    for (int i = 0, st = 1 << n; i < 1 << n; ++i, ++st) {
        for (int j = 0; j <= st; ++j) {
            printf("%c", mp[i][j] ? mp[i][j] : ' ');
        }
        puts("");
    }

#ifdef NIGHT_13
    fprintf(stderr, "\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
    fprintf(stderr, "\t   Time: %dms", (int)clock() - time_night_13);
    fprintf(stderr, "\n...........................................\n\n");
#endif // NIGHT_13
    return 0;
}

运行效果:

-------------------n = 1--------------------------
 /\
/__\

-------------------n = 2--------------------------
   /\
  /__\
 /\  /\
/__\/__\

-------------------n = 3--------------------------
       /\
      /__\
     /\  /\
    /__\/__\
   /\      /\
  /__\    /__\
 /\  /\  /\  /\
/__\/__\/__\/__\

-------------------n = 4--------------------------
               /\
              /__\
             /\  /\
            /__\/__\
           /\      /\
          /__\    /__\
         /\  /\  /\  /\
        /__\/__\/__\/__\
       /\              /\
      /__\            /__\
     /\  /\          /\  /\
    /__\/__\        /__\/__\
   /\      /\      /\      /\
  /__\    /__\    /__\    /__\
 /\  /\  /\  /\  /\  /\  /\  /\
/__\/__\/__\/__\/__\/__\/__\/__\

-------------------n = 5--------------------------
                               /\
                              /__\
                             /\  /\
                            /__\/__\
                           /\      /\
                          /__\    /__\
                         /\  /\  /\  /\
                        /__\/__\/__\/__\
                       /\              /\
                      /__\            /__\
                     /\  /\          /\  /\
                    /__\/__\        /__\/__\
                   /\      /\      /\      /\
                  /__\    /__\    /__\    /__\
                 /\  /\  /\  /\  /\  /\  /\  /\
                /__\/__\/__\/__\/__\/__\/__\/__\
               /\                              /\
              /__\                            /__\
             /\  /\                          /\  /\
            /__\/__\                        /__\/__\
           /\      /\                      /\      /\
          /__\    /__\                    /__\    /__\
         /\  /\  /\  /\                  /\  /\  /\  /\
        /__\/__\/__\/__\                /__\/__\/__\/__\
       /\              /\              /\              /\
      /__\            /__\            /__\            /__\
     /\  /\          /\  /\          /\  /\          /\  /\
    /__\/__\        /__\/__\        /__\/__\        /__\/__\
   /\      /\      /\      /\      /\      /\      /\      /\
  /__\    /__\    /__\    /__\    /__\    /__\    /__\    /__\
 /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\
/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\


以下是Python绘制分形的基础方法,包括曼德勃罗集、分形树叶、科赫曲线、分形龙、谢尔宾斯基三角等。 1. 曼德勃罗集 曼德勃罗集是一种非常著名的分形形,可以用Python来绘制。代码如下: ```python import numpy as np import matplotlib.pyplot as plt def mandelbrot(c, max_iters=100): z = c n = 0 while abs(z) <= 2 and n < max_iters: z = z*z + c n += 1 if n == max_iters: return 0 else: return n def mandelbrot_set(xmin, xmax, ymin, ymax, width, height, max_iters=100): r1 = np.linspace(xmin, xmax, width) r2 = np.linspace(ymin, ymax, height) return np.array([[mandelbrot(complex(r, i), max_iters) for r in r1] for i in r2]) plt.imshow(mandelbrot_set(-2.0, 0.5, -1.25, 1.25, 1000, 1000, 100), cmap='hot') plt.axis('off') plt.show() ``` 2. 分形树叶 分形树叶是一种美丽的分形形,可以用Python来绘制。代码如下: ```python import turtle def draw_branch(branch_len, pensize): if branch_len > 5: turtle.forward(branch_len) turtle.right(20) turtle.pensize(pensize - 1) draw_branch(branch_len - 15, pensize - 1) turtle.left(40) draw_branch(branch_len - 15, pensize - 1) turtle.right(20) turtle.backward(branch_len) turtle.speed('fastest') turtle.left(90) turtle.penup() turtle.backward(200) turtle.pendown() turtle.pensize(10) turtle.color('brown') draw_branch(100, 10) turtle.done() ``` 3. 科赫曲线 科赫曲线是一种经典的分形形,可以用Python来绘制。代码如下: ```python import turtle def koch_curve(length, depth): if depth == 0: turtle.forward(length) return length = length / 3 koch_curve(length, depth - 1) turtle.left(60) koch_curve(length, depth - 1) turtle.right(120) koch_curve(length, depth - 1) turtle.left(60) koch_curve(length, depth - 1) turtle.penup() turtle.goto(-300, -100) turtle.pendown() turtle.color('blue') turtle.pensize(2) koch_curve(600, 4) turtle.done() ``` 4. 分形分形龙是一种神奇的分形形,可以用Python来绘制。代码如下: ```python import turtle def dragon_curve(length, depth, sign=1): if depth == 0: turtle.forward(length) return turtle.right(45 * sign) dragon_curve(length / (2 ** 0.5), depth - 1, 1) turtle.left(90 * sign) dragon_curve(length / (2 ** 0.5), depth - 1, -1) turtle.right(45 * sign) turtle.penup() turtle.goto(-200, -200) turtle.pendown() turtle.color('green') turtle.pensize(2) dragon_curve(400, 12) turtle.done() ``` 5. 谢尔宾斯基三角 谢尔宾斯基三角是一种经典的分形形,可以用Python来绘制。代码如下: ```python import turtle def sierpinski_triangle(length, depth): if depth == 0: for i in range(3): turtle.forward(length) turtle.left(120) return sierpinski_triangle(length / 2, depth - 1) turtle.forward(length / 2) sierpinski_triangle(length / 2, depth - 1) turtle.backward(length / 2) turtle.left(60) turtle.forward(length / 2) turtle.right(60) sierpinski_triangle(length / 2, depth - 1) turtle.left(60) turtle.backward(length / 2) turtle.right(60) turtle.penup() turtle.goto(-200, -200) turtle.pendown() turtle.color('orange') turtle.pensize(2) sierpinski_triangle(400, 5) turtle.done() ``` 以上是Python绘制分形的基础方法,希望对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值