try finish and improve overally correct on the fly this code. the purpose is to draw a…
- User
try finish and improve overally correct on the fly this code. the purpose is to draw a matrix by lines with ability to attach and detach lines to construct the model in 3d matrix representation array visually building the model by dragging lines and atach to other lines. output full awesome code. : // Define canvas and context const canvas = document.createElement(‘canvas’); canvas.width = window.innerWidth; canvas.height = window.innerHeight; document.body.appendChild(canvas);
const ctx = canvas.getContext(‘2d’);
// Define vertices and edges let vertices = [ {x: 0.5, y: 0.5, z: -1, color: “#FF0000”}, {x: -0.3, y: -0.5, z: -1, color: “#00FF00”}, {x: -0.3, y: 0.8, z: -1, color: “#0000FF”}, {x: 0.5, y: -0.5, z: -1, color: “#FFFF00”}, {x: -0.5, y: -0.5, z: -1, color: “#FF00FF”}, {x: 0.5, y: 0.5, z: 1, color: “#00FFFF”}, {x: -0.3, y: -0.5, z: 1, color: “#000000”}, {x: -0.3, y: 0.8, z: 1, color: “#FFFFFF”}, ];
let edges = [ [0, 1], // Connect vertex 0 to 1 [0, 2], // Connect vertex 0 to 2 [1, 2], // Connect vertex 1 to 2 [3, 4], // Connect vertex 3 to 4 [0, 3], // Connect vertex 0 to 3 [2, 5], // Connect vertex 2 to 5 [1, 6], // Connect vertex 1 to 6 [2, 7], // Connect vertex 2 to 7 [4, 5], // Connect vertex 4 to 5 [3, 7], // Connect vertex 3 to 7 [4, 6], // Connect vertex 4 to 6 [5, 7], // Connect vertex 5 to 7 ];
// Define initial rotation and scale values let angleX = 0; let angleY = 0; let angleZ = 0; let scale = 0.025; let zoom = 1;
// Define drag and drop variables let indexToDrag = -1; let lineToAttach = -1;
// Draw the matrix function render() { // Clear screen ctx.fillStyle = “#FFF”; ctx.fillRect(0, 0, canvas.width, canvas.height);
// Calculate new rotated vertices const rotX = rotateX(angleX); const rotY = rotateY(angleY); const rotZ = rotateZ(angleZ);
let rotatedVertices = vertices.map((vertex) => { const cx = vertex.x; const cy = vertex.y; const cz = vertex.z;
const rotated = transform(transform(transform([cx, cy, cz], rotX), rotY), rotZ); return { x: rotated[0], y: rotated[1], z: rotated[2], color: vertex.color }; });
// Draw lines between vertices ctx.strokeStyle = “black”; ctx.beginPath(); edges.forEach((edge) => { const [v1, v2] = edge;
const [screenX1, screenY1] = project(rotatedVertices[v1].x, rotatedVertices[v1].y, rotatedVertices[v1].z); const [screenX2, screenY2] = project(rotatedVertices[v2].x, rotatedVertices[v2].y, rotatedVertices[v2].z);
ctx.moveTo(screenX1, screenY1); ctx.lineTo(screenX2, screenY2); }); ctx.stroke();
// Draw circles on vertices ctx.beginPath(); rotatedVertices.forEach((vertex) => { ctx.fillStyle = vertex.color; drawCircle(ctx, project(vertex.x, vertex.y, vertex.z), 5); ctx.fill(); });
requestAnimationFrame(render); }
// Event listeners for drag and drop canvas.addEventListener(“mousedown”, hover); canvas.addEventListener(“mousemove”, drag); canvas.addEventListener(“mouseup”, release);
- Assistant
angle); const s = Math.sin(angle);
return [ [c, -s, 0], [s, c, 0], [0, 0, 1], ]; }
function transform([x, y, z], mat) { return [ x * mat[0][0] + y * mat[1][0] + z * mat[2][0], x * mat[0][1] + y * mat[1][1] + z * mat[2][1], x * mat[0][2] + y * mat[1][2] + z * mat[2][2], ]; }
// Start rendering render();
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.