racket/collects/teachpack/htdp/Docs/arrow.thtml

72 lines
2.1 KiB
Plaintext

{ (define LIBNAME "Arrows")
(include "head.tinc") }
<p>A shape is a class of data for which <code>move</code> and
<code>draw</code> operations can be drawn. </p>
<p>The teachpack <code>arrow.ss</code> implements controller for moving
shapes across a canvass. It provides three operations:
<menu>
<li> <code>{(idx control-left-right)} : shape number move draw -> true </code>:
<br> It consumes a shape, a number, a <code>move</code> function and a
<code>draw</code> function. The
move function consumes a number and a shape and re-draws the shape on
some canvas and produces a shape that is translated by N pixels left or
right.
<br><br>
<li> <code>{(idx control-up-down)} : shape number move -> true</code>:
<br> It is like <code>control-left-right</code> but controls movements by
N pixels up or down.
<br><br>
<li> <code>{(idx control)} : shape number move-lr move-ud -> true</code>:
<br> It consumes a shape, a number, two <code>move</code> functions, and a
draw function. The <code>move</code> functions consume a number and a
shape and re-draw the shape on some canvas and produces a shape that is
translated by N pixels left or right and up or down, respectively.
</menu>
<p>Example:
<pre>
;; A shape is a structure:
;; (make-posn num num)
;; RAD : the radius of the simple disk moving across a canvas
(define RAD 10)
;; move : number shape -> shape or false
;; to move a shape by delta according to translate
;; effect: to redraw it
(define (move delta sh)
(cond
[(and (clear-solid-disk sh RAD)
(draw-solid-disk (translate sh delta) RAD))
(translate sh delta)]
[else false]))
;; translate : shape number -> shape
;; to translate a shape by delta in the x direction
(define (translate sh delta)
(make-posn (+ (posn-x sh) delta) (posn-y sh)))
;; draw-it : shape -> true
;; to draw a shape on the canvas: a disk with radius
(define (draw-it sh)
(draw-solid-disk sh RAD))
;; TESTS:
;; this creates the canvas
(start 100 50)
;; this creates the controller GUI
(control-left-right (make-posn 10 20) 10 move draw-it)
</pre>
{(include "foot.tinc")}