From 4ae8fcc9c8459c74f32c4c1395264ab564012f97 Mon Sep 17 00:00:00 2001 From: Murilo Andrade Date: Thu, 1 Jan 2026 23:58:08 -0300 Subject: [PATCH 1/2] Edit Polygon --- index.html | 6 +++++ script.js | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/index.html b/index.html index 4e0655e..1badfe2 100644 --- a/index.html +++ b/index.html @@ -46,6 +46,12 @@

How to use

  • Select the desired mode: Press L to draw a line, or P to draw a polygon
  • Click to draw polygon points. Press enter to finish the polygon.
  • +
    +

    Importar / Editar

    +

    Cole seu array NumPy abaixo para carregar e editar os polígonos.

    + + +

    Coordinates

    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..4c1979b 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("Por favor, cole o código NumPy na área de texto."); + 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(`Carregados ${newMasterPoints.length} polígono(s) com sucesso! Agora você pode editá-los.`); + } else { + alert("Nenhum polígono válido encontrado no texto. Verifique o formato."); + } + + } catch (e) { + console.error(e); + alert("Erro ao processar o texto. Verifique se o formato está correto."); + } +} + +// --- Adicionar o Listener ao Botão --- + +document.getElementById('loadPythonButton').addEventListener('click', function(e) { + e.preventDefault(); + loadPolygonsFromPython(); +}); \ No newline at end of file From 749e95e97deeacd5e08d46d52cc43594dba35039 Mon Sep 17 00:00:00 2001 From: Murilo Andrade Date: Sat, 3 Jan 2026 23:07:08 -0300 Subject: [PATCH 2/2] English version --- index.html | 8 ++++---- script.js | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/index.html b/index.html index 1badfe2..ea41392 100644 --- a/index.html +++ b/index.html @@ -47,10 +47,10 @@

    How to use

  • Click to draw polygon points. Press enter to finish the polygon.

  • -

    Importar / Editar

    -

    Cole seu array NumPy abaixo para carregar e editar os polígonos.

    - - +

    Import / Edit

    +

    Copy your NumPy array below to load and edit polygons.

    + +

    Coordinates

    Copy the points below, formatted as NumPy arrays, into your Python code.

    diff --git a/script.js b/script.js index 4c1979b..c9d4602 100644 --- a/script.js +++ b/script.js @@ -721,7 +721,7 @@ function loadPolygonsFromPython() { const inputValue = inputArea.value; if (!inputValue.trim()) { - alert("Por favor, cole o código NumPy na área de texto."); + alert("Please paste your NumPy code in the text area."); return; } @@ -771,14 +771,14 @@ function loadPolygonsFromPython() { // Atualizar os outputs de texto originais para garantir sincronia rewritePoints(); - alert(`Carregados ${newMasterPoints.length} polígono(s) com sucesso! Agora você pode editá-los.`); + alert(`Loaded ${newMasterPoints.length} polygon(s) successfully! You can now edit them.`); } else { - alert("Nenhum polígono válido encontrado no texto. Verifique o formato."); + alert("No valid polygons found in the text. Please check the format."); } } catch (e) { console.error(e); - alert("Erro ao processar o texto. Verifique se o formato está correto."); + alert("Error processing the text. Please check if the format is correct."); } }