Project Tips and Tricks
Introduction
In helping students with their projects, we've developed a number
of techniques that other students may find useful. This document
gathers some of those techniques.
Quick Tips
Dealing with Too-Small Selections
As you may have noted, image-select-rectangle! and
image-select-ellipse! will not work unless the width
and height of the area selected are at least 1.
> (image-select-rectangle! 80 REPLACE 100 200 0.5 0.5)
image-select-rectangle!: width and height must be at least 1
When writing recursive procedures that select areas, you may end up with
a width or height that is too small. You have two obvious solutions:
(1) You can avoid the selection (and corresponding actions) altogether
or (2) You can ensure that any selection you do has a width and height
of at least one.
You can use when to ensure that you only call the
selection procedure (and anything else) when
width and
height are at least 1. For example,
(when (and (>= width 1.0) (>= height 1.0))
(image-select-rectangle! canvas REPLACE left top width height)
(image-fill! canvas)
When we simply need to use 1 when width or
height is smllaer than 1, we can use the old
trick of using max.
(image-select-rectangle! canvas REPLACE left top (max width 1) (max height 1))
(image-fill! canvas)
Dealing with Invalid Selections
Right now, if you try to stroke or fill something, and there's nothing
selected on the image, you get an error message or inappropriate
behavior.
> (image-select-nothing! canvas)
> (image-stroke! canvas)
gimp-edit-stroke: failed to execute
What's the solution?
You can check whether anything is selected with the
image-has-selection? procedure.
(when (image-has-selection? canvas)
(image-stroke! canvas)
Filling Selections
The undocumented selection-compute-pixels!
procedure (which takes the same parameters as
image-compute-pixels!) provides a handy way
to build a color blend in a small section of an image.
> (image-select-ellipse! canvas REPLACE 100 30 150 40)
> (selection-compute-pixels! canvas
(lambda (col row)
(rgb-new col row col)))
Selecting Regular Polygons
We've found that it is quite easy to draw an N-sided regular polygon
with a turtle. We simply repeat the steps of moving forward a
particular amount and turning by 360/N. But turtles only draw
the outline of a polygon. What if we want to select the polygon?
We start by finding a list of the vertices in the polygon. We then
use (image-select-polygon!
image
selection-type) to select them. Once the
polygon is selected, we can do what we want with it.
But how do we get the list of vertices? We can calculate the vertices.
We can also use turtles to draw the polygon, and ask them to report
back on their positions.
Calculating the Vertices in a Polygon
One technique is to compute the positions of the vertices of the polygon.
It is easiest to compute those positions from the center of the polygon.
In that case, we can just think of the angle to each vertex, use
sine and cosine to break the angle into horizontal and vertical
components, and scale and shift appropriately. But the angles should
be obvious. For example, for a hexagon they are pi*1/3, pi*2/3, pi,
pi*4/3, and pi*5/3.
That may be a bit abstract, so here's a procedure to make a list of
the positions in a hexagon. You can think about how to generalize it
for other regular shapes.
It's fairly straightforward to use the procedure.