Drawing Program

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2007
Posts: 32
Reputation: Max_Payne is an unknown quantity at this point 
Solved Threads: 0
Max_Payne's Avatar
Max_Payne Max_Payne is offline Offline
Light Poster

Re: Drawing Program

 
0
  #11
Dec 23rd, 2007
Here's a shorter version of the code, without the Line.

Point.h

  1. #pragma once
  2.  
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. class Point
  7. {
  8. private:
  9. int x;
  10. int y;
  11.  
  12. public:
  13. Point(void);
  14. Point( int x, int y );
  15. Point( const Point &xPoint );
  16. ~Point(void);
  17.  
  18. void setX( int x );
  19. int getX() const;
  20.  
  21. void setY( int y );
  22. int getY() const;
  23.  
  24. Point & operator = ( const Point &xPoint );
  25.  
  26. bool operator == ( const Point &xPoint ) const;
  27.  
  28. bool includes( int cord_X, int cord_Y ) const;
  29. bool includes( const Point &xPoint ) const;
  30.  
  31. void draw() const;
  32. };

/////////////////////////////////////////////////////
/////////////////////////////////////////////////////

Point.cpp

  1. #include "Point.h"
  2.  
  3. Point::Point( void )
  4. {
  5. this->x = 0;
  6. this->y = 0;
  7. }
  8.  
  9. Point::Point( int x, int y )
  10. {
  11. this->x = x;
  12. this->y = y;
  13. }
  14.  
  15. Point::Point( const Point &xPoint )
  16. {
  17. this->x = xPoint.x;
  18. this->y = xPoint.y;
  19. }
  20.  
  21. Point::~Point( void ) {}
  22.  
  23. /////////////////////////////////////////////////////
  24. /////////////////////////////////////////////////////
  25.  
  26. void Point::setX( int x ) // set X
  27. {
  28. this->x = x;
  29. }
  30.  
  31. void Point::setY( int y ) // set Y
  32. {
  33. this->y = y;
  34. }
  35.  
  36.  
  37. int Point::getX() const // get X
  38. {
  39. return ( this->x );
  40. }
  41.  
  42. int Point::getY() const // get Y
  43. {
  44. return ( this->y );
  45. }
  46.  
  47. /////////////////////////////////////////////////////
  48. /////////////////////////////////////////////////////
  49.  
  50. Point &Point::operator = ( const Point &xPoint )
  51. {
  52. if( this != &xPoint )
  53. {
  54. this->x = xPoint.x;
  55. this->y = xPoint.y;
  56. }
  57.  
  58. return ( *this );
  59. }
  60.  
  61. bool Point::includes( int cord_X, int cord_Y ) const
  62. {
  63. return ( this->x == cord_X && this->y == cord_Y );
  64. }
  65.  
  66. void Point::draw() const
  67. {
  68. cout << '.';
  69. }
  70.  
  71. /////////////////////////////////////////////////////
  72. /////////////////////////////////////////////////////
  73.  
  74. bool Point::operator == ( const Point &xPoint ) const
  75. {
  76. bool isEquality = false;
  77. if( this->x == xPoint.x && this->y == xPoint.y )
  78. {
  79. isEquality = true;
  80. }
  81. return isEquality;
  82. }

/////////////////////////////////////////////////////
/////////////////////////////////////////////////////


PointCollection.h

  1. #pragma once
  2.  
  3. #include "Point.h"
  4.  
  5. const int MAXPoint = 100;
  6.  
  7. class PointCollection
  8. {
  9. private:
  10. Point pts[ MAXPoint ];
  11. int quantity;
  12.  
  13. public:
  14. PointCollection( void );
  15. PointCollection( const PointCollection &xPointCollection );
  16. ~PointCollection( void );
  17.  
  18. // To know how many Points are
  19. // ..in the collection
  20. int size() const;
  21.  
  22. PointCollection operator = ( const PointCollection &xPointCollection );
  23.  
  24. PointCollection &operator + ( const Point &xPoint );
  25.  
  26. Point &operator[]( int index );
  27. const Point &operator[]( int index ) const;
  28.  
  29. bool isFull() const;
  30. bool isEmpty() const;
  31.  
  32. bool includes( const Point &xPoint ) const;
  33. int indexOf( const Point &xPoint ) const;
  34. };

