943,907 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1831
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Dec 23rd, 2007
0

Re: Drawing Program

Here's a shorter version of the code, without the Line.

Point.h

C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Max_Payne is offline Offline
32 posts
since Oct 2007
Dec 24th, 2007
2

Re: Drawing Program

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
C++ Syntax (Toggle Plain Text)
  1. void gotoxy(int , int ) const ;
in point .cpp add following.
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  1. void Point::draw() const
  2. {
  3. gotoxy(x,y);
  4. cout<<".";
  5. }
Last edited by dubeyprateek; Dec 24th, 2007 at 8:51 am.
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006
Dec 24th, 2007
0

Re: Drawing Program



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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Max_Payne is offline Offline
32 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: compilnig using Dev C++ (Bloodshed)
Next Thread in C++ Forum Timeline: Urgent: Plz Help me,Prime Number Program Problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC