tangents
by magneto903
HTML
<!DOCTYPE html>
<html>
<head>
<title>easy release</title>
<meta charset="utf-8">
</head>
<body>
<canvas id="canvas" width="700" height="700" ></canvas>
</body>
</html>
JavaScript
// Точка
var Point = function(x=0, y=0) {
this.x = x;
this.y = y;
this.minus = function(other_p) {
return new Point(this.x - other_p.x, this.y - other_p.y)
}
this.p_multi = function(other_p) {
return this.x * other_p.y - this.y * other_p.x;
}
this.multi = function(other_p) {
return this.x * other_p.x + this.y * other_p.y;
}
this.Len2 = function() {
return Math.sqrt(this.x*this.x + this.y*this.y)
}
this.equal = function(other_p) {
return this.x == other_p.x && this.y == other_p.y;
}
}
// Сегмент (отрезок)
var Segment = function(b={"x": 0, "y": 0}, e={"x": 0, "y": 0}) {
this.b = b;
this.e = e;
this.equal = function(other) {
return (b == other.b && e == other.e) ||
(b == other.e && e == other.b);
}
}
// Полигон - многоугольник
function compareNumbers(a, b) {
return a - b;
}
function XOR(a,b) {
return ( a || b ) && !( a && b );
}
function compareVecs(a, b) {
return Math.atan2(a[1], a[0]) - Math.atan2(b[1], b[0])
}
var Polygon = function(n=10, c_x=100, c_y=100, size_x = 100, size_y = 100) {
this.n = n;
this.x = c_x;
this.y = c_y;
this.size_x = 100;
this.size_y = 100;
var polygon = []
var x = [];
var y = [];
for (var i=0; i < n; i++) {
x.push(Math.floor( Math.random() * size_x ) + c_x - size_x / 2)
y.push(Math.floor( Math.random() * size_y ) + c_y - size_y / 2)
}
x.sort(compareNumbers)
y.sort(compareNumbers)
var min_x = x.shift()
var min_y = y.shift()
var max_x = x.pop()
var max_y = y.pop()
var x_chain_1 = [min_x]
var y_chain_1 = [min_y]
var x_chain_2 = [min_x]
var y_chain_2 = [min_y]
for (var i=0; i < x.length; i+=2) {
if (Math.random() > 0.5) {
x_chain_1.push(x[i])
x_chain_2.push(x[i+1])
} else {
x_chain_1.push(x[i+1])
x_chain_2.push(x[i])
}
}
for (var i=0; i < y.length; i+=2) {
if (Math.random() > 0.5) {
y_chain_1.push(y[i])
y_chain_2.push(y[i+1])
} else {
y_chain_1.push(y[i+1])
...