/////////////////////////////////////////////////////
/////////////////////////////////////////////////////

PointCollection.cpp

  1. #include "PointCollection.h"
  2.  
  3. PointCollection::PointCollection( void )
  4. {
  5. this->quantity = 0;
  6. }
  7.  
  8. PointCollection::PointCollection( const PointCollection &xPointCollection )
  9. {
  10. this->quantity = xPointCollection.quantity;
  11.  
  12. for ( int i = 0; i < this->quantity; i++ )
  13. ( this->pts )[ i ] = ( xPointCollection.pts )[ i ];
  14. }
  15.  
  16. PointCollection::~PointCollection( void ) {}
  17.  
  18. /////////////////////////////////////////////////////
  19. /////////////////////////////////////////////////////
  20.  
  21. int PointCollection::size() const
  22. {
  23. return ( this->quantity );
  24. }
  25.  
  26. /////////////////////////////////////////////////////
  27. /////////////////////////////////////////////////////
  28.  
  29. Point &PointCollection::operator []( int index )
  30. {
  31. return ( ( this->pts )[ index ] );
  32. }
  33.  
  34. const Point &PointCollection::operator []( int index ) const
  35. {
  36. return ( ( this->pts )[ index ] );
  37. }
  38.  
  39. /////////////////////////////////////////////////////
  40. /////////////////////////////////////////////////////
  41.  
  42. PointCollection PointCollection::operator =( const PointCollection &xPointCollection )
  43. {
  44. this->quantity = xPointCollection.quantity;
  45.  
  46. for ( int i = 0; i < this->quantity; i++ )
  47. ( this->pts )[ i ] = ( xPointCollection.pts )[ i ];
  48.  
  49. return ( *this );
  50. }
  51.  
  52. PointCollection &PointCollection::operator +( const Point &xPoint )
  53. {
  54. (*this)[ this->quantity ] = xPoint;
  55.  
  56. (this->quantity)++;
  57.  
  58. return ( *this );
  59. }
  60.  
  61. /////////////////////////////////////////////////////
  62. /////////////////////////////////////////////////////
  63.  
  64. bool PointCollection::isFull() const
  65. {
  66. return ( this->quantity == MAXPoint );
  67. }
  68.  
  69. bool PointCollection::isEmpty() const
  70. {
  71. return ( this->quantity == 0 );
  72. }
  73.  
  74. /////////////////////////////////////////////////////
  75. /////////////////////////////////////////////////////
  76.  
  77. bool PointCollection::includes( const Point &xPoint ) const
  78. {
  79. bool thisOne = false;
  80.  
  81. for ( int i = 0; i < this->quantity && ! thisOne; i++ )
  82. {
  83. if( pts[ i ] == xPoint )
  84. thisOne = true;
  85. }
  86.  
  87. return ( thisOne );
  88. }
  89.  
  90. int PointCollection::indexOf( const Point &xPoint ) const
  91. {
  92. int index = -1;
  93.  
  94. for ( int i = 0; i < this->quantity && index == -1; i++ )
  95. {
  96. if( pts[ i ] == xPoint )
  97. index = i;
  98. }
  99.  
  100. return ( index );
  101. }


Page.h

  1. #pragma once
  2.  
  3. #include "PointCollection.h"
  4.  
  5. const int MAXPAGE = 100;
  6.  
  7. class Page
  8. {
  9. private:
  10. int heigh;
  11. int width;
  12.  
  13. public:
  14. Page( void );
  15. Page( int width, int heigh );
  16. Page( const Page &xPage );
  17. ~Page( void );
  18.  
  19. void setWidth( int width );
  20. int getWidth() const;
  21.  
  22. void setHeigh( int heigh );
  23. int getHeigh() const;
  24.  
  25. bool operator == ( const Page &xPage ) const;
  26.  
  27. void draw() const;
  28. };

/////////////////////////////////////////////////////
/////////////////////////////////////////////////////

