Code Sketch


shree bodare
By: Mhalsakant School
Category: Art
clear()
invisible()

// 1. Define 8 vertices of a 3D Box (X, Y, Z)
val vertices = Array(
  (-60.0, -60.0, -60.0), ( 60.0, -60.0, -60.0), ( 60.0,  60.0, -60.0), (-60.0,  60.0, -60.0),
  (-60.0, -60.0,  60.0), ( 60.0, -60.0,  60.0), ( 60.0,  60.0,  60.0), (-60.0,  60.0,  60.0)
)

// 2. Define the 12 line edges connecting the 8 vertices
val edges = Array(
  (0, 1), (1, 2), (2, 3), (3, 0), // Back face lines
  (4, 5), (5, 6), (6, 7), (7, 4), // Front face lines
  (0, 4), (1, 5), (2, 6), (3, 7)  // Connecting side lines
)

var angle = 0.0

animate {
  clear()
  angle += 0.02

  val cosA = math.cos(angle)
  val sinA = math.sin(angle)

  // 3. Transform 3D coordinates to 2D
  val projected = vertices.map { case (x, y, z) =>
    // Rotate Y
    val x1 = x * cosA + z * sinA
    val z1 = -x * sinA + z * cosA

    // Rotate X
    val y2 = y * cosA - z1 * sinA
    val z2 = y * sinA + z1 * cosA

    // Perspective scale
    val scale = 300.0 / (300.0 + z2 + 100.0)
    (x1 * scale, y2 * scale)
  }

  // 4. Draw wireframe edges using simple jumpTo and moveTo
  setPenColor(cm.blue)
  setPenThickness(2)

  for ((start, end) <- edges) {
    val (x1, y1) = projected(start)
    val (x2, y2) = projected(end)
    
    jumpTo(x1, y1)
    moveTo(x2, y2)
  }
}