hw3-turtle.h,
hw3-turtle.c,
hw3-poly.c
.
These programs are to be electronically submitted by using the submit program on csa. The code you submit should conform with the program assignment guidelines.
"Turtle graphics" refers to graphics libraries which can be imagined as producing graphics by giving instruction to a turtle (or a remotely controlled car) which tell it what direction to go and how far. As the turtle moves, it produces the graphical image. Thus a square would be formed by issuing commands to point north, move forward 2 inches, turn right (90 degrees), move forward 2 inches, turn right, move forward 2 inches, turn right, move forward 2 inches.
Certain images are much easier to describe this way. For example, how does a turtle trace out a regular polygon with N sides? (A regular polygon is a plane figure with all sides the same length and all angles the same, squares and stop signs--which are regular octagons--are examples.)
Using the graphics.h graphics package for your primitives,
implement a turtle graphics package which includes the following
functions:
InitTurtle();
PlaceTurtle(xpos, ypos);
MoveTurtle(length);
DrawTurtle(length);
TurnLeft(angle);
TurnRight(angle);
ArcLeft(radius, angle);
ArcRight(radius, angle);
width = GetWindowWidth();
height = GetWindowHeight();
x = GetCurrentX();
y = GetCurrentY();
a = GetCurrentDirection();
Important Notes:
Most of these functions will be fairly straightforward to code. In fact, four of them are already done (why?). Nevertheless, a few hints are in order.
DrawTurtle()
(which draws a straight line as the turtle "walks forward")
and MoveTurtle()
(which moves the turtle to the same place as the previous function, but
without drawing a line) you will need to know
how much the turtle moves in the x (horizontal) and y (vertical) directions.
This can be determined using a little trigonometry. See the picture below.
The formulas indicated by this picture are good for all angles, not just
angles between 0 and 90 degrees. In order to use the cosine and sine
functions, you will need to include math.h. (where?)
The functions in math.h
use radians, not degrees, so to get the sine and cosine of an angle of
N degrees, you must use
sin(N*PI/180.0),
cos(N*PI/180.0).
Also note, that
this assumes that 0 degrees points directly right,
90 degrees straight up, 180 degrees left, 270 degrees down,
as is standard in mathematics,
but not on the compass, where north is 0 degrees.
ArcLeft() and ArcRight() make a turn of
angle degrees while tracing out a portion of a circle
with radius radius.
You need to think about
how to translate what you want into DrawArc() from
graphics.h. For this it is handy to know that the direction
the turtle is going as it enters the arc will be 90 degrees different from
the direction of the line segment running from the center of the circle to the
point where the arc begins. The same is true as the turtle leaves the
arc. This fact can be used to compute the start and sweep values for
drawing the arc. It also implies that the turtle's angle changes by
the same amount as the arc swept out. Again see the picture below:
Be careful to put the correct things in
hw3-turtle.h and hw3-turtle.c,
especially comments.
Files: hw3-turtle.h, hw3-turtle.c
hw3-draw.
To do this use the
Makefile
for this assignment and type make hw3-draw. This will
link your turtle.o with hw3-draw.o and should
produce
the program hw3-draw for you to run. (This is one way we will test your
turtle graphics package, by linking it with other programs which make
use of turtle graphics.)
Run the program. It will allow you to test your turtle graphics package.
Files: None.
To locate the polygon roughly in the center of the screen, use the fact
that the distance from the center of the screen to a corner of the polygon
is approximately numSides * lengthOfSide / 6.
(I had 3 here before. That was an error, I was working with the
diamter instead of the radius, sorry.)
(One extra point if you can justify this approximation in your comments.
Two extra points for working out the value exactly and explaining why
your method works.)
You should only use graphics functions from your turtle graphics package and not from graphics.h directly. You may, of course, write new functions for this program, but DO NOT ADD ANY ADDITIONAL FUNCTIONS TO THE TURTLE PACKAGE.
Files:hw3-poly.c
Makefile.
Makefile.a3,
you will have to use the mv (short for move)
command to rename it by typing
mv Makefile.a3 Makefile
cp command to copy it by typing
cp Makefile.a3 Makefile
make -k. (The -k means "keep going", which instructs
make to build as much as it can, rather than quit after the first error.)
Do not allow your work to be used by others:
Warning: If someone cheats by using your work, you will also be penalized.