Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,423 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,971 people online right now. Registration is fast and FREE... Join Now!




Classes & Collections - Printing dots, lines etc.. in specific loc

 
Reply to this topicStart new topic

Classes & Collections - Printing dots, lines etc.. in specific loc

Max_Payne
13 Jan, 2008 - 07:59 PM
Post #1

D.I.C Head
**

Joined: 19 Oct, 2007
Posts: 51


My Contributions
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 ) { x = x; }
    int getX() const { return x; }

    void setY( int y ) { y = y; }
    int getY() const { return y; }

    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 ) {} // Destructor

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


Page.h
CODE

#ifndef _PAGE
#define _PAGE

#include "PointCollection.h"
#include "LineCollection.h"

const int MAXPAGE = 100;

class Page
{
private:
    int heigh;
    int width;
    PointCollection mp_collection;

public:
    Page( void );
    Page( int width, int heigh );
    Page( const Page &xPage );
    ~Page( void );

    void setWidth( int width ) { this->width = width; }
    int getWidth() const { return (this->width); }

    void setHeigh( int heigh ) { this->heigh = heigh; }
    int getHeigh() const { return (this->heigh); }

    void addPoint(const Point & point)
    {
        mp_collection = mp_collection +  point;
    }

    void draw() const;
};

#endif




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 ) {} // Destructor


void Page::draw() const
{
    bool included;
    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 < mp_collection.size()  && !included; p++ )
            {

                if ( mp_collection[ p ].includes( x, y ) )
                {
                    mp_collection[ p ].draw();
                    included = true;
                }

                if ( !included )
                    cout << ' ';
            }
        }
        cout << endl;
    }
}




The Program prints the dot in the specific location but there's the problem:

If i use 1 point:
Main
CODE

#include "Page.h"

int main ()
{
    Page pg(65, 10);

    Point p1(1, 1);
    pg.addPoint(p1);

    pg.draw();

    system("PAUSE");
    return 0;
}


It prints the point normally: Here's a Screen shot of the exe:
IPB Image




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

If i use more than 1 point:
Main
CODE

#include "Page.h"

int main ()
{
    Page pg(65, 10);

    Point p1(1, 1);
    pg.addPoint(p1);

    Point p2(2, 2);
    pg.addPoint(p2);

    Point p3(3, 3);
    pg.addPoint(p3);

    pg.draw();

    system("PAUSE");
    return 0;
}


Point (1,1) changed it's location and the page changed it's size: Here's a Screen shot of the exe:
IPB Image

I think the problem is with this function.

CODE

void Page::draw() const
{
    bool included;
    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 < mp_collection.size()  && !included; p++ )
            {

                if ( mp_collection[ p ].includes( x, y ) )
                {
                    mp_collection[ p ].draw();
                    included = true;
                }

                if ( !included )
                    cout << ' ';
            }
        }
        cout << endl;
    }


User is offlineProfile CardPM
+Quote Post

Pontus
RE: Classes & Collections - Printing Dots, Lines Etc.. In Specific Loc
14 Jan, 2008 - 11:27 PM
Post #2

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 530



Thanked: 2 times
Dream Kudos: 275
My Contributions
Dont know what this means but it seams strange to use it in a for loop
CODE
for ( int p = 0; p < mp_collection.size()  && !included; p++ )

What is this supposed to mean?



*mod edit - pulled the bbcode tags out of the code block - they won't display right in there smile.gif
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Classes & Collections - Printing Dots, Lines Etc.. In Specific Loc
15 Jan, 2008 - 12:00 AM
Post #3

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
QUOTE(manhaeve5 @ 15 Jan, 2008 - 12:27 AM) *

Dont know what this means but it seams strange to use it in a for loop
CODE
for ( int p = 0; p < mp_collection.size()  && !included; p++ )

What is this supposed to mean?


The middle section of code in the for-loop control statement is the continutation condition for the loop. The loop will only continue to run (i.e. move on to the next iteration) if the statement given evaluates to true. Normally, you usually only see a single boolean expression in there. However, you can AND booleans down to a single true or false. In this case, the loop will only continue if both p < mp_collection.size() and !included evaluate to true. Otherwise, the inner loop will stop.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Classes & Collections - Printing Dots, Lines Etc.. In Specific Loc
15 Jan, 2008 - 04:53 AM
Post #4

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,047



Thanked: 106 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
The basic way to troubleshoot this is to make your whitespace visible, i.e. [li]cout << ' ';[/li] becomes [li]cout << '-';[/li]. I was going to suggest you move the includes detection to your PointCollection, but you already had one!

Forgive me, but your code is fascinating. Simultaneously some very good OO practices with some, well, not so good ones. e.g: Your Point class is way too busy. You'd do better to just make it hold x and y. You'd probably do better to make it a struct, actually.

Here's an example of a Page::draw that works, using only of the methods already defined.
CODE

void Page::draw() const {
    for( int y = 0; y < this->heigh; y++ ) {
        for ( int x = 0; x < this->width; x++ ) {
            Point pt(x, y);
            if (mp_collection.includes(pt)) {
                cout << '*';
            } else {
                cout << '.';
            }
        }
        cout << endl;
    }
}


Hope this helps.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 04:30AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month