VG101 RECITATION 5

24
VG101 RECITATION 5 By TAs

description

VG101 RECITATION 5. By TAs. CONTENTS. How to read Vg101Class.h Samples about graphics About assignment 5 Array. HOW TO READ VG101CLASS.H. This file defines the following classes: ConsoleT GwindowsT: represent the graphics window GObjectT: base class for all geometric classes LineT - PowerPoint PPT Presentation

Transcript of VG101 RECITATION 5

Page 1: VG101 RECITATION 5

VG101RECITATION 5By TAs

Page 2: VG101 RECITATION 5

CONTENTS

How to read Vg101Class.h Samples about graphics About assignment 5 Array

Page 3: VG101 RECITATION 5

HOW TO READ VG101CLASS.H

This file defines the following classes:

ConsoleTGwindowsT: represent the graphics window

GObjectT: base class for all geometric classesLineTTriangleTRectangleTArcT: a circle is an arc with an angle of 360

degrees

Page 4: VG101 RECITATION 5

HOW TO READ VG101CLASS.H

RandomT: random numbers and colors PointT: represent a point using (x, y) PenT: record the current location and color of

the pen MouseT: detect the click event of the mouse,

get mouse current location TextT: set text color, style, font… when

printing a message to the graphics window (not console window)

Page 5: VG101 RECITATION 5

HOW TO READ VG101CLASS.H

Let’s see what’s the general structure of a class definition

Page 6: VG101 RECITATION 5

class RectangleT : public GObjectT

{

PointT llp, urp;

void drawRect ();

public:

RectangleT (const double x0 = 0, const double y0 = 0,

const double x1 = 0, const double y1 = 0,

const string clr = "Blue", const double fill = 0);

RectangleT (const PointT &p0, const PointT &p1,

const string clr = "Blue", const double fill = 0);

const PointT &getLocation () {return llp;}

double getWidth () const;

double getHeight () const;

double getArea () const;

void setLocation (const double x, const double y);

void setLocation (const PointT p);

void setWidth (const double w);

void setHeight (const double h);

void draw ();

void draw (const string clr, const double density = 0);

};

Base class

Private membe

r

Public membe

r

constructor

Get & Set

Data members

Function members

Page 7: VG101 RECITATION 5

HOW TO READ VG101CLASS.H

About constructorsRectangleT (const double x0 = 0, const double y0 = 0,

const double x1 = 0, const double y1 = 0,

const string clr = "Blue", const double fill = 0);

RectangleT (const PointT &p0, const PointT &p1,

const string clr = "Blue", const double fill = 0);

Note: 1. No return type, function name is just its class

name. 2. You should declare an object using one of its

constructors:

Page 8: VG101 RECITATION 5

HOW TO READ VG101CLASS.H

If the constructor gives the default value of a parameter, then you don’t need to give that parameter.

use RectangleT myRec(); // default position, color, fill

or RectangleT myRec(1, 1, 2, 3); // default color, fill

or RectangleT myRec(1, 1, 2, 3, “red”, 0.5); // p1, p2 are two known PointT object

or RectangleT myRec(p1, p2); // default color, fill

or RectangleT myRec(p1, p2, “green”, 0.8);

Page 9: VG101 RECITATION 5

HOW TO READ VG101CLASS.H

What are useful to us Get and Set functions

double getWidth () const;

void setWidth (const double w);

Other public functionsvoid draw ();

void draw (const string clr, const double density = 0); // overloaded function

Note:

1. You can’t access private/protected data or use private/protected functions directly.

2. The get & set functions offer you access to some private data.

3. Read function comment carefully.

Page 10: VG101 RECITATION 5

SAMPLES ABOUT GRAPHICS

Draw a line from (2,1) to (5,3) in red

Page 11: VG101 RECITATION 5

SAMPLES ABOUT GRAPHICS

Draw an empty triangle specified by three points (1,1), (3, 1), (2.3, 3) in green

Page 12: VG101 RECITATION 5

SAMPLES ABOUT GRAPHICS

Draw a filled triangle specified by three points (1,1), (3, 1), (2.3, 3) in green with density of 0.4

Page 13: VG101 RECITATION 5

SAMPLES ABOUT GRAPHICS

Draw an empty rectangle whose lower left point is (0.5, 0.5) and upper right point is (1.5, 1.2) in yellow

Page 14: VG101 RECITATION 5

SAMPLES ABOUT GRAPHICS

Draw a filled rectangle whose lower left point is (0.5, 0.5) and upper right point is (1.5, 1.2) in yellow with density of 0.8

Page 15: VG101 RECITATION 5

SAMPLES ABOUT GRAPHICS

Draw an arc start from 0 degree and sweep 270 degree with radius 0.6 in purple, centered at (0.6, 0.6)

Page 16: VG101 RECITATION 5

SAMPLES ABOUT GRAPHICS

Draw an empty circle with radius 0.6 in purple, centered at (0.6, 0.6)

Page 17: VG101 RECITATION 5

SAMPLES ABOUT GRAPHICS

Draw a filled circle with radius 0.6 in purple, centered at (0.6, 0.6) with density of 0.9

Page 18: VG101 RECITATION 5

ABOUT ASSIGNMENT 5

Where’s TomUnderstand the pseudo code for function

takeTomHome ()Every step is a line with constant length

(step size) in a random directionCheck before taking the step if Tom is

going out of the universe (the do-while loop in pseudo code)

Check after taking the step if Tom is already at home

Page 19: VG101 RECITATION 5

ABOUT ASSIGNMENT 5

Bouncing ballCheck before taking the step if the ball is

going to hit the wallWhen hitting the wall, adjust the location

of the ball so that it just touch the wall (on the line of its original direction)

Change color when change direction

Don’t use magic numbers Give a good name for your variables

Page 20: VG101 RECITATION 5

ARRAY

An array is an ordered collection of data values of the same type

Passing an array into a function

void initArray(int array[], int n); void printArray(int array[], int n);

Usually, n is the array length

Page 21: VG101 RECITATION 5

ARRAY A sample on passing an array into a function

#include <Vg101Class.h>#include <cmath>using namespace std;void initArray ( int intArray[], int n);void printArray ( int intArray[], int n);

void main(){

int intArray[4];initArray ( intArray, 4);printArray ( intArray, 4);

}

Page 22: VG101 RECITATION 5

ARRAY A sample on passing an array into a function

void initArray(int intArray[], int n){

ConsoleT c;for ( int i = 0 ; i < n ; i++){

c.printLine ( "Enter a integer value for the ", i+1, " th element: ");

intArray[i] = c.readInt ("");;}c.printLine ( endl );

}

Page 23: VG101 RECITATION 5

ARRAY

A sample on passing an array into a function

void printArray(int intArray[], int n){

ConsoleT c;for ( int i = 0 ; i < n ; i++){

c.printLine ( "The ", i+1, " th element is ", intArray[i], endl );}

}

Page 24: VG101 RECITATION 5

ARRAY

Here is the program’s output