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 141 142 143 144 145 146 147 148 149
| // Import de classes de dessin
import java.awt.*;
import javax.swing.*;
// Import de la classe permettant l'utilisation des fonctions mathématiques
import java.lang.Math;
public class vonKoch {
// Définition de la fenêtre d'affichage héritée d'une JFrame
public class maFenêtre extends JFrame {
JPanel pan;
public maFenêtre() {
// Titre de la fenêtre
super("Tracé d'un flocon de Von Koch");
// Taille de la fenêtre
setSize(1000,1000);
// Définition du panneau contenu dans la fenêtre
pan = new Panneau();
pan.setBackground(Color.white); // fond de fenêtre blanc
getContentPane().add(pan);
}
}
// Fonction calculant les coordonnées de C image de B par la rotation de centre
//A et d'angle +PI/3
double[] rotation(double[] pointA, double[] pointB) {
double[] rotation = new double[2];
rotation[0] = Math.cos(Math.PI/3)*(pointB[0]-pointA[0]) - Math.sin(Math.PI/3)*(pointB[1]-pointA[1]) + pointA[0];
rotation[1] = Math.sin(Math.PI/3)*(pointB[0]-pointA[0]) + Math.cos(Math.PI/3)*(pointB[1]-pointA[1]) + pointA[1];
return rotation;
}
// Définition du panneau d'affichage hérité d'un JPanel
class Panneau extends JPanel {
// Rédéfinition de la méthode paintComponent
public void paintComponent(Graphics g) {
// DEFINITION DE LA PARTIE GRAPHIQUE
// Héritage de la méthode existante
super.paintComponent(g);
// Restriction à la 2D
Graphics2D g2 = (Graphics2D) g;
// Définition des dimensions de la fenêtre
int hauteur = this.getHeight();
int largeur = this.getWidth();
// Redéfinition de l'origine du repère au centre de la fenêtre
g2.translate(largeur/2, hauteur/2);
// CHOIX DU NOMBRE D'ITERATIONS N
int N = 0;
// TRACE DU TRIANGLE ABC DE BASE (N=0)
// Coordonnées du sommet A
double[] A = new double[2];
A[0] = -400;
A[1] = -250;
// Coordonnées du sommet B
double B[] = new double[2];
B[0] = 400;
B[1] = -250;
// Coordonnées de C
double[] C = new double[2];
C = rotation(A, B);
g.setColor(Color.red);
g.drawLine((int)A[0], (int)A[1], (int)B[0], (int)B[1]);
g.drawLine((int)A[0], (int)A[1], (int)C[0], (int)C[1]);
g.drawLine((int)B[0], (int)B[1], (int)C[0], (int)C[1]);
// TRACE DES FLOCONS PAR RECURSIVITE SUR CHACUN DES COTES
if (N>0) {
vonkoch(B, A, N, g);
vonkoch(A, C, N, g);
vonkoch(C, B, N, g);
}
}
}
// Procédure permettant de partager un segment A(x1;y1)-B(x2;y2) en 3
// et de construire un triangle équilatéral sur le segment central
void vonkoch(double[] pointA, double[] pointB, int nb, Graphics g) {
// Décrémentation du nombre d'itérations
nb = nb -1 ;
// Efface le segment [AB] tracé
g.setColor(Color.white);
g.drawLine((int)pointA[0], (int)pointA[1], (int)pointB[0], (int)pointB[1]);
// Création du barycentre A2 de (A;2) et (B;1)
double[] pointA2 = new double[2];
for(int i = 0; i <2 ; i++) {
pointA2[i] = (2*pointA[i] + pointB[i])/3;
}
// Création du barycentre B2 de (A;1) et (B;2)
double[] pointB2 = new double[2];
for(int i = 0; i <2 ; i++) {
pointB2[i] = (pointA[i] + 2*pointB[i])/3;
}
// Création du sommet C2 du petit triangle équilatéral
double[] pointC2 = new double[2];
pointC2 = rotation(pointA2, pointB2);
// Tracé des quatre segments du flocon
g.setColor(Color.red);
g.drawLine((int)pointA[0], (int)pointA[1], (int)pointA2[0], (int)pointA2[1]);
g.drawLine((int)pointA2[0], (int)pointA2[1], (int)pointC2[0], (int)pointC2[1]);
g.drawLine((int)pointC2[0], (int)pointC2[1], (int)pointB2[0], (int)pointB2[1]);
g.drawLine((int)pointB2[0], (int)pointB2[1], (int)pointB[0], (int)pointB[1]);
if (nb > 0) {
vonkoch(pointA, pointA2, nb, g);
vonkoch(pointA2, pointC2, nb, g);
vonkoch(pointC2, pointB2, nb, g);
vonkoch(pointB2, pointB, nb, g);
}
}
// Constructeur
vonKoch() {
JFrame fenetre = new maFenêtre();
fenetre.setVisible(true);
}
public static void main(String[] args) {
vonKoch fen = new vonKoch();
}
} |
Partager