Spaces:
Running
Running
File size: 7,261 Bytes
bfa8e66 e074385 7210c6e bfa8e66 7210c6e e074385 7210c6e 1cb0601 7210c6e 1cb0601 bfa8e66 1cb0601 bfa8e66 e074385 1cb0601 bfa8e66 e074385 7210c6e bfa8e66 e074385 bfa8e66 7210c6e e074385 bfa8e66 e074385 bfa8e66 7210c6e bfa8e66 | 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 | document.addEventListener('DOMContentLoaded', function() {
// DOM elements
const glassTypeSelect = document.getElementById('glass-type');
const customGlassDiv = document.getElementById('custom-glass');
const tableShapeSelect = document.getElementById('table-shape');
const roundTableDiv = document.getElementById('round-table');
const rectTableDiv = document.getElementById('rect-table');
const calculateBtn = document.getElementById('calculate-btn');
const resultsDiv = document.getElementById('results');
const noResultsDiv = document.getElementById('no-results');
const exportBtn = document.getElementById('export-btn');
// Glass type presets
const glassPresets = {
champagne: { height: 18, diameter: 6 },
wine: { height: 15, diameter: 6.5 },
water: { height: 12, diameter: 7 },
cocktail: { height: 14, diameter: 8 }
};
// Arrangement pattern - only triangle
const arrangementPattern = (count) => {
// Classic triangle formation
const base = Math.ceil((Math.sqrt(8 * count + 1) - 1) / 2);
return Array.from({length: base}, (_, i) => i + 1);
};
// Event listeners
glassTypeSelect.addEventListener('change', function() {
if (this.value === 'custom') {
customGlassDiv.classList.remove('hidden');
} else {
customGlassDiv.classList.add('hidden');
}
});
tableShapeSelect.addEventListener('change', function() {
if (this.value === 'round') {
roundTableDiv.classList.remove('hidden');
rectTableDiv.classList.add('hidden');
} else {
roundTableDiv.classList.add('hidden');
rectTableDiv.classList.remove('hidden');
}
});
calculateBtn.addEventListener('click', calculatePyramid);
exportBtn.addEventListener('click', exportToPDF);
// Initial setup
feather.replace();
function calculatePyramid() {
// Get input values
const guests = parseInt(document.getElementById('guests').value) || 0;
if (guests <= 0) {
alert('Veuillez entrer un nombre valide de convives');
return;
}
// Get glass dimensions
let glassHeight, glassDiameter;
if (glassTypeSelect.value === 'custom') {
glassHeight = parseFloat(document.getElementById('glass-height').value) || 0;
glassDiameter = parseFloat(document.getElementById('glass-diameter').value) || 0;
} else {
const preset = glassPresets[glassTypeSelect.value];
glassHeight = preset.height;
glassDiameter = preset.diameter;
}
// Get table dimensions
let tableWidth, tableLength;
if (tableShapeSelect.value === 'round') {
const diameter = parseFloat(document.getElementById('table-diameter').value) || 0;
tableWidth = diameter;
tableLength = diameter;
} else {
tableLength = parseFloat(document.getElementById('table-length').value) || 0;
tableWidth = parseFloat(document.getElementById('table-width').value) || 0;
}
// Calculate arrangement
const pattern = arrangementPattern(guests);
const totalGlasses = pattern.reduce((a, b) => a + b, 0);
const maxRow = Math.max(...pattern);
// Calculate pyramid dimensions
const baseGlasses = pattern[pattern.length - 1];
const pyramidHeight = pattern.length * glassHeight;
const baseArea = maxRow * glassDiameter * pattern.length * glassDiameter * 0.9;
// Check feasibility
const feasible = maxRow * glassDiameter * 0.9 <= Math.min(tableWidth, tableLength);
// Display results
document.getElementById('total-glasses').textContent = totalGlasses;
document.getElementById('base-glasses').textContent = baseGlasses;
document.getElementById('pyramid-height').textContent = pyramidHeight.toFixed(1);
document.getElementById('base-area').textContent = baseArea.toFixed(1) + ' cm²';
const feasibilityResult = document.getElementById('feasibility-result');
const feasibilityDiv = document.getElementById('feasibility');
if (feasible) {
feasibilityResult.textContent = '✓ Disposition possible';
feasibilityDiv.className = 'bg-white p-4 rounded-lg shadow valid';
} else {
feasibilityResult.textContent = '✗ Table trop petite';
feasibilityDiv.className = 'bg-white p-4 rounded-lg shadow invalid';
}
// Show visualization
visualizeArrangement(pattern, glassDiameter * 10, tableWidth, tableLength);
// Show results
noResultsDiv.classList.add('hidden');
resultsDiv.classList.remove('hidden');
}
function visualizeArrangement(pattern, glassSize, tableWidth, tableHeight) {
const container = document.getElementById('visualization');
container.innerHTML = '';
// Create canvas
const canvas = document.createElement('canvas');
canvas.width = container.clientWidth;
canvas.height = container.clientHeight;
container.appendChild(canvas);
const ctx = canvas.getContext('2d');
const scale = Math.min(canvas.width / (tableWidth * 1.2), canvas.height / (tableHeight * 1.2));
// Draw table
ctx.fillStyle = '#e2e8f0';
if (tableShapeSelect.value === 'round') {
const radius = (tableWidth / 2) * scale;
ctx.beginPath();
ctx.arc(canvas.width / 2, canvas.height / 2, radius, 0, Math.PI * 2);
ctx.fill();
ctx.strokeStyle = '#94a3b8';
ctx.stroke();
} else {
const width = tableWidth * scale;
const height = tableHeight * scale;
ctx.fillRect((canvas.width - width) / 2, (canvas.height - height) / 2, width, height);
ctx.strokeStyle = '#94a3b8';
ctx.strokeRect((canvas.width - width) / 2, (canvas.height - height) / 2, width, height);
}
// Draw glasses in triangle formation
const glassRadius = (glassSize / 2) * scale;
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
ctx.fillStyle = 'rgba(147, 51, 234, 0.3)';
ctx.strokeStyle = 'rgba(147, 51, 234, 0.7)';
ctx.lineWidth = 1;
// Triangle arrangement
for (let row = 0; row < pattern.length; row++) {
const glassesInRow = pattern[row];
const y = centerY - (row * glassRadius * 1.5) + (pattern.length * glassRadius * 0.7);
for (let col = 0; col < glassesInRow; col++) {
const x = centerX - ((glassesInRow - 1) * glassRadius) + (col * glassRadius * 2);
ctx.beginPath();
ctx.arc(x, y, glassRadius, 0, Math.PI * 2);
ctx.fill();
ctx.stroke();
}
}
}
function exportToPDF() {
alert('Fonctionnalité PDF en cours de développement');
// In a real implementation, we would use jsPDF or similar library
}
}); |