Death Star
In this lesson, we'll combine geometry and Python knowledge to create perfect circles and spheres.
Circle Algorithm
The standard equation of a circle centered at (0, 0) is x*x + y*y = r*r
, where r
is the radius.
In a 2D coordinate grid, any point (x,y)
inside a flat circle of radius r
should satisfy the condition (x*x + y*y) <= r*r
. We can use this condition to limit the locations of the blocks, and build a flat "circle" in CodeCraft world.
# small circle, blue, radius 10
for i in range(-10, 11):
for j in range(-10, 11):
if i*i+j*j <= 100:
block(i, j+20, -20, 'linen_blue')
16-07 Big Circle
If you increase the radius, the shape will get closer to a perfect circle. Try building a large circle that has radius of at least 20 like this lime green one:
16-08 A Circle Function
Can you define a function that builds circles?
Create a function circle(x,y,z,r,m)
where r
is the radius, m
is the material, and x,y,z
is the origin (center) of the circle.
Call the function to build a few circles like in the picture.
Sphere algorithm
The points (x, y, z)
inside a sphere should satisfy (x*x + y*y + z*z) <= r*r
. The equation looks similar to a circle, but in three dimensions instead of two.
16-09 Sphere Function
Create a function sphere(x,y,z,m)
, and generate a sphere with radius at least 10.
A death star
sphere(0, 20, -10, 'stonebrick_carved')
A burning sun
sphere(30, 20, -10, 'redstone')