Drawing Program

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 12-21-2007
Max_Payne
Drawing Program
I made a program that prints dots and lines in a Page. So far that's all i could come up with.
When i try to print the lines and the dots it just prints consecutive points,I want it to print the points & lines in the page's coordinates.

Point.h

Code:

```#pragma once #include <iostream> using namespace std; class Point { private:         int x;         int y; public:         Point(void);         Point( int x, int y );         Point( const Point &xPoint );         ~Point(void);         void setX( int x );         int getX() const;         void setY( int y );         int getY() const;         Point & operator = ( const Point &xPoint );         bool operator == ( const Point &xPoint ) const;         bool includes( int cord_X, int cord_Y ) const;         bool includes( const Point &xPoint ) const;         void draw() const; };```
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////

Point.cpp

Code:

```#include "Point.h" Point::Point( void ) {         this->x = 0;         this->y = 0; } Point::Point( int x, int y ) {         this->x = x;         this->y = y; } Point::Point( const Point &xPoint ) {         this->x = xPoint.x;         this->y = xPoint.y; } Point::~Point( void ) {} ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// void Point::setX( int x ) // set X {         this->x = x; } void Point::setY( int y ) // set Y {         this->y = y; } int Point::getX() const // get X {         return ( this->x ); } int Point::getY() const // get Y {         return ( this->y ); } ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// Point &Point::operator = ( const Point &xPoint ) {         if( this != &xPoint )     {                 this->x = xPoint.x;                 this->y = xPoint.y;     }     return ( *this ); } bool Point::includes( int cord_X, int cord_Y ) const {         return ( this->x == cord_X && this->y == cord_Y ); } void Point::draw() const {         cout << '.'; } ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// bool Point::operator == ( const Point &xPoint ) const {     bool isEquality = false;     if( this->x == xPoint.x && this->y == xPoint.y )     {         isEquality = true;     }     return isEquality; }```
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////

PointCollection.h

Code:

```#pragma once #include "Point.h" const int MAXPoint = 100; class PointCollection { private:         Point pts[ MAXPoint ];         int quantity; public:         PointCollection( void );         PointCollection( const PointCollection &xPointCollection );         ~PointCollection( void );         // To know how many Points are         // ..in the collection         int size() const;         PointCollection operator = ( const PointCollection &xPointCollection );         PointCollection &operator + ( const Point &xPoint );                 Point &operator[]( int index );         const Point &operator[]( int index ) const;         bool isFull() const;         bool isEmpty() const;         bool includes( const Point &xPoint ) const;         int indexOf( const Point &xPoint ) const; };```
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////

PointCollection.cpp

Code:

```#include "PointCollection.h" PointCollection::PointCollection( void ) {         this->quantity = 0; } PointCollection::PointCollection( const PointCollection &xPointCollection ) {         this->quantity = xPointCollection.quantity;         for ( int i = 0 ; i < this->quantity ; i++ )                 ( this->pts )[ i ] = ( xPointCollection.pts )[ i ]; } PointCollection::~PointCollection( void ) {} ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// int PointCollection::size() const {         return ( this->quantity ); } ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// Point &PointCollection::operator []( int index ) {         return ( ( this->pts )[ index ] ); } const Point &PointCollection::operator []( int index ) const {         return ( ( this->pts )[ index ] ); } ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// PointCollection PointCollection::operator =( const PointCollection &xPointCollection ) {         this->quantity = xPointCollection.quantity;         for ( int i = 0 ; i < this->quantity ; i++ )                 ( this->pts )[ i ] = ( xPointCollection.pts )[ i ];         return ( *this ); } PointCollection &PointCollection::operator +( const Point &xPoint ) {         (*this)[ this->quantity ] = xPoint;         (this->quantity)++;         return ( *this ); } ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// bool PointCollection::isFull() const {         return ( this->quantity == MAXPoint ); } bool PointCollection::isEmpty() const {         return ( this->quantity == 0 ); } ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// bool PointCollection::includes( const Point &xPoint ) const {         bool thisOne = false;         for ( int i = 0 ; i < this->quantity && ! thisOne ; i++ )         {                 if( pts[ i ] == xPoint )                         thisOne = true;         }         return ( thisOne ); } int PointCollection::indexOf( const Point &xPoint ) const {         int index = -1;         for ( int i = 0 ; i < this->quantity && index == -1 ; i++ )         {                 if( pts[ i ] == xPoint )                         index = i;         }         return ( index ); }```
Line.h

