Advertisement
xX-AAAAAAAAAA-Xx

Thing (Test)

Feb 22nd, 2025 (edited)
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.79 KB | None | 0 0
  1. local interface = peripheral.wrap("back")
  2.  
  3. local owner = interface.getMetaOwner()
  4. local canvas = interface.canvas3d()
  5.  
  6.  
  7. -- Define tesseract vertices (a 4D hypercube with coordinates ±1)
  8. local vertices = {}
  9. local s = 1  -- half-side length
  10. for i = -1, 1, 2 do
  11.   for j = -1, 1, 2 do
  12.     for k = -1, 1, 2 do
  13.       for l = -1, 1, 2 do
  14.         table.insert(vertices, {x = i * s, y = j * s, z = k * s, w = l * s})
  15.       end
  16.     end
  17.   end
  18. end
  19.  
  20. -- Function to rotate a 4D point around a specified plane.
  21. -- Available planes: "xy", "xz", "xw", "yz", "yw", "zw"
  22. function rotate4D(point, angle, plane)
  23.   local cosA = math.cos(angle)
  24.   local sinA = math.sin(angle)
  25.   -- Copy the point so we don't modify the original
  26.   local p = {x = point.x, y = point.y, z = point.z, w = point.w}
  27.  
  28.   if plane == "xy" then
  29.     p.x = point.x * cosA - point.y * sinA
  30.     p.y = point.x * sinA + point.y * cosA
  31.   elseif plane == "xz" then
  32.     p.x = point.x * cosA - point.z * sinA
  33.     p.z = point.x * sinA + point.z * cosA
  34.   elseif plane == "xw" then
  35.     p.x = point.x * cosA - point.w * sinA
  36.     p.w = point.x * sinA + point.w * cosA
  37.   elseif plane == "yz" then
  38.     p.y = point.y * cosA - point.z * sinA
  39.     p.z = point.y * sinA + point.z * cosA
  40.   elseif plane == "yw" then
  41.     p.y = point.y * cosA - point.w * sinA
  42.     p.w = point.y * sinA + point.w * cosA
  43.   elseif plane == "zw" then
  44.     p.z = point.z * cosA - point.w * sinA
  45.     p.w = point.z * sinA + point.w * cosA
  46.   end
  47.  
  48.   return p
  49. end
  50.  
  51. -- Function to project a 4D point to 3D using perspective projection.
  52. -- d is the distance from the "camera" to the 4D viewer.
  53. function project4Dto3D(point, d)
  54.   -- Calculate the perspective factor.
  55.   local factor = d / (d - point.w)
  56.   return {
  57.     x = point.x * factor,
  58.     y = point.y * factor,
  59.     z = point.z * factor
  60.   }
  61. end
  62.  
  63. -- Assuming `canvas` object and `canvas.create` is available.
  64.  
  65.  
  66. -- Settings for rotation and projection
  67. local angle = math.rad(45)  -- 45° rotation (example)
  68. local plane = "xw"          -- Rotating in the xw plane (feel free to experiment!)
  69. local d = 5                 -- Distance for the 4D->3D perspective projection
  70.  
  71. -- Define edges of the tesseract (each edge connects two vertices)
  72. local edges = {
  73.   {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}
  74. }
  75.  
  76. -- Process each vertex: rotate, project, then translate
  77. local function processVertices()
  78.   local projectedVertices = {}
  79.   for i, vertex in ipairs(vertices) do
  80.     local rotated = rotate4D(vertex, angle, plane)
  81.     local projected = project4Dto3D(rotated, d)
  82.     -- Translate so that the 3D center is at (3143, 72, -6823)
  83.     projected.x = projected.x + 3143
  84.     projected.y = projected.y + 72
  85.     projected.z = projected.z - 6823
  86.     projectedVertices[i] = projected
  87.   end
  88.   return projectedVertices
  89. end
  90.  
  91. -- Main loop to continuously draw the tesseract
  92. while true do
  93.   -- Clear the canvas before drawing new frame
  94.  
  95.   canvas.clear()
  96.   local c = canvas.create({-owner.x, -owner.y, -owner.z})  -- Create a new canvas object
  97.  
  98.   -- Process the vertices and get the projected 3D points
  99.   local projectedVertices = processVertices()
  100.  
  101.   -- Draw the edges on the canvas
  102.   for _, edge in ipairs(edges) do
  103.     local startIdx = edge[1]
  104.     local endIdx = edge[2]
  105.     local startVertex = projectedVertices[startIdx]
  106.     local endVertex = projectedVertices[endIdx]
  107.  
  108.     -- Draw a line between the two 3D points (start to end)
  109.     c.addLine(
  110.       {startVertex.x, startVertex.y, startVertex.z},
  111.       {endVertex.x, endVertex.y, endVertex.z},
  112.       2,  -- Line width
  113.       0xFFFFFFFF  -- Color (white)
  114.     )
  115.   end
  116.  
  117.   -- Sleep for a bit before next update (this controls the animation speed)
  118.   sleep(0.05)  -- Adjust this value for faster/slower updates
  119. end
  120.  
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement