< 8. Recursion |
9. API Reference
Code editor
Keyboard shortcuts:
Ctrl+Enter
orAlt+Enter
: Evaluate codeShift+Ctrl+Enter
orShift+Alt+Enter
: Evaluate code and clear editorCtrl+Up
orAlt+Up
: Recover previously evaluated code blockCtrl+Down
orAlt+Down
: Scroll to next evaluated code blockEsc
: Clear editor
2-D API
Canvas
The canvas uses a Cartesian coordinate system, where a point is defined as (x, y)
. Its size is 360 pixels by 360 pixels.
Increasing x
values go to the right, and increasing y
values go to the top. This is consistent with math standards, and is not the same as the behavior of HTML/JavaScript canvas
.
The origin, (0, 0)
, is at the center.
The bottom-left corner is (-180, -180)
and the top-right corner is (179, 179)
.
Clear
The clear
function fills the entire canvas white.
Click on any code box below to copy the code to the editor and evaluate it.
(clear)
Redraw the axes
After clearing the canvas, the axes will also be gone. Evaluate axes
to redraw the axes.
(axes)
Shapes
Point
(pt x y)
(pt 15 20)
Line
Draws a line from (x1, y1)
to (x2, y2)
.
(line x1 y1 x2 y2)
(line -100 50 180 100)
Rectangle
Draws a rectangle with its bottom-left corner at (x, y)
, with width w
, and height h
.
(rect x y w h)
(rect -75 25 50 75)
Circle
Draws a circle with centered at (x, y)
, with radius r
.
(circle x y r)
(circle 30 30 60)
Ellipse
Draws an ellipse with centered at (x, y)
, with x-radius w
and y-radius h
.
(ellipse x y w h)
(ellipse 30 30 90 45)
Text
Draws the string value of the argument v
with the lower-left corner at (x, y)
.
(text x y v)
(text 30 60 "Hello")
Set font size
Changes the font size to the given value, in pixels.
(set-font-size! 14)
Set font
Changes the font. By default, "serif"
, "sans-serif"
, and "monospace"
are available.
(set-font! "sans-serif")
Color
Fill color
The color that fills the inside of rectangles and circles. A color may be a CSS name, such as "PaleVioletRed"
or a 3-digit or 6-digit hexadecimal value. Setting an invalid color has no effect.
(set-fill! "green")
(set-fill! "PaleVioletRed")
(set-fill! "#1a2a30")
Stroke color
The color of lines and outlines of rectangles and circles. The color may be a value described above. Setting an invalid color has no effect.
(set-stroke! "blue")
Fill shape
Fills the last shape drawn with the current fill color.
(fill)
Loops
The basic looping construct is for-each
. The first argument it expects is a function with n
parameters. Following the function, n
lists of the same length must be provided.
The function will be called as many times as there are elements in a list. In the beginning, the function will be called using the first elements of each list as arguments, then the second elements of the lists, and so on.
;; a function with one parameter (the center is fixed) (define (centered-circle radius) (circle 0 0 radius)) ;; draw three circles of increasing radius (for-each centered-circle '(10 50 150))
;; a function with two parameters (the radius is fixed) (define (medium-circle x y) (circle x y 30)) ;; draw five circles in a diagonal line (for-each medium-circle '(-50 -30 0 30 50) '(-150 -75 0 75 150))
< 8. Recursion |