1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| #include <easyx.h> #include <conio.h> #include <vector> #include <ctime> #include <cstdlib> #include <cmath> #include <iostream>
#define SEE(x) cout << #x << ":" << x << endl; #define RED (RGB(240, 128, 128)) #define WHITE (RGB(255, 255, 255)) #define BROWN (RGB(160, 82, 45))
const int angle_min = 18; const int angle_max = 28;
const int length_min = 60; const int length_max = 90;
const int thick = 10;
int width = 800, height = 600;
using namespace std;
typedef pair<int, int> Point;
void drawline(Point st, Point ed) { line(st.first, st.second, ed.first, ed.second); }
int rand_range(int st, int ed) { return st + rand() % (ed - st); }
Point getPointFromAngle(Point src, float angle, int length) { angle = 3.14/180.0 * angle; return { src.first+length*cos(angle), src.second-length*sin(angle) }; }
void draw_bifurcation(Point p, float angle, int layer) { if (layer == 13) { return; } srand(time(NULL)); float delta = rand_range(angle_min, angle_max); float shrink = pow(layer, 0.5); float left_angle = angle + delta/shrink; float right_angle = left_angle - 2.4*delta/shrink; int length_left = rand_range(length_min / shrink, length_max / shrink); int length_right = rand_range(length_min / shrink, length_max / shrink); Point left = getPointFromAngle(p, left_angle, length_left); Point right = getPointFromAngle(p, right_angle, length_right); int type = rand() % 2; if (layer > 8) { if (type == 0) { setlinecolor(RED); } else { setlinecolor(WHITE); } } else { setlinecolor(BROWN); } setlinestyle(PS_SOLID , thick/layer); drawline(p, left); draw_bifurcation(left, left_angle, layer + 1); setlinestyle(PS_SOLID, thick / layer); if (layer > 8) { if (type == 0) { setlinecolor(WHITE); } else { setlinecolor(RED); } } else { setlinecolor(BROWN); } Sleep(1); drawline(p, right); draw_bifurcation(right, right_angle, layer + 1); }
void draw_ground() { int left = width * 0; int right = width * 1; int up = height - 60; int down = height; int length = 4; setlinecolor(RED); setlinestyle(PS_SOLID, 3); int x, y, angle; for (int i = 0; i < 300; i++) { x = rand_range(left, right); y = rand_range(up, down); angle = rand_range(0, 360); Point ed = getPointFromAngle({ x, y }, angle, length); Sleep(5); drawline({ x, y }, ed); } }
int main() { initgraph(width, height); setbkcolor(RGB(241, 215, 118)); cleardevice(); setlinecolor(BROWN); setlinestyle(PS_SOLID | PS_ENDCAP_SQUARE, 10); line(width/2, height-110, width/2, height-20); draw_bifurcation({ width/2, height-110 }, 90.0, 1); draw_ground(); _getch(); closegraph(); return 0; }
|