Code:

```#pragma once #include "Point.h" class Linea { private:         Point begin;         Point end; public:         Linea( void );         Linea( const Linea &xLinea );         Linea( Point XY1, Point XY2 );         ~Linea( void );         Linea &operator = ( const Linea &xLinea );         bool includes( int cord_X, int cord_Y ) const;         bool operator == ( const Linea &xLinea ) const;         double m() const; // Pendiente         double b() const; // Intercept in Y         bool isVertical() const;         bool isHorizontal() const;         bool isDiagonal() const;         void draw() const; };```
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////

Line.cpp

Code:

```#include "Linea.h" Linea::Linea() { } Linea::Linea( Point XY1, Point XY2 ) {         this->begin = begin;         this->end = end; } Linea::Linea( const Linea &xLinea ) {         this->begin = xLinea.begin;         this->end = xLinea.end; } Linea::~Linea( void ) { } ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// Linea &Linea::operator =( const Linea &xLinea ) {         if( this != &xLinea )     {                 this->begin = xLinea.begin;                 this->end = xLinea.end;     }     return ( *this ); } bool Linea::operator == ( const Linea &xLinea ) const {     bool isEquality = false;     if( this->begin == xLinea.begin &&                 this->end == xLinea.end )     {         isEquality = true;     }     return isEquality; } ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// double Linea::m()const {         return ( (this->end.getY()) - (this->begin.getY()) ) / ((this->end.getX()) - (this->begin.getX()) ); } double Linea::b()const {         return ( this->end.getY() - (this->m() * this->end.getX()) ); } bool Linea::isVertical () const {         return ( ( (this->end.getX()) - (this->begin.getX()) ) == 0 ); } bool Linea::isHorizontal () const {         return ( ( (this->end.getY()) - (this->begin.getY()) ) == 0 ); } bool Linea::isDiagonal () const {         return ( this->isVertical() && this->isHorizontal() ); } ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// bool Linea::includes( int cord_X, int cord_Y ) const {         bool included = false;         if ( this->isHorizontal() )         {                 if ( ( (this->begin).getY() == cord_Y ) &&                     ( (this->begin).getX() <= cord_X ) &&                     ( (this->end).getX() >= cord_X ) ||                     ( (this->begin).getX() >= cord_X ) &&                     ( (this->end).getX() <= cord_X ) )                                 included = true;         }         else if ( this->isVertical() )         {                 if ( ( (this->begin).getX() == cord_X ) &&                     ( (this->begin).getY() <= cord_Y ) &&                     ( (this->end).getY() >= cord_Y ) ||                     ( (this->begin).getY() >= cord_Y ) &&                                                 ( (this->end).getY() <= cord_Y ) )                                 included = true;         }         else         {                 if ( ( cord_Y == this->m() * cord_X + this->b() ) &&                         ( (this->begin).getX() <= cord_X ) &&                                                 ( (this->end).getX() >= cord_X ) ||                                                 ( (this->begin).getX() >= cord_X ) &&                         ( (this->end).getY() >= cord_Y ) ||                         ( (this->begin).getY() >= cord_Y ) &&                         ( (this->end).getY() <= cord_Y ) )                                 included = true;         }         return ( included ); } ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// void Linea::draw() const {         cout << '.'; }```

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

LineCollectoin.h

Code:

```#pragma once #include "Linea.h" const int MAX = 100; class LineCollection { private:         Linea lc[ MAX ];         int quantity; public:         LineCollection(void);         LineCollection( const LineCollection &xLineCollection );         ~LineCollection(void);         // To know how many Lines are         // ..in the collection         int size() const;         LineCollection operator = ( const LineCollection &xLineCollection );         LineCollection &operator + ( const Linea &xLinea );         Linea &operator [] ( int index );         const Linea &operator[]( int index ) const;         bool isFull() const;         bool isEmpty() const;         bool includes( const Linea &xLinea ) const;         int indexOF( const Linea &xLinea ) const;         friend ostream &operator << ( ostream &output, const LineCollection &xLineCollection );         friend istream &operator >> ( istream &input, LineCollection &xLineCollection ); };```
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////

