CS1321 Fall 2002
Xtra Credit

Due before 6 a.m. Monday, Nov. 19, 2001


Instructions:

Note:


Before You Start


 

General Homework Information

For part of this assignment, you will be creating a fractal. Some basic concepts need to be understood in how coordinates work on a computer screen before we continue.

On a computer screen, coordinates work in terms of placement from the upper-left-hand corner of the screen. This makes it possible for every coordinate on the screen to be represented by any pair of positive numbers. Lets assume for a moment that the box shown below is a computer screen that is set to a 800 x 640 resolution.

A

 

B

 

C

The letter A would be in the (0,0) position on the computer screen. The letter B is half way across on the "x-axis" and half way down on the "y-axis" represented by the (400,320) position coordinate. The letter C would be in the (800,640) coordinate position.

For this homework, we are going to be using some basic geometric formulas in order to generate our fractal. In order to do this, we need a way to translate between "normal" geometric coordinates, and those on a computer screen. In order to do this, only three things have to be known

  1. The point that you want to convert
  2. The coordinate system you are in
  3. The point that is the "center" of your coordinate system.

The first two parts are rather easy, but the third one might be a problem at times. In most geometric coordinate systems, the origin (0,0) is the center of the coordinate systems. For the purposes of this assignment, this will do just fine. On a computer screen this is slightly different. When graphics are done with Dr.Scheme, a small segment of the screen is portioned off into a window called a CANVAS. A canvas lets you draw on it through some built-in functions provided in the Dr.Scheme environment. The coordinate system of a canvas works the same as that of a computer screen. The key difference between the canvas and the computer screen is that you can make the canvas an arbitrary size, thus making it easy for you to also calculate the center of the canvas. In order to facilitate drawing processes in Dr.Scheme, a teachpack called draw.ss is provided. You may want to stop and take some time to use the Help Desk to familiarize yourself with the functions in draw.ss.

In order to convert between the geometric coordinate system and the graphical coordinate system, the work is minimal

The contract, purpose, and definition for this process is given below

;; geometric->graphic : posn posn -> posn
;; 
;; Purpose:

;; Converts a coordinate given in a geometric system
;; to a coordinate on a computer screen centered at the
;; the point represented by the first POSN structure
;; consumed


(define (geometric->graphic center point)
     (make-posn (+ (posn-x point) (posn-x center))
                (- (posn-y center) (posn-y point))
     )
)

You are welcome to use this function in this assignment without providing any further documentation or design-recipe components.


Problem 1

You are provided with a way to convert between geometric to graphical coordinates. Write a function called graphic->geometric. The function should consume two posn structures and return the desired coordinate in the geometric system. The first posn structure will represent the "center point" of the graphics display area. The second posn structure will represent the point you wish to convert into geometric terms. Your function will be called in the following manner

(graphic->geometric <center point> <point to convert>)

The sample calls of this function would produce the following output

> (graphic->geometric (make-posn 200 200) (make-posn 205 195))
(make-posn 5 5)

> (graphic->geometric (make-posn 480 230) (make-posn 205 195))
(make-posn -275 35)


Problem 2

Now we need to create a function that will rotate a point through a given angle about the origin in the geometric system. The function should be called rotate which consumes a posn structure and an angle measurement (taken in degrees). The function will then return the given posn structure as if it was rotated about the origin of the geometric system through the given angle. The formula for rotating a point Q through a given angle A is

x = Qx cos A - Qy sin A

y = Qx sin A + Qy cos A

Your function will be called in the following manner

(rotate <point> <angle>)

The sample calls of this function would produce the following output

> (rotate (make-posn 1 0) 90)
(make-posn #i6.123031769111886e-017 #i1.0)

 

NOTES

The sin and cos function in Dr.Scheme consume angle measurements that are in radian measurements. You will have to convert your angle from degrees to radians inside of your rotate function.


Problem 3

Write a function called rotate-graphic that consumes two posn structures and a angle. The first posn structure consumed represents a point in the graphical system that you wish to rotate about, the second posn structure consumed represents the point in the graphical system you wish to rotate. The angle (measured in degrees) is the amount that you wish to rotate the second point by. Your function should return the corresponding point in the graphical system once the rotation is complete.

Your function will be called in the following manner

(rotate-graphic <center point> <point to rotate> <angle measurement>)

Sample output

> (rotate-graphic (make-posn 300 400) (make-posn 325 390) 180)
(make-posn #i275.0 #i410.0)

> (rotate-graphic (make-posn -325 400) (make-posn -325 390) 180)
(make-posn #i-325.0 #i410.0)


Problem 4

You are now going to put your fractal together. Write a function called dragon that will take in two posn structures and an angle measurement. The function will use generative recursion to create the following fractal.

This fractal was created using the draw.ss teachpack. The commands used for this particular fractal will be given later, but lets go over the algorithm used to produce the image above.

The image above is actual two calls to the dragon function. The commands below show calls that created the image (note that these commands may take a while to complete on some computers. Chances are if you are waiting more than 3 minutes to have any of these calls complete, you are doing something incorrectly.

> (stop)
> (start 400 400)
> (dragon (make-posn 100 350) (make-posn 100 250) 0)
true
> (dragon (make-posn 25 150) (make-posn 125 150) 0)
true

NOTE


Extra Variations

You've seen an "ugly" version of the fractal already. You can also modify your function from problem 4 to do the following variations

Note: You may change the parameters given in p4 in order to achieve these variations if the need arises. You must provide a commented out explanation of how to call your function if you attempt these variations.