Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//*A simple screen saver in which the square bounces off the edges of the screen

ID: 3700037 • Letter: #

Question

//*A simple screen saver in which the square bounces off the edges of the screen. To do this, we need a velocity for both x and y:

QUESTIONS:

a) What are the return type used in the the code below

b)Parameters used

c) Description of every function written

Code:

#include "LPC17xx.H"

#include "GLCD.h"

#include "GLCD_UTILS.h"

void drawSquare(int cx, int cy, int width, unsigned short color){

int i;

int j;

for(i=cx-width/2; i<=cx+width/2; i++)

{

  

for(j=cy-width/2; j<=cy+width/2; j++)

{

GLCD_PutPixel(i,j);

}

  

}

//GLCD_Clear(Black);

}

int main() {

int vx=3;

int vy=2;

int cx=320/2;

int cy=240/2;

int width=50;

  

GLCD_Init(); //Initializes the LCD display

GLCD_Clear(Black); //Clear display and set background color to black

GLCD_SetTextColor(Blue); //Pixels will be drawn in blue

  

while(1) {

//An infinite loop is needed or else the program will just exit

int k;

GLCD_Clear(Black);

cx=cx+vx;

cy=cy+vy;

drawSquare(cx, cy, width, Black);

drawSquare(cx,cy,width, Blue);

for (k=0; k<10000;k++);

if(cx>=319-width/2){

vx=-vx;

}

if(cx<=width/2){

vx=-vx;

}

if(cy>=239-width/2){

vy=-vy;

}

if(cy<=width/2){

vy=-vy;

}

  

}

Explanation / Answer

3. void drawSquare(int cx, int cy, int width, unsigned short color) :- This method takes in screen d imension and the width of square as input and then with the help of loops starts creates the square which fits the size of screen by putting the pixels together with help of .GLCD_PutPixel(i,j) in x and y directions.

GLCD_Init(), GLCD_Clear(Black), GLCD_SetTextColor(Blue):- These functions have already been explained in comments.

Then in the infinite while loop we are drawing the squares by calling draw square method and making them float on the screen. By using, vx = -vx and vy = -vy we are reversing the direction of square's motion on hitting/touching the border of screen.

Hope this will help!!! Thanks!!!