LineCollection.cpp

Code:

```#include "LineCollection.h" LineCollection::LineCollection( void ) {         this->quantity = 0; } LineCollection::LineCollection( const LineCollection &xLineCollection ) {         this->quantity = xLineCollection.quantity;         for ( int i = 0 ; i < this->quantity ; i++ )                 ( this->lc )[ i ] = ( xLineCollection.lc )[ i ]; } LineCollection::~LineCollection( void ) {} ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// int LineCollection::size() const {         return ( this->quantity ); } ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// Linea &LineCollection::operator []( int index ) {         return ( ( this->lc )[ index ] ); } const Linea &LineCollection::operator []( int index ) const {         return ( ( this->lc )[ index ] ); } ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// LineCollection LineCollection::operator =( const LineCollection &xLineCollection ) {         this->quantity = xLineCollection.quantity;         for ( int i = 0 ; i < this->quantity ; i++ )                 ( this->lc )[ i ] = ( xLineCollection.lc )[ i ];         return ( *this ); } LineCollection &LineCollection::operator +( const Linea &xLinea ) {         (*this)[ this->quantity ] = xLinea;         (this->quantity)++;         return ( *this ); } ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// bool LineCollection::isFull() const {         return ( this->quantity == MAX ); } bool LineCollection::isEmpty() const {         return ( this->quantity == 0 ); } ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// bool LineCollection::includes(const Linea &xLinea) const {         bool thisOne = false;         for ( int i = 0 ; i < this->quantity && ! thisOne ; i++ )         {                 if ( lc[i] == xLinea )                         thisOne = true;         }         return ( thisOne ); } int LineCollection::indexOF( const Linea &xLinea ) const {         int index = -1;         for ( int i = 0 ; i < this->quantity && index == -1 ; i++ )         {                 if ( lc[i] == xLinea )                         index = i;         }         return ( index ); }```
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////

Page.h

Code:

```#pragma once #include "PointCollection.h" #include "LineCollection.h" const int MAXPAGE = 100; class Page { private:         int heigh;         int width; public:         Page( void );         Page( int width, int heigh );         Page( const Page &xPage );         ~Page( void );         void setWidth( int width );         int getWidth() const;         void setHeigh( int heigh );         int getHeigh() const;         bool operator == ( const Page &xPage ) const;         void draw() const; };```
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////

Page.cpp

Code:

```#include "Page.h" Page::Page( void ) {         this->width = 0;         this->heigh = 0; } Page::Page( int width, int heigh ) {         this->width = width;         this->heigh = heigh; } Page::Page( const Page &xPage ) {         this->width = xPage.width;         this->heigh = xPage.heigh; } Page::~Page( void ) {} ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// void Page::setHeigh( int heigh ) {         this->heigh = heigh; } void Page::setWidth( int width ) {         this->width = width; } int Page::getHeigh() const {         return ( this->heigh ); } int Page::getWidth() const {         return ( this->width ); } ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// void Page::draw() const {         bool included;         PointCollection pc;         LineCollection li;         for( int y = 0 ; y < this->heigh ; y++ )         {                 included = false;                 for ( int x = 0; x < this->width ; x++ )                 {                         for ( int p = 0 ; p < pc.size()  && !included ; p++ )                         {                                 if ( pc[ p ].includes( x, y ) )                                 {                                         pc[ p ].draw();                                         included = true;                                 }                                 if ( included )                                         cout << ' ';                         }                         for ( int line = 0 ; line < li.size()  && !included ; line++ )                         {                                 if ( li[ line ].includes( x, y ) )                                 {                                         li[ line ].draw();                                         included = true;                                 }                                 if ( included )                                         cout << ' ';                         }                 }                 cout << endl;         } } ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// bool Page::operator ==( const Page &xPage ) const {         bool isEquality = false;         if( this->heigh == xPage.heigh && this->width == xPage.width )     {                 isEquality = true;     }     return isEquality; }```

Main.cpp

Code:

```#include "Page.h" int main () {                 Point p1( 5, 5 ), p2( 1, 1 );         Linea L;         Page pg( 15, 15 );         p1.draw();         pg.draw();         /*         if ( p1.includes( 1, 1 ) == true )                 cout << "Point 1 & 2 are Equal" << "\n\n";         else                 cout << "Point 1 & 2 are NOT Equal" << "\n\n";         */         system("PAUSE");         return 0; }```

