Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-21-2007, 01:07 PM
Max_Payne's Avatar   
Max_Payne Max_Payne is offline
Newbie
 
Join Date: Dec 2007
Posts: 1
Rep Power: 0
Max_Payne is on a distinguished road
Question [ C++ ] 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.

I have been stuck for a long time now. Please Help.




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;

}

Last edited by Max_Payne; 12-21-2007 at 01:23 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 01-21-2008, 02:24 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 793
Last Blog:
Programs Under the Hoo...
Rep Power: 13
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default

If I'm reading this right, you're doing this in a console application. You can't really draw all that well on a console application without changing the video mode, so lines will show up as a series of points.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-22-2008, 12:11 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,421
Last Blog:
wxWidgets is NOT code ...
Rep Power: 37
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default

C++ does not support the idea of locating a cursor position on the screen. As a result, you generally need to store ALL screen information in an array and redraw the array to screen to "refresh" the display.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-22-2008, 10:06 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 793
Last Blog:
Programs Under the Hoo...
Rep Power: 13
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default

I disagree. You can use WinAPI - SetConsoleCursorPosition() to change the cursor position, like so:

Code:
#include <windows.h>

HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD cursorLoc;

cursorLoc.X = myXCoord;
cursorLoc.Y = myYCoord;

SetConsoleCursorPosition(hOut,cursorLoc);
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-23-2008, 02:13 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,654
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

I think WingedPanther meant the language itself. C++ does not have any support for locating the cursor position. In your code example you're communicating with the Win32 API - which by the way is pure C code.
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 01-23-2008, 11:42 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,421
Last Blog:
wxWidgets is NOT code ...
Rep Power: 37
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default

Exactly, Void.

Granted, the WinAPI is extremely powerful and useful, but it isn't C/C++. This is where we start getting into code the works, but is not platform independent.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 01-23-2008, 10:18 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 793
Last Blog:
Programs Under the Hoo...
Rep Power: 13
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default

Right...I learned C first, never really made the full transition into C++...I still use printf and stuff. Sorry about that. But it does work, and Max_Payne never really mentioned multi-system compatibility, so it's a viable solution, just not exactly the way I phrased it.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Square root and calculator program!!! 123456789asdf C and C++ 10 12-02-2007 05:35 PM
! Need urgent help ! Drawing program using cygwin siren C and C++ 0 05-26-2007 11:51 PM
Need help w/ word count program (ASAP) siren C and C++ 1 04-23-2007 09:14 AM
How to modify a program written in .NET 2.0? jackyjack C# Programming 7 03-27-2007 01:26 PM


All times are GMT -5. The time now is 11:20 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 101%


Complete - Celebrate!

Ads