Exercice langage C corrigé fonction arbre
Reprenez le programme vu en cours et permettant de dessiner un arbre :
#include
#include "swindow.h"
void dessine_arbre(SimpleWindow * window,
int x1, int y1, int x2, int y2, int nb_niveaux)
{
window->drawLine(x1, y1, x2, y2);
if (nb_niveaux > 0) {
int vx = 0.8 * (x2 - x1);
int vy = 0.8 * (y2 - y1);
float angle = 30 * 3.14159 / 180;
int xA = x2 + cos(angle) * vx - sin(angle) * vy;
int yA = y2 + sin(angle) * vx + cos(angle) * vy;
int xB = x2 + cos(-angle) * vx - sin(-angle) * vy;
int yB = y2 + sin(-angle) * vx + cos(-angle) * vy;
dessine_arbre(window, x2, y2, xA, yA, nb_niveaux - 1);
dessine_arbre(window, x2, y2, xB, yB, nb_niveaux - 1);
}
}
int main(int argc, char ** argv)
{
const int longueur = 500;
const int hauteur = 500;
SimpleWindow window("arbre", longueur, hauteur);
window.map();
// Remplit la fenetre en blanc:
window.color(1, 1, 1);
window.fill();
// Dessine en noir:
window.color(0, 0, 0);
// Instructions de dessin:
dessine_arbre(&window, 250, 450, 250, 350, 5);
window.show();
getchar();
return 0;
}Comment modifier le programme pour que les segments les plus petits soient affichés en vert, le reste de l’arbre étant toujours affiché en noir ?
La correction exercice langage C (voir page 2 en bas)