diff --git a/index.html b/index.html index 4e0655e..ea41392 100644 --- a/index.html +++ b/index.html @@ -46,6 +46,12 @@
Copy your NumPy array below to load and edit polygons.
+ + +Copy the points below, formatted as NumPy arrays, into your Python code.
Copy Python to Clipboard diff --git a/script.js b/script.js index 5e068b3..c9d4602 100644 --- a/script.js +++ b/script.js @@ -712,4 +712,79 @@ window.addEventListener('keydown', function(e) { if (e.key === 'f' || e.key === 'F') { toggleFullscreen(); } -}) \ No newline at end of file +}) + +// --- Função para Interpretar o Texto NumPy --- + +function loadPolygonsFromPython() { + const inputArea = document.getElementById('python-input'); + const inputValue = inputArea.value; + + if (!inputValue.trim()) { + alert("Please paste your NumPy code in the text area."); + return; + } + + try { + const newMasterPoints = []; + + // 1. Separar os blocos por "np.array" + // O formato esperado é: [np.array([[x,y], [x,y]]), np.array(...)] + const parts = inputValue.split('np.array'); + + // Ignoramos a primeira parte se ela for apenas o início da lista "[" + for (let i = 1; i < parts.length; i++) { + const chunk = parts[i]; + const polygon = []; + + // 2. Regex para encontrar pares de coordenadas [x, y] + // Procura por colchetes contendo dígitos, vírgula, dígitos + const coordRegex = /\[\s*(\d+)\s*,\s*(\d+)\s*\]/g; + let match; + + while ((match = coordRegex.exec(chunk)) !== null) { + const x = parseInt(match[1]); + const y = parseInt(match[2]); + polygon.push([x, y]); + } + + if (polygon.length > 0) { + newMasterPoints.push(polygon); + } + } + + // 3. Atualizar o Estado do Canvas + if (newMasterPoints.length > 0) { + // Limpar estado atual + points = []; + masterPoints = newMasterPoints; + + // Recalcular cores (para garantir que cada novo polígono tenha uma cor) + masterColors = masterPoints.map((_, index) => { + return color_choices[index % color_choices.length]; + }); + + // Redesenhar tudo + drawAllPolygons(offScreenCtx); + blitCachedCanvas(); + + // Atualizar os outputs de texto originais para garantir sincronia + rewritePoints(); + + alert(`Loaded ${newMasterPoints.length} polygon(s) successfully! You can now edit them.`); + } else { + alert("No valid polygons found in the text. Please check the format."); + } + + } catch (e) { + console.error(e); + alert("Error processing the text. Please check if the format is correct."); + } +} + +// --- Adicionar o Listener ao Botão --- + +document.getElementById('loadPythonButton').addEventListener('click', function(e) { + e.preventDefault(); + loadPolygonsFromPython(); +}); \ No newline at end of file