#include <Adafruit_ILI9341.h>
#include <TFT_eSPI.h>
#include <Adafruit_GFX.h>
// Écran ILI9341
// #define TFT_CLK 13 // Broche de l'horloge
// #define TFT_MISO 12 // Broche de la ligne MISO (inutile si vous n'utilisez pas la communication SPI)
// #define TFT_MOSI 11 // Broche de la ligne MOSI
#define TFT_CS 15 // Broche de la sélection de la puce
#define TFT_DC 2 // Broche de la commande/données
// #define TFT_RST 8 // Broche de réinitialisation (peut être connectée à RST ou laissée flottante)
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// Dimensions de l'écran
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240
// Variables pour la rotation du cube
float angleX = 0;
float angleY = 0;
float angleZ = 0;
float targetAngleX = 0;
float targetAngleY = 0;
float targetAngleZ = 0;
float interpolationAmount = 0.05;
// Points du cube
float points[8][3] = {
{-50, -50, -50},
{50, -50, -50},
{50, 50, -50},
{-50, 50, -50},
{-50, -50, 50},
{50, -50, 50},
{50, 50, 50},
{-50, 50, 50}
};
// Arêtes du cube
int edges[12][2] = {
{0, 1},
{1, 2},
{2, 3},
{3, 0},
{4, 5},
{5, 6},
{6, 7},
{7, 4},
{0, 4},
{1, 5},
{2, 6},
{3, 7}
};
void setup() {
// Initialisation de l'écran
tft.begin();
tft.setRotation(3);
tft.fillScreen(ILI9341_BLACK);
}
void loop() {
// Efface l'écran
tft.fillScreen(ILI9341_BLACK);
// Calcule la rotation du cube
if (angleX != targetAngleX) {
angleX = lerp(angleX, targetAngleX, interpolationAmount);
}
if (angleY != targetAngleY) {
angleY = lerp(angleY, targetAngleY, interpolationAmount);
}
if (angleZ != targetAngleZ) {
angleZ = lerp(angleZ, targetAngleZ, interpolationAmount);
}
// Dessine les arêtes du cube en rotation
for (int i = 0; i < 12; i++) {
int p1Index = edges[i][0];
int p2Index = edges[i][1];
// Applique les transformations de rotation
float x1 = points[p1Index][0];
float y1 = points[p1Index][1];
float z1 = points[p1Index][2];
float x2 = points[p2Index][0];
float y2 = points[p2Index][1];
float z2 = points[p2Index][2];
// Rotation autour de l'axe X
float x1Rotated = x1;
float y1Rotated = y1 * cos(angleX) - z1 * sin(angleX);
float z1Rotated = y1 * sin(angleX) + z1 * cos(angleX);
float x2Rotated = x2;
float y2Rotated = y2 * cos(angleX) - z2 * sin(angleX);
float z2Rotated = y2 * sin(angleX) + z2 * cos(angleX);
// Rotation autour de l'axe Y
float x1RotatedY = x1Rotated * cos(angleY) + z1Rotated * sin(angleY);
float y1RotatedY = y1Rotated;
float z1RotatedY = -x1Rotated * sin(angleY) + z1Rotated * cos(angleY);
float x2RotatedY = x2Rotated * cos(angleY) + z2Rotated * sin(angleY);
float y2RotatedY = y2Rotated;
float z2RotatedY = -x2Rotated * sin(angleY) + z2Rotated * cos(angleY);
// Rotation autour de l'axe Z
float x1RotatedYZ = x1RotatedY * cos(angleZ) - y1RotatedY * sin(angleZ);
float y1RotatedYZ = x1RotatedY * sin(angleZ) + y1RotatedY * cos(angleZ);
float z1RotatedYZ = z1RotatedY;
float x2RotatedYZ = x2RotatedY * cos(angleZ) - y2RotatedY * sin(angleZ);
float y2RotatedYZ = x2RotatedY * sin(angleZ) + y2RotatedY * cos(angleZ);
float z2RotatedYZ = z2RotatedY;
// Calcule les coordonnées sur l'écran
int x1Screen = SCREEN_WIDTH / 2 + x1RotatedYZ;
int y1Screen = SCREEN_HEIGHT / 2 + y1RotatedYZ;
int x2Screen = SCREEN_WIDTH / 2 + x2RotatedYZ;
int y2Screen = SCREEN_HEIGHT / 2 + y2RotatedYZ;
// Dessine l'arête
tft.drawLine(x1Screen, y1Screen, x2Screen, y2Screen, ILI9341_WHITE);
}
// Augmente l'angle de rotation pour l'animation
targetAngleX += 0.01;
targetAngleY += 0.01;
targetAngleZ += 0.01;
// Attend un court instant pour ralentir l'animation
// delay(10);
}
float lerp(float start, float end, float amount) {
return start + (end - start) * amount;
}