中点圆算法(Midpoint Circle Algorithm)无输出问题求助
Hey there! Let's figure out why your circle isn't rendering even though you're confident the midpoint circle algorithm logic is right. The issue with var vertices = circle(100,100,200); not returning valid points is almost certainly one of a few common pitfalls for new JS/HTML canvas developers—let's break them down:
1. Did you forget to return the vertices array from your circle function?
This is the #1 mistake I see with custom algorithm functions. Even if your midpoint logic is perfect, if you don't explicitly return the collected vertices at the end of the function, vertices will be undefined when you call it.
Here's a corrected version of the midpoint circle function with the critical return statement:
function circle(x0, y0, radius) { const vertices = []; let x = radius; let y = 0; let err = 0; // Midpoint algorithm logic while (x >= y) { // Add all 8 symmetric points to the vertex array vertices.push([x0 + x, y0 + y]); vertices.push([x0 + y, y0 + x]); vertices.push([x0 - y, y0 + x]); vertices.push([x0 - x, y0 + y]); vertices.push([x0 - x, y0 - y]); vertices.push([x0 - y, y0 - x]); vertices.push([x0 + y, y0 - x]); vertices.push([x0 + x, y0 - y]); // Update error and coordinates if (err <= 0) { y++; err += 2 * y + 1; } if (err > 0) { x--; err -= 2 * x + 1; } } // THIS IS ESSENTIAL: return the collected vertices! return vertices; }
2. Are you properly using the vertices to draw on the canvas?
Even if vertices has valid points, you need to make sure you're using the canvas context correctly to render them. Double-check these steps:
- You've grabbed the canvas element and its 2D context
- You're starting a path, connecting the vertices, and calling
stroke()orfill()
Example drawing code to pair with the function above:
// Get canvas and context const canvas = document.getElementById('your-canvas-id'); const ctx = canvas.getContext('2d'); function renderCircle() { const vertices = circle(100, 100, 200); // Debug check: see what vertices you're getting console.log('Circle vertices:', vertices); if (!vertices || vertices.length === 0) { console.error('No vertices returned from circle function!'); return; } // Draw the circle from vertices ctx.beginPath(); ctx.moveTo(vertices[0][0], vertices[0][1]); // Start at first point for (let i = 1; i < vertices.length; i++) { ctx.lineTo(vertices[i][0], vertices[i][1]); // Connect each vertex } ctx.closePath(); // Close the circle shape ctx.strokeStyle = '#000'; ctx.lineWidth = 2; ctx.stroke(); // Render the outline } // Call the render function renderCircle();
3. Quick debugging tip
Add console.log(vertices) right after you call circle(100,100,200);. If the output is undefined, fix the return statement in your function. If it's an empty array, double-check your algorithm's loop logic (e.g., is the initial x/y set correctly? Is the loop condition x >= y working as expected?).
Start with verifying the return statement—that's almost always the culprit here!
内容的提问来源于stack exchange,提问作者Wes

