void tr(float x1, float y1, float x2, float y2, float x3, float y3)
{
line(int(x1), int(y1), int(x2), int(y2));
line(int(x2), int(y2), int(x3), int(y3));
line(int(x3), int(y3), int(x1), int(y1));
}
void dr(float x1, float y1, float x2, float y2, float x3, float y3, int n)
{
if (n>0) {
float x1n = (x1 + x2) / 2;
float y1n = (y1 + y2) / 2;
float x2n = (x2 + x3) / 2;
float y2n = (y2 + y3) / 2;
float x3n = (x3 + x1) / 2;
float y3n = (y3 + y1) / 2;
tr(x1n, y1n, x2n, y2n, x3n, y3n);
dr(x1, y1, x1n, y1n, x3n, y3n, n - 1);
dr(x2, y2, x1n, y1n, x2n, y2n, n - 1);
dr(x3, y3, x2n, y2n, x3n, y3n, n - 1);
}
}
Виклик процедури:
int iter=5;
tr(320,10,600,470,40,470);
dr(320,10,600,470,40,470,iter);
|
function dr( x1, y1, x2, y2, x3, y3, n){
if (n>0) {
let x1n,x2n,x3n,y1n,y2n,y3n;
x1n = (x1 + x2) / 2; y1n = (y1 + y2) / 2;
x2n = (x2 + x3) / 2; y2n = (y2 + y3) / 2;
x3n = (x3 + x1) / 2; y3n = (y3 + y1) / 2;
triangle(x1n, y1n, x2n, y2n, x3n, y3n);
dr(x1, y1, x1n, y1n, x3n, y3n, n - 1);
dr(x1n, y1n, x2, y2, x2n, y2n, n - 1);
dr(x3n, y3n, x2n, y2n, x3, y3,n - 1);
}
}
|