Code Sketch
yadnesh shahare use of the ai
Category: Programming
// Start writing your Kojo code here
cleari()
setBackground(Color(10, 10, 30)) // ????????? ??? ??????????
// 3D Vector Representation
case class Vector3D(x: Double, y: Double, z: Double)
// Viewport and Geometry Configurations
val viewAngleHalf = Math.toRadians(90.0 / 2.0)
val size = canvasBounds.width * 0.5
// ???????? ??????: ???, ????? ??????? ???????? (Orbit Radius), ????, ???, ???
case class PlanetInfo(name: String, orbitRadius: Double, radius: Double, color: Color, speed: Double)
val solarSystem = Seq(
PlanetInfo("Surya (Sun)", 0, 42, Color(255, 215, 0), 0.0),
PlanetInfo("Mercury", 85, 7, Color(192, 192, 192), 4.0),
PlanetInfo("Venus", 135, 11, Color(230, 190, 138), 1.6),
PlanetInfo("Earth", 185, 13, Color(100, 149, 237), 1.0),
PlanetInfo("Mars", 235, 9, Color(220, 20, 60), 0.8),
PlanetInfo("Jupiter", 310, 25, Color(218, 165, 32), 0.4),
PlanetInfo("Saturn", 390, 20, Color(244, 164, 96), 0.3),
PlanetInfo("Uranus", 460, 15, Color(72, 209, 204), 0.2),
PlanetInfo("Neptune", 520, 14, Color(30, 144, 255), 0.1)
)
// Transformation Helpers
def rotateAroundX(angle: Double, point: Vector3D): Vector3D =
Vector3D(
point.x,
point.y * Math.cos(angle) - point.z * Math.sin(angle),
point.y * Math.sin(angle) + point.z * Math.cos(angle)
)
def normalisePoint(point: Vector3D): Vector3D =
Vector3D(point.x / size, point.y / size, point.z / size)
def toWorld(point: Vector3D): Vector3D =
Vector3D(point.x * size, point.y * size, point.z * size)
def toPerspective(point: Vector3D): Vector3D =
Vector3D(
point.x / (point.z * Math.tan(viewAngleHalf)),
point.y / (point.z * Math.tan(viewAngleHalf)),
point.z
)
// ???????? ????? (Orbit Line) ?????
def drawOrbit(orbitRadius: Double, tiltAngle: Double): Unit = {
val numSegments = 60
val points = (0 to numSegments).map { i =>
val theta = 2.0 * Math.PI * i / numSegments
val rawPoint = Vector3D(orbitRadius * Math.cos(theta), 0, orbitRadius * Math.sin(theta) + size + 100)
val centered = Vector3D(rawPoint.x, rawPoint.y, rawPoint.z - (size + 100))
val tilted = rotateAroundX(Math.toRadians(tiltAngle), centered)
val final3D = Vector3D(tilted.x, tilted.y, tilted.z + (size + 100))
toWorld(toPerspective(normalisePoint(final3D)))
}
val orbitPic = Picture.fromVertexShape { p =>
p.beginShape()
points.foreach(v => p.vertex(v.x, v.y))
p.endShape()
}
orbitPic.setPenColor(Color(255, 255, 255, 40)) // ????? ????? ????
orbitPic.setPenThickness(1)
draw(orbitPic)
}
// ???? ??? ??????? ??? ??????
def renderPlanet(planet: PlanetInfo, currentAngle: Double, tiltAngle: Double): Unit = {
val orbitalRad = Math.toRadians(currentAngle * planet.speed)
val rawPos = if (planet.orbitRadius == 0) {
Vector3D(0, 0, size + 100)
} else {
val x = planet.orbitRadius * Math.cos(orbitalRad)
val z = planet.orbitRadius * Math.sin(orbitalRad)
Vector3D(x, 0, z + size + 100)
}
val centered = Vector3D(rawPos.x, rawPos.y, rawPos.z - (size + 100))
val tilted = rotateAroundX(Math.toRadians(tiltAngle), centered)
val final3D = Vector3D(tilted.x, tilted.y, tilted.z + (size + 100))
val projected = toWorld(toPerspective(normalisePoint(final3D)))
// ??? ????????? ??????? ????????? ??????? ????
val scaleFactor = size / final3D.z
val apparentRadius = planet.radius * scaleFactor * 1.4
// ?. ???? ?????
val planetPic = Picture.circle(apparentRadius)
planetPic.setFillColor(planet.color)
planetPic.setPosition(projected.x, projected.y)
draw(planetPic)
// ?. ??? ??????? ??? (Saturn's Ring)
if (planet.name == "Saturn") {
val ring = Picture.ellipse(apparentRadius * 2.3, apparentRadius * 0.7)
ring.setPenColor(Color(220, 190, 140))
ring.setPenThickness(2)
ring.setPosition(projected.x, projected.y)
draw(ring)
}
// ?. ??????? ??? (Label)
val namePic = Picture.text(planet.name, 12)
namePic.setPosition(projected.x - (planet.name.length * 3), projected.y - apparentRadius - 15)
draw(namePic)
}
// ????? ??????? ???
animateWithState(0.0) { angle =>
erasePictures()
val viewTilt = 25.0 // ??????????? ??? ????? (Tilt Angle)
// ?. ????? ????????? ????
solarSystem.tail.foreach { p =>
drawOrbit(p.orbitRadius, viewTilt)
}
// ?. ??? ???? ?????????? (Z-axis sorting) ????-???? ??????
val sortedPlanets = solarSystem.map { p =>
val rad = Math.toRadians(angle * p.speed)
val rawPos = if (p.orbitRadius == 0) Vector3D(0, 0, size + 100)
else Vector3D(p.orbitRadius * Math.cos(rad), 0, p.orbitRadius * Math.sin(rad) + size + 100)
val centered = Vector3D(rawPos.x, rawPos.y, rawPos.z - (size + 100))
val tilted = rotateAroundX(Math.toRadians(viewTilt), centered)
val finalZ = tilted.z + (size + 100)
(p, finalZ)
}.sortBy(_._2).reverse
// ?. ???? ????????? ???? ????
sortedPlanets.foreach { case (p, _) =>
renderPlanet(p, angle, viewTilt)
}
angle + 0.5
}