Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ <h2>How to use</h2>
<li>Select the desired mode: Press <kbd>L</kbd> to draw a line, or <kbd>P</kbd> to draw a polygon</li>
<li>Click to draw polygon points. Press enter to finish the polygon.</li>
</ol>
<hr style="margin: 20px 0; border: 0; border-top: 1px solid #eee;">
<h2>Import / Edit</h2>
<p>Copy your NumPy array below to load and edit polygons.</p>
<textarea id="python-input" style="width: 100%; height: 150px; background: rgba(17,24,39,0.05); border: 1px solid #ddd; padding: 10px; font-family: monospace;" placeholder="Paste your NumPy array here, e.g., [np.array([[100, 200], ...])]"></textarea>
<button id="loadPythonButton" class="widgetButton left" style="margin-top: 10px;">Load Points from Python</button>
<hr style="margin: 20px 0; border: 0; border-top: 1px solid #eee;">
<h2>Coordinates</h2>
<p>Copy the points below, formatted as NumPy arrays, into your Python code.</p>
<a href="" id="copyPythonButton" class="widgetButton left">Copy Python to Clipboard</a>
Expand Down
77 changes: 76 additions & 1 deletion script.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,4 +712,79 @@ window.addEventListener('keydown', function(e) {
if (e.key === 'f' || e.key === 'F') {
toggleFullscreen();
}
})
})

// --- 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();
});