Used pre-existing Graham Scan library to make custom configurations look nicer
This commit is contained in:
24
script.js
24
script.js
@ -2,6 +2,8 @@
|
||||
* BEGIN CLASS DECLARATIONS
|
||||
*/
|
||||
|
||||
import GrahamScan from "./vendor/lucio/graham-scan.mjs";
|
||||
|
||||
// 2D vector class to make some of the math easier
|
||||
class Vec2D {
|
||||
constructor(x, y) {
|
||||
@ -489,6 +491,7 @@ function drawModule(ctx, module) {
|
||||
ctx.restore();
|
||||
}
|
||||
|
||||
|
||||
function drawRobot(ctx, robot, heading) {
|
||||
ctx.save(); // Save current state before rotation
|
||||
|
||||
@ -498,18 +501,29 @@ function drawRobot(ctx, robot, heading) {
|
||||
ctx.fillStyle = rootStyles.getPropertyValue('--robot-fill-color');
|
||||
ctx.lineWidth = 4;
|
||||
|
||||
const modules = robot.modules.sort((a, b) => Math.atan2(a.position.y, a.position.x) - Math.atan2(b.position.y, b.position.x));
|
||||
let hull = [];
|
||||
// Get the convex hull of the robot if there are more than 3 modules
|
||||
if (robot.modules.length > 3) {
|
||||
const grahamScan = new GrahamScan();
|
||||
grahamScan.setPoints(robot.modules.map((module) => [module.position.x, module.position.y]));
|
||||
hull = grahamScan.getHull();
|
||||
} else {
|
||||
hull = robot.modules.map((module) => [module.position.x, module.position.y]);
|
||||
}
|
||||
|
||||
|
||||
// Draw the convex hull as the robot frame
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(modules[0].position.x, modules[0].position.y);
|
||||
for (let i = 1; i < modules.length; i++) {
|
||||
ctx.lineTo(modules[i].position.x, modules[i].position.y);
|
||||
ctx.moveTo(hull[0][0], hull[0][1]);
|
||||
for (let i = 1; i < hull.length; i++) {
|
||||
ctx.lineTo(hull[i][0], hull[i][1]);
|
||||
}
|
||||
ctx.closePath();
|
||||
ctx.fill();
|
||||
ctx.stroke();
|
||||
|
||||
modules.forEach(module => drawModule(ctx, module));
|
||||
// Draw all modules (not just hull modules)
|
||||
robot.modules.forEach(module => drawModule(ctx, module, heading));
|
||||
|
||||
ctx.restore(); // Restore to remove rotation
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user