APL/APL1.2/TP07/flocon.c

110 lines
1.9 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <graph.h>
#include <math.h>
#include "graph_sup.h"
struct vertex {
double x;
double y;
struct vertex* next;
};
typedef struct vertex vert;
typedef vert* poly;
double rad(double deg) {
return deg/180 * M_PI;
}
poly makeTri(double x, double y, double r) {
vert* p1 = malloc(sizeof(vert));
p1->x = x + cos(rad(30)) * r;
p1->y = y + sin(rad(30)) * r;
vert* p2 = malloc(sizeof(vert));
p2->x = x + cos(rad(150)) * r;
p2->y = y + sin(rad(150)) * r;
vert* p3 = malloc(sizeof(vert));
p3->x = x + cos(rad(270)) * r;
p3->y = y + sin(rad(270)) * r;
p1->next = p2;
p2->next = p3;
p3->next = p1;
return p1;
}
void incPoly(poly* tri_add) {
poly tri = *tri_add;
vert* start = tri;
int has_started = 0;
while (tri != start || has_started == 0) {
has_started = 1;
int x = tri->x;
int y = tri->y;
vert* next = tri->next;
int xx = next->x;
int yy = next->y;
vert* p1 = malloc(sizeof(vert));
vert* p2 = malloc(sizeof(vert));
vert* p3 = malloc(sizeof(vert));
double tx = (xx - x) / 3;
double ty = (yy - y) / 3;
p1->x = x + tx;
p1->y = y + ty;
p3->x = x + tx * 2;
p3->y = y + ty * 2;
p2->x = p1->x + (cos(rad(-60)) * tx - sin(rad(-60)) * ty);
p2->y = p1->y + (sin(rad(-60)) * tx + cos(rad(-60)) * ty);
tri->next = p1;
p1->next = p2;
p2->next = p3;
p3->next = next;
tri = tri->next->next->next->next;
}
}
void drawPoly(poly tri) {
vert* start = tri;
int has_started = 0;
while (tri != start || has_started == 0) {
has_started = 1;
int x = tri->x;
int y = tri->y;
vert* next = tri->next;
int xx = next->x;
int yy = next->y;
tri = tri->next;
DessinerSegment(x, y, xx, yy);
}
}
int main(int argc, char * argv[]) {
poly tri = makeTri(1920/2, 1080/2, 550);
InitialiserGraphique();
CreerFenetre(100, 100, 1920, 1080);
SetColor(255, 0, 0);
for (int i = 0; i < 10; i++) incPoly(&tri);
drawPoly(tri);
Touche();
FermerGraphique();
return EXIT_SUCCESS;
}