CSCI 261 - Programming Concepts (C++)Fall 2016 - Lab 06BQuick Links: Blackboard | EECS | Mines | Piazza | zyBooks |
||||||||||||||||
| Home | Contact | Syllabus | Assignments | Schedule | Resources | | ||||||||||||||||
This lab is due by October 05, 2016 08:00am.
Approximating a Circle
In computer graphics, it is very difficult to draw a true circle. Our screen is made up of pixels which is essentially a large grid. We can therefore only draw straight lines and we cannot truly represent a curve on our screen. Instead of drawing a true circle, we can draw an approximation of a circle by connecting straight lines.
Turtle Graphics
We are going to lay the ground work for a very simple Turtle Graphics program. Turtle Graphics is a simple drawing program often used to teach basic computer programming. Imagine a turtle with a pen attached to his tail. The turtle can only move forward and when he does, a line is drawn behind him. We can send other commands to the turtle to make the turtle turn. If you want to play around with a visual Turtle Graphics program, then here is a link to try online.
We will revisit the graphical portion later on in the course, but for now we are going to print out the Points that the Turtle moves over. We are going to need our Point and Vector structs, plus the functions we wrote in Lab06A. Let's get started. Start off by creating a new struct called Turtle. It should have two data members, (1) a Point for the location and (2) a Vector for the heading. These two data members correspond go the turtle's current location and the direction it is facing. In our main(), create a Turtle variable and set it's location to (1,0) and it's heading to <1, 0>. We now need two functions to allow our Turtle to move. The first will be called MoveTurtleForward and will do just what it says - move our Turtle forward along its heading some distance. This function will accept two parameters - a Turtle variable passed by reference and a double variable passed by value representing the distance to move. The function does not return a value. Inside the function, it will add the turtle's heading (a Vector) scaled by the distance to the turtle's current location (a Point). In vector math, this is represented by
The second function will be called TurnTurtleLeft and will do as it says - rotate the turtle's heading to the left by some angle. This function again takes two parameters - a Turtle variable passed by reference and a double variable passed by value representing the angle to rotate. The function does not return a value. Inside the function, it will rotate the turtle's heading by the angle provided (hmm, don't we have a function that does this already? Let's call it!).
Our Turtle will follow the following pseudocode to draw an approximation for a circle:
If we do have steps left to take, then we'll take the recursive case. The recursive case will do four things in this order:
These values will keep our circle approximation to have a radius close to one.
Congrats on writing your first recursive program! We will see more recursive programs coming up in the near future. And we will pick this lab back up to actually draw the points so we can see where our turtle has moved. Test ValuesHere are some known input/output values that you can test against. Be sure to try your own to verify the code works! After our discussion following the coding quiz, think about the boundary conditions and edge cases that may arise.
Lab Submission
You will submit your solution to this lab with the rest of Week06. Detailed instructions for doing this are posted in Assignment 06.
This lab is due by October 05, 2016 08:00am.
| ||||||||||||||||
Last Updated: 01/01/70 00:00
|