JSFiddle - React, Tailwind, and code Playground
HTML
<canvas id="canvas" width="600" height="400"></canvas>
CSS
body {
background-color: silver;
}
#canvas {
background-color: white;
display: block;
border: 1px solid #777;
margin: 5px auto;
}
JavaScript
"use strict";
// Canvas init
var cnv = document.getElementById("canvas"),
ctx = cnv.getContext('2d'),
width = cnv.width,
height = cnv.height;
// Functions
var DrawText = function(text, x, y) {
ctx.font="12px Arial";
ctx.fillStyle = '#333';
ctx.fillText(text, x, y);
}
var DrawCircle = function(point, radius, color) {
ctx.beginPath();
ctx.arc(point.x, point.y, radius, 0, 2 * Math.PI);
ctx.strokeStyle = color;
ctx.stroke();
ctx.fillStyle = color;
ctx.fill();
}
var DrawLine = function(pStart, pEnd, style) {
ctx.beginPath();
ctx.moveTo(pStart.x, pStart.y);
ctx.lineTo(pEnd.x, pEnd.y);
ctx.strokeStyle = style;
ctx.stroke();
}
var Point = function (x, y) {
this.x = x;
this.y = y;
}
var GetAngle = function(a, b, c) {
// два вектора
var v1 = new Point(a.x - b.x, a.y - b.y);
var v2 = new Point(c.x - b.x, c.y - b.y);
// длина вектора
function len(v) {
return Math.sqrt(v.x * v.x + v.y * v.y);
}
// находим косинус угла между векторами.
var cosAngle = (v1.x*v2.x + v1.y*v2.y) / (len(v1) * len(v2));
// находим угол через arcos
var angle = Math.acos(cosAngle);
return angle;
}
var lgAngleIndex = function(points, index, prev) {
var maxAngle = 0;
var largeIndex = 0;
var Prev;
if (index == 0)
Prev = new Point(points[index].x + 1, points[index].y);
else
Prev = new Point(points[prev].x, points[prev].y);
for (var i = 0; i < points.length; i++) {
if (i == index || i == prev) continue;
var tmp = GetAngle(Prev, points[index], points[i]);
if (tmp > maxAngle) {
maxAngle = tmp;
largeIndex = i;
}
}
return largeIndex;
}
var GetPoints = function(points) {
var indexes = [-1, 0];
var tmpIndex;
for (var i = 0; i < points.length; i++) {
tmpIndex = lgAngleIndex(points, indexes[indexes.length - 1], indexes[indexes.length - 2]);
if (tmpIndex == 0)
break;
indexes.push(tmpIndex);
}
indexes.shift()
return indexes;
}
// end functions
// Init random points
var points_length = 10;
var points =...