科赫雪花
绘图库:Easy Graphics Engine (EGE)
编程语言:c++
科赫雪花
n=5
反科赫雪花
n=5
代码:
#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include <time.h>
const double pi = 3.1415926536;struct Point
{double x;double y;
};Point Rotate(Point p1, Point p2, double A)
{Point r;r.x = p1.x + (p2.x - p1.x) * cos(A) + (p2.y - p1.y) * sin(A);r.y = p1.y + (p2.y - p1.y) * cos(A) - (p2.x - p1.x) * sin(A);return r;
}Point Zoom(Point p1, Point p2, double a)
{Point r;r.x = p1.x + (p2.x - p1.x) * a;r.y = p1.y + (p2.y - p1.y) * a;return r;
}
void Draw2(Point p1, Point p2,int n)//雪花
{if(n>0){Point p3,p4,p;p3.x=p2.x/3+2*p1.x/3;p3.y=p2.y/3+2*p1.y/3;p4.x=2*p2.x/3+p1.x/3;p4.y=2*p2.y/3+p1.y/3;p=Rotate(p3,p1,120*pi/180);setcolor(CYAN);line_f(p1.x,p1.y,p3.x,p3.y);line_f(p3.x,p3.y,p.x,p.y);line_f(p.x,p.y,p4.x,p4.y);line_f(p4.x,p4.y,p2.x,p2.y);setcolor(BLACK);line_f(p3.x,p3.y,p4.x,p4.y);Draw2(p1,p3,n-1);Draw2(p3,p,n-1);Draw2(p,p4,n-1);Draw2(p4,p2,n-1);}
}
int main()
{int n=5;struct Point p1,p2,p3;p1.x=100;p1.y=450;p2.x=500;p2.y=450;p3= Rotate(p1,p2,pi/7);printf(" 科赫雪花\n");printf("\n\n");initgraph(600,600);setcolor(RED);Draw2(p1,p2,n);Draw2(p3,p1,n);Draw2(p2,p3,n);getch();closegraph();return 0;
}