1 / 24

VG101 RECITATION 5

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

Download Presentation

VG101 RECITATION 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. VG101RECITATION 5 By TAs

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

  3. 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 • TriangleT • RectangleT • ArcT: a circle is an arc with an angle of 360 degrees

  4. 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)

  5. HOW TO READ VG101CLASS.H • Let’s see what’s the general structure of a class definition

  6. Base class Data members Private member constructor 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); }; Public member Get & Set Function members

  7. HOW TO READ VG101CLASS.H • About constructors 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); • Note: • 1. No return type, function name is just its class name. • 2. You should declare an object using one of its constructors:

  8. 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);

  9. HOW TO READ VG101CLASS.H • What are useful to us • Get and Set functions double getWidth () const; void setWidth (const double w); • Other public functions void 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.

  10. Samples about graphics • Draw a line from (2,1) to (5,3) in red

  11. SAMPLES ABOUT GRAPHICS • Draw an empty triangle specified by three points (1,1), (3, 1), (2.3, 3) in green

  12. 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

  13. 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

  14. 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

  15. SAMPLES ABOUT GRAPHICS • Draw an arc start from0 degree and sweep 270 degree with radius 0.6 in purple, centered at (0.6, 0.6)

  16. SAMPLES ABOUT GRAPHICS • Draw an empty circle with radius 0.6 in purple, centered at (0.6, 0.6)

  17. SAMPLES ABOUT GRAPHICS • Draw a filled circle with radius 0.6 in purple, centered at (0.6, 0.6) with density of 0.9

  18. ABOUT ASSIGNMENT 5 • Where’s Tom • Understand the pseudo code for function takeTomHome () • Every step is a line with constant length (step size) in a random direction • Check 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

  19. ABOUT ASSIGNMENT 5 • Bouncing ball • Check before taking the step if the ball is going to hit the wall • When 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

  20. Array • An array is an ordered collection of data values of the sametype • Passing an array into a function • void initArray(int array[], int n); • void printArray(int array[], int n); • Usually, n is the array length

  21. 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); }

  22. 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 ); }

  23. 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 ); } }

  24. ARRAY • Here is the program’s output

More Related