Page.cpp

  1. #include "Page.h"
  2.  
  3. Page::Page( void )
  4. {
  5. this->width = 0;
  6. this->heigh = 0;
  7. }
  8.  
  9. Page::Page( int width, int heigh )
  10. {
  11. this->width = width;
  12. this->heigh = heigh;
  13. }
  14.  
  15. Page::Page( const Page &xPage )
  16. {
  17. this->width = xPage.width;
  18. this->heigh = xPage.heigh;
  19. }
  20.  
  21. Page::~Page( void ) {}
  22.  
  23. /////////////////////////////////////////////////////
  24. /////////////////////////////////////////////////////
  25.  
  26. void Page::setHeigh( int heigh )
  27. {
  28. this->heigh = heigh;
  29. }
  30.  
  31. void Page::setWidth( int width )
  32. {
  33. this->width = width;
  34. }
  35.  
  36. int Page::getHeigh() const
  37. {
  38. return ( this->heigh );
  39. }
  40.  
  41. int Page::getWidth() const
  42. {
  43. return ( this->width );
  44. }
  45.  
  46. /////////////////////////////////////////////////////
  47. /////////////////////////////////////////////////////
  48.  
  49. void Page::draw() const
  50. {
  51. bool included;
  52. PointCollection pc;
  53.  
  54. for( int y = 0; y < this->heigh; y++ )
  55. {
  56. included = false;
  57.  
  58. for ( int x = 0; x < this->width; x++ )
  59. {
  60. for ( int p = 0; p < pc.size() && !included; p++ )
  61. {
  62.  
  63. if ( pc[ p ].includes( x, y ) )
  64. {
  65. pc[ p ].draw();
  66. included = true;
  67. }
  68.  
  69. if ( included )
  70. cout << ' ';
  71. }
  72.  
  73. }
  74. cout << endl;
  75. }
  76. }
  77.  
  78. /////////////////////////////////////////////////////
  79. /////////////////////////////////////////////////////
  80.  
  81. bool Page::operator ==( const Page &xPage ) const
  82. {
  83. bool isEquality = false;
  84.  
  85. if( this->heigh == xPage.heigh && this->width == xPage.width )
  86. {
  87. isEquality = true;
  88. }
  89.  
  90. return isEquality;
  91. }


Main.cpp

  1. #include "Page.h"
  2.  
  3. int main ()
  4. {
  5.  
  6. Point p1( 5, 5 ), p2( 1, 1 );
  7. Page pg( 15, 15 );
  8.  
  9. p1.draw();
  10.  
  11. pg.draw();
  12.  
  13. system("PAUSE");
  14. return 0;
  15.  
  16. }


This has nothing to do with Graphics.

I only want p1.draw() to be printed on the location ( 5, 5 ) ( x, y ) in the page.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Re: Drawing Program

 
2
  #12
Dec 24th, 2007
You are not moving the cursor at right place before you do a "cout"
Ycan move cursor by including one more function in your point class
in point.h
add a member funtion in the point class
  1. void gotoxy(int , int ) const ;
in point .cpp add following.
  1.  
  2.  
  3. void Point::gotoxy(int x, int y) const
  4. {
  5. HANDLE hConsoleOutput;
  6. COORD Cursor_Pos = {x, y};
  7.  
  8. hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  9. SetConsoleCursorPosition(hConsoleOutput, Cursor_Pos);
  10. }

use it in your draw member function
  1. void Point::draw() const
  2. {
  3. gotoxy(x,y);
  4. cout<<".";
  5. }
Last edited by dubeyprateek; Dec 24th, 2007 at 8:51 am.
I know I am. Therefore I am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 32
Reputation: Max_Payne is an unknown quantity at this point 
Solved Threads: 0
Max_Payne's Avatar
Max_Payne Max_Payne is offline Offline
Light Poster

Re: Drawing Program

 
0
  #13
Dec 24th, 2007


Thank You Very much. I can't begin to express my gratitude for helping me out on this. Now i can add the rest of my classes without any problem.

Thank You.

Cheers...

Last edited by Max_Payne; Dec 24th, 2007 at 9:11 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC