forked from Marilane20/modification_en_fonction-heure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
194 lines (150 loc) · 6.87 KB
/
script.js
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
function changerCouleurFondEnFonctionHeure() {
// Obtenir l'heure actuelle
const date = new Date();
const heures = date.getHours();
// Déterminer la couleur du fond en fonction de l'heure
const isJournee = heures >= 6 && heures < 18;
const couleurFond = isJournee ? "white" : "black";
// Appliquer la couleur du fond au corps du document
document.body.style.backgroundColor = couleurFond;
}
window.onload = function() {
// appeler la fonction pour changer le background
changerCouleurFondEnFonctionHeure();
};
// JavaScript pour résoudre l'équation et déclencher l'animation
function resolveEquation() {
// Récupère les valeurs des coefficients
var aInput = document.getElementById('a').value;
var bInput = document.getElementById('b').value;
var cInput = document.getElementById('c').value;
// Valide et convertit les valeurs des coefficients en parties réelle et imaginaire
var a = parseComplexNumber(aInput);
var b = parseComplexNumber(bInput);
var c = parseComplexNumber(cInput);
// Vérifie si le coefficient 'a' est différent de zéro pour une équation du second degré
if (a.real === 0 && a.imaginary === 0) {
alert("Le coefficient 'a' ne peut pas être égal à zéro dans une équation du second degré.");
return;
}
// Calcule le discriminant
var discriminant = b.real * b.real - 4 * a.real * c.real;
// Calcule les racines de l'équation
var x1, x2;
// Si le discriminant est positif ou nul, affiche l'animation du t-rex
if (discriminant >= 0) {
x1 = calculateRoot((-b.real + Math.sqrt(discriminant)) / (2 * a.real), a.imaginary, b.imaginary);
x2 = calculateRoot((-b.real - Math.sqrt(discriminant)) / (2 * a.real), a.imaginary, b.imaginary);
document.querySelector('.animation').removeAttribute('hidden');
document.querySelector('.my-anim-1').removeAttribute('hidden');
var animationDuration = calculateAnimationDuration(x1, x2);
document.querySelector('.t-rex').style.animationDuration = animationDuration;
document.querySelector('.my-anim-2').setAttribute('hidden', true);
} else {
var realPart = -b.real / (2 * a.real);
var imaginaryPart = Math.sqrt(-discriminant) / (2 * a.real);
x1 = formatComplexNumber(realPart, imaginaryPart);
x2 = formatComplexNumber(realPart, -imaginaryPart);
// Affiche l'animation du pterodactyle
document.querySelector('.animation').removeAttribute('hidden');
document.querySelector('.my-anim-2').removeAttribute('hidden');
document.querySelector('.my-anim-2').style.display = 'flex';
animatePterodactyl(); // Démarre l'animation du pterodactyle
document.querySelector('.my-anim-1').setAttribute('hidden', true);
}
document.getElementById('solution').innerText = `x1 = ${x1} et x2 = ${x2}`;
var isDaytime = checkDaytime();
fetchWeatherInfo(isDaytime);
}
// Fonction pour convertir une chaîne de caractères représentant un nombre complexe en objet contenant ses parties réelle et imaginaire
function parseComplexNumber(input) {
var matches = input.match(/(-?\d*(?:\.\d+)?)([+-]?\d*(?:\.\d+)?i)?/);
var real = parseFloat(matches[1]) || 0;
var imaginary = parseFloat(matches[2]) || 0;
return { real: real, imaginary: imaginary };
}
// Fonction pour calculer la différence entre les racines
function calculateAnimationDuration(x1, x2) {
var difference = Math.abs(parseFloat(x1) - parseFloat(x2));
var duration = difference * 1.5;
return Math.max(Math.min(duration, 10), 2) + 's';
}
// Fonction pour calculer la racine en prenant en compte la partie imaginaire
function calculateRoot(realPart, imaginaryA, imaginaryB) {
var real = realPart;
var imaginary = imaginaryA + imaginaryB;
return formatComplexNumber(real, imaginary);
}
// Fonction pour formater un nombre complexe sous forme de chaîne de caractères
function formatComplexNumber(real, imaginary) {
if (imaginary === 0) {
return real.toString();
} else if (real === 0) {
return `${imaginary}i`;
} else if (imaginary > 0) {
return `${real} + ${imaginary}i`;
} else {
return `${real} - ${Math.abs(imaginary)}i`;
}
}
/* Fonction pour vérifier s'il fait jour en fonction de l'heure locale de la machine
function checkDaytime() {
var currentHour = new Date().getHours();
return currentHour >= 6 && currentHour < 18;
}*/
// Fonction pour démarrer l'animation du pterodactyle
function animatePterodactyl() {
var pterodactylContainer = document.getElementById('pterodactylContainer');
pterodactylContainer.style.display = 'flex';
var position = 100;
var animationInterval = setInterval(frame, 40);
function frame() {
if (position <= -100) {
clearInterval(animationInterval);
} else {
position--;
pterodactylContainer.style.left = position + '%';
}
}
}
// Fonction pour vérifier s'il pleut
function isRaining(weatherData) {
return weatherData.weather.some(weather => weather.main.toLowerCase().includes('rain'));
}
function fetchWeatherInfo(isDaytime) {
var apiKey = '3ab95bbccb312fc4c53a58222f8e5952';
// var apiKey = '4d0168f1babd4e74801d4aa39f749660';
// Récupère les coordonnées géographiques de l'utilisateur
navigator.geolocation.getCurrentPosition(function(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var apiUrl = `https://api.openweathermap.org/data/3.0/onecall?lat=${latitude}&lon=${longitude}&appid=${apiKey}&`;
try {
fetch(apiUrl)
.then(response => response.json())
.then(data => {
var isRainingNow = isRaining(data); // Vérifie s'il pleut
applyWeatherStyles(isDaytime, isRainingNow); // Applique les styles en fonction des conditions météorologiques
})
.catch(error => console.error('Error fetching weather data:', error));
} catch (error) {
alert("Impossible de récupérer les informations météorologiques. Veuillez réessayer plus tard.");
}
});
}
function applyWeatherStyles(isDaytime, isRaining) {
var body = document.body;
if (isDaytime) {
document.querySelector('.my-body').style.backgroundColor = '#f8f8ff';
document.querySelector('.my-body').style.color = '#000';
} else {
document.querySelector('.my-body').style.backgroundColor = '#000';
document.querySelector('.my-body').style.color = '#fff';
}
var rainDiv = document.querySelector('.rain');
if (isRaining) {
rainDiv.removeAttribute('hidden'); // Affiche l'animation de pluie si nécessaire
} else {
rainDiv.setAttribute('hidden', true); // Masque l'animation de pluie si nécessaire
}
}