prosperon/scripts/geometry.js

71 lines
1.7 KiB
JavaScript
Raw Normal View History

2024-02-25 17:31:48 -06:00
var shape = {};
2024-06-07 00:39:37 -05:00
shape.box = {};
2024-09-26 11:36:09 -05:00
shape.box.points = function (ll, ur) {
return [ll, ll.add([ur.x - ll.x, 0]), ur, ll.add([0, ur.y - ll.y])];
};
2024-02-25 17:31:48 -06:00
shape.sphere = {};
shape.circle = {};
2024-09-26 11:36:09 -05:00
shape.sphere.volume = function (r) {
return (Math.pi * r * r * r * 4) / 3;
};
shape.sphere.random = function (r, theta = [0, 1], phi = [-0.5, 0.5]) {
if (typeof r === "number") r = [r, r];
if (typeof theta === "number") theta = [theta, theta];
if (typeof phi === "number") phi = [phi, phi];
var ra = Math.random_range(r[0], r[1]);
var ta = Math.turn2rad(Math.random_range(theta[0], theta[1]));
var pa = Math.turn2rad(Math.random_range(phi[0], phi[1]));
return [ra * Math.sin(ta) * Math.cos(pa), ra * Math.sin(ta) * Math.sin(pa), ra * Math.cos(ta)];
};
2024-01-01 17:30:42 -06:00
2024-09-26 11:36:09 -05:00
shape.circle.area = function (r) {
return Math.pi * r * r;
};
shape.circle.random = function (r, theta) {
return shape.sphere.random(r, theta).xz;
};
2024-02-25 17:31:48 -06:00
2024-09-26 11:36:09 -05:00
shape.box = function (w, h) {
2024-02-25 17:31:48 -06:00
w /= 2;
h /= 2;
var points = [
2024-09-26 11:36:09 -05:00
[w, h],
[-w, h],
[-w, -h],
[w, -h],
2024-02-25 17:31:48 -06:00
];
return points;
};
2024-09-26 11:36:09 -05:00
shape.ngon = function (radius, n) {
return shape.arc(radius, 360, n);
2024-02-25 17:31:48 -06:00
};
2024-09-26 11:36:09 -05:00
shape.arc = function (radius, angle, n, start = 0) {
2024-02-25 17:31:48 -06:00
start = Math.deg2rad(start);
2024-09-26 11:36:09 -05:00
if (angle >= 360) angle = 360;
2024-02-25 17:31:48 -06:00
if (n <= 1) return [];
var points = [];
angle = Math.deg2rad(angle);
2024-09-26 11:36:09 -05:00
var arclen = angle / n;
for (var i = 0; i < n; i++) points.push(Vector.rotate([radius, 0], start + arclen * i));
2024-02-25 17:31:48 -06:00
return points;
};
2024-09-26 11:36:09 -05:00
shape.circle.points = function (radius, n) {
2024-02-25 17:31:48 -06:00
if (n <= 1) return [];
return shape.arc(radius, 360, n);
};
2024-09-26 11:36:09 -05:00
shape.corners2points = function (ll, ur) {
return [ll, ll.add([ur.x, 0]), ur, ll.add([0, ur.y])];
};
2024-08-28 16:38:31 -05:00
2024-09-26 11:36:09 -05:00
return { shape };