Code:

``` ............... .  .      .  . .      .      . .  .......  . ...............                   .                 .                 .               .               .             . I made the calculations needed to do so: Depending on the coordinates the line would be printed. for example L( 1, 1 ) will print a Horizontal line of dots.```
Program Compiles.

The PROBLEM: When i print the line or dot. It just pritns consecutive dots in the page not how it's supposed to be in the page's coordinates.
• 12-21-2007
Codeplug
>> p1.draw();
>> pg.draw();

So what's the problem with the code you posted? Those two lines are doing exactly what they've been coded up to do.

gg
• 12-21-2007
anon
I'm not sure if it makes sense for Point and Line to have a Draw method. With standard C++ output you can't choose a location on the screen where the dot should be printed.

I would rather make Point and Line purely geometrical object (line's a collection of points, which - since you have only 4 available orientation - could be defined by the starting location, orientation and number of dots).

Then it would be solely the Page's task to take all the points and lines and place them on the screen. For that it should be quite simple to use a 2D array. First loop over all the points in the page and mark them in the array. Then print the array.

As to your Page, where does it get the data what to print? Shouldn't you pass a PointCollection and a LineCollection to it (for those you might use std::vector and various algorithms - no need to write your own class)?

Some other notes are that you don't need your own assignment operator and copy constructor if you are simply doing a member-wise copy (shallow copy). The compiler would generate these automatically for you.

Code:

```bool Point::includes( int cord_X, int cord_Y ) const {         return ( this->x == cord_X && this->y == cord_Y ); } bool Point::operator == ( const Point &xPoint ) const {     bool isEquality = false;     if( this->x == xPoint.x && this->y == xPoint.y )     {         isEquality = true;     }     return isEquality; }```
These functions seem to do exactly the same thing. How comes one is so simple and the other so complicated? If you really want two functions that do exactly the same thing, you might consider implementing one in terms of the other (call the other one and define the logic only once). But includes sounds really strange for a Point. A Point is the smallest thing there is, how can it include something else?
• 12-21-2007
Max_Payne
Quote:

Originally Posted by anon
I'm not sure if it makes sense for Point and Line to have a Draw method. With standard C++ output you can't choose a location on the screen where the dot should be printed.

lol. a 6 year old kid can do that.

You didn't seem to have understood my problem.

Point p( 1, 2 )

1, 2 are x, y coordinates on the page.

I don't know how to print that coordinate on the page. That's my problem.
• 12-21-2007
nga_tom
I think somewhere along the line you forgot to map your points and lines to a physical device or to a buffer that maps to a physical device - without that you have no spatial reference to the screen or paper.

tom
• 12-21-2007
Salem
• 12-21-2007
brewbuck
Quote:

Originally Posted by Max_Payne
lol. a 6 year old kid can do that.

You didn't seem to have understood my problem.

Point p( 1, 2 )

1, 2 are x, y coordinates on the page.

I don't know how to print that coordinate on the page. That's my problem.

No, the problem is that the coordinate (1, 2) is meaningless. With respect to what point? Is (0,0) at the lower left corner of the page? The upper left? The upper right? Does the coordinates increase upward, downward, left, right? You have defined NONE of this.

The coordinate (1, 2) is as meaningless as the location "23rd and Ash Street." What the city are you talking about?
• 12-21-2007
Max_Payne
Quote:

Originally Posted by Salem

OK thanks 4 the advice but i posted this on 3 forums that i currently go and i posted them in the right place.
This way i get to see as many opinions as i can from many users. It's great 4 learning purposes. It's not considered spam as you referred to it.
• 12-21-2007
anon
Quote:

Originally Posted by Max_Payne
lol. a 6 year old kid can do that.

You didn't seem to have understood my problem.

Point p( 1, 2 )

1, 2 are x, y coordinates on the page.

I don't know how to print that coordinate on the page. That's my problem.

I think that you didn't understand me. I said you can't do something. You say a 6 year old kid can do it. Then you say that you can't do it. That makes you - 5? :)

I meant with cout you cannot print a single '.' in an arbitrary location in the console window, so there's no point pretending that your Points and Lines can print themselves.

What I suggested:
1) Page creates a 2d array (of space characters).
2) Page asks each point where it is (optionally what character should be printed for that point) and sets a character in the appropriate place in the array (after checking bounds - may-be the point doesn't fit in the page).
3) Page prints the array.

