How to Create a Sphere with Pen

Материал из Поле цифровой дидактики

This article explains how to create a sphere with pen in Scratch.

The Process

Method One: Using a Light Source

Step 1: Create a sprite which will be the "Light Source", and create the pen sprite.

Step 2: Create the following code in the pen sprite:

when gf clicked
generate sphere of size (100)

define generate sphere of size (sphere size)
set [v v] to [0]
repeat (sphere size)
  create circle of size (v)  // Remember to check the "run without screen refresh"!
  change [v v] by (0.01)
end

define create circle of size (circle size)
repeat (360)
  pen down
  set pen color to (5) // The color of the sphere
  set pen (brightness v) to ((100)-(distance to [Light Source v]))
  turn right (1) degrees
  move (circle size) steps
end

Method Two: Pen Sprite Only

Step 1: Create the pen sprite.

Step 2: Do the same as Step 2, method 1, but replace the last script with this one:

define create circle of size (circle size)
repeat (360)
  move (circle size) steps
  pen down
  set pen color to (5) // reddish orange
  set pen (brightness v) to ([sqrt v] of ((((center x) - (x position)) * ((center x) - (x position))) + (((center y) - (y position)) * ((center y) - (y position))))) // Remember, (center x) is the x position of where it will be shaded, and (center y) is the y position of where it will be shaded.
  turn right (1) degrees
end

Method 3: Wireframe

Another method for a wireframe 3d sphere is shown below:

define go to (x :: custom-arg) (y :: custom-arg) (z :: custom-arg) (rc_x :: custom-arg) (rc_y :: custom-arg) (rc_z :: custom-arg) (r_x :: custom-arg) (r_y :: custom-arg) (r_z :: custom-arg) (cam_x :: custom-arg) (cam_y :: custom-arg) (cam_z :: custom-arg) (fs :: custom-arg)
go to x: ((((((((x :: custom-arg) - (rc_x :: custom-arg)) * ([cos v] of (r_y :: custom-arg))) - (((z :: custom-arg) - (rc_z :: custom-arg)) * ([sin v] of (r_y :: custom-arg)))) * ([cos v] of (r_z :: custom-arg))) - (((((y :: custom-arg) - (rc_y :: custom-arg)) * ([cos v] of (r_x :: custom-arg))) - (((((x :: custom-arg) - (rc_x :: custom-arg)) * ([sin v] of (r_y :: custom-arg))) + (((z :: custom-arg) - (rc_z :: custom-arg)) * ([cos v] of (r_y :: custom-arg)))) * ([sin v] of (r_x :: custom-arg)))) * ([sin v] of (r_z :: custom-arg)))) - (cam_x :: custom-arg)) * ((fs :: custom-arg) / ((fs :: custom-arg) + (((((y :: custom-arg) - (rc_y :: custom-arg)) * ([sin v] of (r_x :: custom-arg))) + (((((x :: custom-arg) - (rc_x :: custom-arg)) * ([sin v] of (r_y :: custom-arg))) + (((z :: custom-arg) - (rc_z :: custom-arg)) * ([cos v] of (r_y :: custom-arg)))) * ([cos v] of (r_x :: custom-arg)))) - (cam_z :: custom-arg))))) y: ((((((((x :: custom-arg) - (rc_x :: custom-arg)) * ([cos v] of (r_y :: custom-arg))) - (((z :: custom-arg) - (rc_z :: custom-arg)) * ([sin v] of (r_y :: custom-arg)))) * ([sin v] of (r_z :: custom-arg))) + (((((y :: custom-arg) - (rc_y :: custom-arg)) * ([cos v] of (r_x :: custom-arg))) - (((((x :: custom-arg) - (rc_x :: custom-arg)) * ([sin v] of (r_y :: custom-arg))) + (((z :: custom-arg) - (rc_z :: custom-arg)) * ([cos v] of (r_y :: custom-arg)))) * ([sin v] of (r_x :: custom-arg)))) * ([cos v] of (r_z :: custom-arg)))) - (cam_y :: custom-arg)) * ((fs :: custom-arg) / ((fs :: custom-arg) + (((((y :: custom-arg) - (rc_y :: custom-arg)) * ([sin v] of (r_x :: custom-arg))) + (((((x :: custom-arg) - (rc_x :: custom-arg)) * ([sin v] of (r_y :: custom-arg))) + (((z :: custom-arg) - (rc_z :: custom-arg)) * ([cos v] of (r_y :: custom-arg)))) * ([cos v] of (r_x :: custom-arg)))) - (cam_z :: custom-arg)))))

define Loop
erase all
set [i v] to [0]
repeat (12)
  set [j v] to [90]
  repeat (6)
    Line ((([cos v] of (i)) * (100)) * ([cos v] of (j))) (([sin v] of (j)) * (100)) (((([sin v] of (i)) * (100)) * ([cos v] of (j))) + (5000)) ((([cos v] of (i)) * (100)) * ([cos v] of ((j) + (30)))) (([sin v] of ((j) + (30))) * (100)) (((([sin v] of (i)) * (100)) * ([cos v] of ((j) + (30)))) + (5000)) :: custom
    change [j v] by (30)
  end
  change [i v] by (30)
end
set [j v] to [120]
repeat (5)
  set [i v] to [0]
  repeat (12)
    Line ((([cos v] of (i)) * (100)) * ([cos v] of (j))) (([sin v] of (j)) * (100)) (((([sin v] of (i)) * (100)) * ([cos v] of (j))) + (5000)) ((([cos v] of ((i) + (30))) * (100)) * ([cos v] of (j))) (([sin v] of (j)) * (100)) (((([sin v] of ((i) + (30))) * (100)) * ([cos v] of (j))) + (5000)) :: custom
    change [i v] by (30)
  end
  change [j v] by (30)
end

define Line (X1 :: custom-arg) (Y1 :: custom-arg) (Z1 :: custom-arg) (X2 :: custom-arg) (Y2 :: custom-arg) (Z2 :: custom-arg)
go to (X1 :: custom-arg) (Y1 :: custom-arg) (Z1 :: custom-arg) :: custom
pen down
go to (X2 :: custom-arg) (Y2 :: custom-arg) (Z2 :: custom-arg) :: custom
pen up

when green flag clicked
set pen color to [#632d99]
set pen size to (5)
forever
  Loop :: custom
end

define go to (x :: custom-arg) (y :: custom-arg) (z :: custom-arg)
go to (x :: custom-arg) (y :: custom-arg) (z :: custom-arg) (0) (0) (5000) (mouse y) ((mouse x) / (3)) (0) (0) (0) (0) (240) :: custom

Examples

See Also