CS 105 - Understanding Our Algorithmic World

CS 105 - Assignment Two

Goals

  • Demonstrate your ability to create custom blocks
  • Demonstrate your familiarity with spatial programming
  • Demonstrate your knowledge of the use of reporters and predicates

Challenges

For this assignment, I am providing you with some starter code. This primarily provides you with sine and cosine functions (which can be found in the Operators palette). Save the project as Assignment02. Please do all of your work in this project.

Draw polygons (take two)

Our generalized polygons are only somewhat useful. The problem is that trying to control the size of the shape purely by controlling the length of the sides is quite challenging for anything beyond a square.

A better approach is to think of the radius of the shape -- the distance from the center out to the points of the shape. To figure out where the points of the shape are, we need to reach back to your Geometry or Trigonometry class. The points of the shape are essentially points on a circle of radius R.

Trig diagram

Write a new block that looks like draw polygon block. You will find blocks for sin and cos in the Operators palette.

Expectations

  • You should create a block that looks exactly like this: draw polygon block
  • When run, the block should draw a polygon with the specified sides and radius
  • The polygon should be centered on the current location of the sprite
  • The sprite should return to the center of the shape at the end of the drawing
  • The block should run clean -- it should handle pen up and pen down so that the drawing draws, but the pen is raised at the end.

Draw a color wheel

Now that you know how to draw a centered polygon, we are going to use this knowledge to explore hue. I would like you to create a block that draws a color wheel. This is what I am looking for:

color wheel

The code for this will look very similar to the code for the polygon. However, this time instead of a single line, you will be drawing full triangles (from the center, out to the point corresponding to the current angle, then to the next point around the circle, and back to the center again). I suggest you try this first before getting the color involved.

Once you have the triangles working, use the angle to set the hue. This is another mapping problem. This time you aren't going from location on the stage (0-480) to hue (0-100), you are going from angle (0-360) to hue (0-100). The color of the wedge should correspond to the angle of the first line of the wedge.

Filling the wedge is a little tricky because the fill block fills whatever region the sprite is in, so we need to get the sprite into the white area inside of your wedge. I found the most reliable way was to lift the pen and then move to the middle of the wedge to run fill block. To do that, I split the angle and then found a point that was only half a radius out.

compute the fill point

As an example, consider a four pointed color wheel with radius 100.

four point wheel

Since there are four sides, each wedge has an angle of 90 degrees. So the first spoke is at 0 degrees, the next one is at 90, the third is at 180 and the last is at 270.

four point outer points

The fill points would then be at 45, 135, 225, and 315.

four point inner points

Expectations

  • You should create a block that looks exactly like this: draw color wheel block
  • When run, the block should draw a color wheel with the specified number of slices and radius
  • The wheel should be centered on the current location of the sprite
  • The sprite should return to the center of the shape at the end of the drawing
  • The block should run clean -- it should handle pen up and pen down so that the drawing draws, but the pen is raised at the end.
  • Each slice should be filled with the appropriate color (determined by the angle of the start of the slice).

Exceeding expectations

For this assignment, you can exceed expectations by finishing all of the below:

  • Use your new polygon tool to draw a stop sign. It should look just like the one below and should draw centered on the sprite (I will leave size to you, but it can be a fixed size rather than an input). At the end of the block, the sprite should be left in the same position.

draw stop sign

  • Write a script that uses the mouse location to adjust the color wheel. Things that you could control include the number of slices, the radius, the saturation level or the brightness level. The important thing would be that the mouse location has a direct mapping to the given parameter (moving between the two extremes of the X or Y dimension should cover the desired range of the parameter), and that the mapping is performed for both the X and Y dimensions.

Since I want to encourage play, I will also consider other modifications, however, an E score will be at my discretion. Ask if you want to make sure that your contribution is sufficient.

Submitting

Share the project using the instructions from exercise 1.

Visit the assignment page on Canvas to submit the URL.