Step 1: find the center of the polygon using the midpoint of the points
This function will find the center defined by all points in the drawing, regardless of order:
function findCenter(points) { var x = 0, y = 0, i, len = points.length; for (i = 0; i < len; i++) { x += points[i].x; y += points[i].y; } return {x: x / len, y: y / len};
Demo showing the center point in a polygon.
var pointsArray = []; var canvas = document.getElementById("myCanvas"); var context = canvas.getContext("2d"); function Point(x, y) { this.x = x; this.y = y; } function drawDot(e) { var position = getMousePosition(canvas, e); posx = position.x; posy = position.y; storeCoordinate(posx, posy); context.fillStyle = "#F00"; context.fillRect(posx, posy, 6, 6); } function getMousePosition(c, e) { var rect = canvas.getBoundingClientRect(); return {x: e.clientX - rect.left, y: e.clientY - rect.top} } function storeCoordinate(xVal, yVal) {pointsArray.push(new Point(xVal, yVal))} $("#solve").click( function() { var p = findCenter(pointsArray); context.fillStyle = "green"; context.fillRect(px, py, 4, 4); } ); function findCenter(points) { var x = 0, y = 0, i, len = points.length; for (i = 0; i < len; i++) { x += points[i].x; y += points[i].y; } return {x: x / len, y: y / len};
#myCanvas {border: 1px solid #000}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <canvas id="myCanvas" width="400" height="300" onclick="drawDot(event)"></canvas> <div> <button type="button" class="btn btn-default" id="solve">Show center point</button> </div>
Step 2: Sort Points Based on Angle
- Extend the object in the same way as the angle argument.
- Iterate over a point array
- Calculate angle relative to center point
- Angle-based array sorting
To find the angles, simply calculate the angle relative to the center point.
Here's how:
function findAngles(c, points) { var i, len = points.length, p, dx, dy; for (i = 0; i < len; i++) { p = points[i]; dx = px - cx; dy = py - cy; p.angle = Math.atan2(dy, dx); } }
Then you need to sort the points based on the angle using a special sorting function. Just use the standard sort() method in the array and put your own function, which will use the angle property of the point feature:
pointsArray.sort(function(a, b) { if (a.angle > b.angle) return 1; else if (a.angle < b.angle) return -1; return 0; });
Then draw a line between all the points.
Working demo
var pointsArray = []; var canvas = document.getElementById("myCanvas"); var context = canvas.getContext("2d"); function Point(x, y) { this.x = x; this.y = y; this.angle = 0; } canvas.onclick = drawDot; function drawDot(e) { var position = getMousePosition(canvas, e); posx = position.x; posy = position.y; storeCoordinate(posx, posy); context.fillStyle = "#F00"; context.fillRect(posx-3, posy-3, 6, 6); } function getMousePosition(c, e) { var rect = canvas.getBoundingClientRect(); return {x: e.clientX - rect.left, y: e.clientY - rect.top} } function storeCoordinate(xVal, yVal) {pointsArray.push(new Point(xVal, yVal))} $("#solve").click( function() {
#myCanvas {border: 1px solid #000}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <canvas id="myCanvas" width="400" height="300"></canvas> <div><button id="solve">Draw Polygon</button><button id="reset">Reset</button></div>
source share