Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local interface = peripheral.wrap("back")
- local owner = interface.getMetaOwner()
- local canvas = interface.canvas3d()
- -- Define tesseract vertices (a 4D hypercube with coordinates ±1)
- local vertices = {}
- local s = 1 -- half-side length
- for i = -1, 1, 2 do
- for j = -1, 1, 2 do
- for k = -1, 1, 2 do
- for l = -1, 1, 2 do
- table.insert(vertices, {x = i * s, y = j * s, z = k * s, w = l * s})
- end
- end
- end
- end
- -- Function to rotate a 4D point around a specified plane.
- -- Available planes: "xy", "xz", "xw", "yz", "yw", "zw"
- function rotate4D(point, angle, plane)
- local cosA = math.cos(angle)
- local sinA = math.sin(angle)
- -- Copy the point so we don't modify the original
- local p = {x = point.x, y = point.y, z = point.z, w = point.w}
- if plane == "xy" then
- p.x = point.x * cosA - point.y * sinA
- p.y = point.x * sinA + point.y * cosA
- elseif plane == "xz" then
- p.x = point.x * cosA - point.z * sinA
- p.z = point.x * sinA + point.z * cosA
- elseif plane == "xw" then
- p.x = point.x * cosA - point.w * sinA
- p.w = point.x * sinA + point.w * cosA
- elseif plane == "yz" then
- p.y = point.y * cosA - point.z * sinA
- p.z = point.y * sinA + point.z * cosA
- elseif plane == "yw" then
- p.y = point.y * cosA - point.w * sinA
- p.w = point.y * sinA + point.w * cosA
- elseif plane == "zw" then
- p.z = point.z * cosA - point.w * sinA
- p.w = point.z * sinA + point.w * cosA
- end
- return p
- end
- -- Function to project a 4D point to 3D using perspective projection.
- -- d is the distance from the "camera" to the 4D viewer.
- function project4Dto3D(point, d)
- -- Calculate the perspective factor.
- local factor = d / (d - point.w)
- return {
- x = point.x * factor,
- y = point.y * factor,
- z = point.z * factor
- }
- end
- -- Assuming `canvas` object and `canvas.create` is available.
- -- Settings for rotation and projection
- local angle = math.rad(45) -- 45° rotation (example)
- local plane = "xw" -- Rotating in the xw plane (feel free to experiment!)
- local d = 5 -- Distance for the 4D->3D perspective projection
- -- Define edges of the tesseract (each edge connects two vertices)
- local edges = {
- {1, 2}, {1, 3}, {1, 4}, {1, 5}, {2, 6}, {2, 7}, {3, 6}, {3, 8}, {4, 5}, {4, 8}, {5, 6}, {6, 8}, {7, 8}
- }
- -- Process each vertex: rotate, project, then translate
- local function processVertices()
- local projectedVertices = {}
- for i, vertex in ipairs(vertices) do
- local rotated = rotate4D(vertex, angle, plane)
- local projected = project4Dto3D(rotated, d)
- -- Translate so that the 3D center is at (3143, 72, -6823)
- projected.x = projected.x + 3143
- projected.y = projected.y + 72
- projected.z = projected.z - 6823
- projectedVertices[i] = projected
- end
- return projectedVertices
- end
- -- Main loop to continuously draw the tesseract
- while true do
- -- Clear the canvas before drawing new frame
- canvas.clear()
- local c = canvas.create({-owner.x, -owner.y, -owner.z}) -- Create a new canvas object
- -- Process the vertices and get the projected 3D points
- local projectedVertices = processVertices()
- -- Draw the edges on the canvas
- for _, edge in ipairs(edges) do
- local startIdx = edge[1]
- local endIdx = edge[2]
- local startVertex = projectedVertices[startIdx]
- local endVertex = projectedVertices[endIdx]
- -- Draw a line between the two 3D points (start to end)
- c.addLine(
- {startVertex.x, startVertex.y, startVertex.z},
- {endVertex.x, endVertex.y, endVertex.z},
- 2, -- Line width
- 0xFFFFFFFF -- Color (white)
- )
- end
- -- Sleep for a bit before next update (this controls the animation speed)
- sleep(0.05) -- Adjust this value for faster/slower updates
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement