Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit




ASSIGNMENT 5: Simple Drawing Program

COURSE NUMBER: COMP 1010

COURSE TITLE: Introduction to Computer Science 1

TERM: Fall 2021


Assignment 5:  Simple Drawing Program

 

 

Simple Drawing program - 40 marks

Implement a simple drawing program. This program uses arrays to store points where the mouse was clicked, and functions that modify the array in certain ways. You will be bulding the             drawing program in 3 questions (stages). You will use the program from previous question as a   starting point for the next question.

The program uses two global arrays to store points

where mouse clicks have occurred. These arrays

initially hold only 5 points each – you will

automatically grow them to accommodate more points,

as described below. You will use the partially filled

array technique which uses an integer to indicate how

many bins are actually full. That is, ifthe integer

indicates that there are n data items in the array, this

means the first n entries ofthe array contain the data.

Note: this means that the length property ofthe array

does not tell you how many data points are in the

array, just the maximum number it can hold.

The setup and draw functions are provided for you, as

well as some initial global declarations in the starting

template called A5_Start.pde. You may need to add

some additional global constants/variables to the program. You are allowed to use these global   variables and constants in the draw, setup, mousePressed, and keyPressed functions. You are      NOT allowed to use them, or any other global variable you may create, in the other small utility functions described in the questions. They must only operate on the data provided in their           parameters, and only return data using the return mechanism.



 

Q1: Basic Drawing Feature (10 marks)

Start with provided template A5_Start.pde and implement the following functions to create the basic drawing feature:

int addPoint(int x, int y, int[] pointsX, int[] pointsY, int curSize)

     Adds the given x and y point to the corresponding arrays. curSize indicates how many      data entries are already in the partially-filled arrays, and the function returns the new number of data points in the arrays. This function may assume that there is enough space for one        more point.

void drawLines(int[] x, int[] y, int curSize)

     Draws lines between consecutive pairs of points in the partially filled arrays, forming a continuous line.

int[] doubleArray(int[] oldArry)

     Creates and returns a new array that is double the size ofthe old array, with all data from oldArry copied into the new one. There is no curSize parameter this time – it assumes      that the oldArry is full of data.

Implementation of built-in function mousePressed():

void mousePressed()

Processing automatically calls this function each time a mouse button is pressed. When this          happens, add the current mouse location to your global arrays of points using the addPoint            function. This is the function that must make sure there is enough space in the array. Ifthere         isn't, double the size of the arrays using the doubleArray function. Remember, you are allowed to use the global variables in this function.

At this point, you will have a program that provides basic drawing. Save and submit your program for Q1.

 

Q2: Drawing Modification Feature - Part 1 (15 marks)

In this question, you will add some basic features for the user to be able to modify the drawing    using keyboard. User will be able to change the thickness of lines, move the drawing to different directions, remove the last point (line), erase the drawing, and change the color of drawing. Start with your program from Q1 and add implementation of the following functions:

void addToData(int[] data, int curSize, int delta)

     Add the quantity in delta to the value of every element in the partially filled array.

int removeLastPoint(int curSize)

     Remove the last point added to the partially filled array which consequently removes the last line added.

int changeLineThickness (int currThickness, int thicknessChange)

     Change the current line thickness by thicknessChange value for drawing. Minimum thickness is 1 and maximum thickness is 5.


 

Implementation of built-in function keyPressed()

void keyPressed()

     Processing calls this function once for each time a key is pressed, and stores the pressed   key as a character in the key variable. Implement the following functionalities depending on what key is pressed. Again, you are allowed to use the global variables in this              function. Also, use your other functions described previously where appropriate.

     q – Erase all points (hint: only one line of code is needed to accomplish this).

     w, a, s, d – Move all points by some fixed amount (define a named constant) in the appropriate direction (up, down, left, or right).

      ! – Change the color to some new random color.

     r – remove the last point added which removes the last line.

     u – Increase the thickness of lines by one.

     v – Decrease the thickness of lines by one

At this point, you will have a program that provides basic drawing and some drawing modifications. Save and submit your program for Q2.

Q3: Drawing Modification Feature - Part 2 (11 marks)

In this question, you will add more features for the user to be able to modify the drawing using keyboard. User will be able to change the size of the drawing and make the drawing chunkier.  Start with your program from Q2 and add implementation of the following functions:

void multiplyData(int[]data, int curSize, float rate)

     Multiplies the value of every element in the partially filled array by the rate value.

int shrinkData(int[] data, int curSize)

     Removes half of the data from the given array, and returns the new array size (simply use integer division by two to calculate the new size – do not worry about the remainder).       Data from the even numbered positions are kept, and the ones from odd numbered            positions are removed.

     Hint: this is tricky, but simple once you get it. Use a loop to go through the data, and at each index i, copy the data from its old location to this new location. Write out a few     examples on paper to see the pattern.

int averagePoints(int[] data, int curSize)

     Averages all the data in the partially-filled array. Simply sum and then divide by the count.

void scaleAroundOffset(int[] data, int curSize, int offset, float scale)

     This is a function which modifies all of the values in data so that they get closer to, or     further from, the offset value, by a factor of scale. For example, if offset is 0 and scale is 2, then all values will move twice as far from 0. (Simply multiplying all of the values by

2 would do this.)

     However, if offset is 5 and scale is 0.5, the all values must move twice as close to the     value 5. To do this, first subtract offset from the data values, then multiply them by the  scale factor, then add the offset back in. Do not use a for loop. Use your other functions to accomplish this.


 

Implementation of built-in function keyPressed()

void keyPressed()

     Processing calls this function once for each time a key is pressed, and stores the pressed   key as a character in the key variable. Implement the following functionalities depending on what key is pressed. Again, you are allowed to use the global variables in this              function. Also, use your other functions described previously where appropriate.

     c – Delete every second point to make your sketch “chunkier”.

     + – Increase the size of your drawing, by first using averagePoints to calculate your         shape’s centre, and then scaling your drawing by a factor of 1.5 times around that centre.

     - – Decrease the size of your drawing, by first using averagePoints to calculate your     shape’s centre, and then scaling your drawing by a factor of 2.0/3.0 around that centre.

At this point, you will have a program that provides basic drawing and some more drawing modifications. Save and submit your program for Q3.

 

Code Check (4 marks)

     Proper naming for variable and constants

     Proper commenting

     All other programming standards are followed