Or you could indeed use OS specific code to move the cursor where the next point goes and write it there. But a Point only knows an offset from some point but not where this corner point is supposed to be. That would be up to the Page (e.g are there blank margins and how wide).
• 12-21-2007
brewbuck
Quote:

Originally Posted by Max_Payne
OK thanks 4 the advice but i posted this on 3 forums that i currently go and i posted them in the right place.
This way i get to see as many opinions as i can from many users. It's great 4 learning purposes. It's not considered spam as you referred to it.

Cross posting is spam. Why? It wastes everybody's time. If you post on three boards and get three identical answers, two of those answers were redundant. That means two people wasted their time by answering your question, because you already got the answer somewhere else.

While it might seem convenient to throw your question out across the big wide Internet and reel in as much data as possible, remember that we are not computer programs, but actual people who invest actual time by helping you. Don't render everybody's efforts pointless. Ask you question to a direct, specific audience, and if they can't help you, move on to somewhere else.
• 12-21-2007
Max_Payne
Quote:

Originally Posted by brewbuck
Cross posting is spam. Why? It wastes everybody's time. If you post on three boards and get three identical answers, two of those answers were redundant. That means two people wasted their time by answering your question, because you already got the answer somewhere else.

While it might seem convenient to throw your question out across the big wide Internet and reel in as much data as possible, remember that we are not computer programs, but actual people who invest actual time by helping you. Don't render everybody's efforts pointless. Ask you question to a direct, specific audience, and if they can't help you, move on to somewhere else.

OK and I thank everyone for the help and their time. But with all do respect: Who are You the Internet Police?

Who told i get Identical responses? I always get different answers from each forum, People think different ways, a program can be done in different ways, which at the end will help me learn more.

Dude just let it go. If you don't want to help then don't waste your time on me. That's all it takes. I still going to post my questions on these forums that i always visit. & Stop referring to it as Spam. Let it go. I'm sure you have better things to do with your life.
• 12-21-2007
anon
Well, to do you justice, your question at daniweb was not quite the same, although it looks similar.

Anyhow, try not to become like this guy for example.
• 12-21-2007
brewbuck
Quote:

Originally Posted by Max_Payne
OK and I thank everyone for the help and their time. But with all do respect: Who are You the Internet Police?

I'm not the Internet police, but if I spend 10 minutes typing a reply that you've already seen on some other forum, will I be mad? You bet.

Quote:

Who told i get Identical responses?
Nobody did. That's the whole freaking problem. I have no idea, because you have posted your questions in various places I don't know about. So why should I spend 30 minutes looking at a problem when there's a good chance somebody else is already trying to help you?

Apparently you are too selfish to see how absolutely rude that is.

Quote:

I always get different answers from each forum, People think different ways, a program can be done in different ways, which at the end will help me learn more.
So, the only thing that matters is what YOU learn, and who gives a crap what other people have to go through to teach it to you? Interesting attitude.
• 12-21-2007
Max_Payne
Quote:

Originally Posted by anon
Well, to do you justice, your question at daniweb was not quite the same, although it looks similar.

Anyhow, try not to become like this guy for example.

Of course. I only post the big projects on various forums. Just when i think it that different opinions are needed to help me understand more. Trust me i will never become that guy.
• 12-21-2007
Salem
Put it this way.

1. What makes your post so damn important that it needs to be seen by as many people as possible as soon as possible?
That's right, posting on multiple forums may seem like a good idea to you, but to people who actually help on forums, it looks like a cry of urgency.

2. What if every single person decided to be as unselfish as you, and post on multiple forums (glares at george2 in particular). Pretty soon, the situation would deteriorate into everyone cross-posting 10 or 20 times just to stand a change of being heard in the noise of everyone elses spamming. At that point, all the helpers would ........ off because the forum would have become unusable.

> If you don't want to help then don't waste your time on me.
Just as you're free to keep spamming the boards, I'm just as free to keep complaining at your unselfish behaviour, and generally not helping you (or anyone else during that time).
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last