Public Group

# Drawing A Line In Console

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys, whatsup. Well I was asking a question on gotoxoy the other day and thanks for the help to anyone who answered it. I also found my older text book that had information on it. But now I realized I can't even use gotoxy for what Im trying to do. Im trying to draw a line in console with ASCII graphics. I have two points, the left point and the right point. Ive done some work on it but nothing seems to work just quite right I'll paste the code I've tried so far at the end of this. I made a 2D array of unsigned char to represent the buffer of the screen. The console screen being 23x79 so my points can be any where within that number. Anyways I would greatly appreciate any help on this problem, I look forward to your replies. Here is my code, and I would like to thank you for your help in advance.
void CLine::Render(unsigned char grid[gciScreenYSize][gciScreenXSize])const
{
int slope = 1;
if(m_right.col != m_left.col)
//int slope = (m_left.row - m_right.row)/(m_right.col - m_left.col);
slope = (m_right.col - m_left.col)/(m_left.row - m_right.row);
int iCol = m_left.col;
/*for(int i=m_left.col; i<m_right.col; i++){
for(int j=0; (slope >= 0)?(j<slope):(j>slope); (slope >= 0)?j++:j--)
grid[iRow] = m_cChar;
iRow += (slope < 0)?slope*-1:slope;
}*/
for(int j=m_left.row; (slope >= 0)?(j<=m_right.row):(j>=m_right.row); (slope >= 0)?j++:j--)
for(int i=0; (slope >= 0)?(i<=slope):(i>=slope); (slope >= 0)?i++:i--, iCol++)
grid[j][iCol] = m_cChar;
}


As I said none of that works, though I'm hoping im on my way. There is also a structure that is holding the points. SPoint, which the class has had set earlier. The character was also set earlier.

##### Share on other sites
A pretty good way to go about rendering lines is to use an algorithm like this:

It requires a collision detection routine. Extend an imaginary line from point A to point B, and highlight any parts of the grid that the line touches. The main thing you have to realize is that you must treat the console not as a grid of points, but as a grid of shapes.

##### Share on other sites
Well that doesn't quite make sense to me, but I'll look at it...

So far this is what I have, and it doesn't quite extend the line far enough, but it does draw a line.

 void CLine::Render(unsigned char grid[gciScreenYSize][gciScreenXSize])const{	//int slope = 1;	//if(m_right.col != m_left.col)	//	slope = (m_right.col - m_left.col)/(m_left.row - m_right.row);	//int iRow = m_left.row;	//for(int i=m_left.col; i<m_right.col; i++){	//	for(int j=0; (slope >= 0)?(j<slope):(j>slope); (slope >= 0)?j++:j--)	//		grid[iRow][i + ((j<0)?(j*-1):j)] = m_cChar;	//	iRow += (slope < 0)?slope*-1:slope;	//}	int slope = 0;	if(m_right.col != m_left.col)		slope = (m_right.col - m_left.col)/(m_left.row - m_right.row);	int iCol = m_left.col;	for(int i=m_left.row;(slope>=0)?(i>m_right.row):(i<m_right.row);(slope>=0)?i--:i++){		//for(int j=m_left.col;j<m_right.col;j+=(slope>=0)?slope:(-1*slope))			for(int m=0; (slope>=0)?(m<slope):(m<(slope*-1)); m++)				grid[iCol+m] = m_cChar;					iCol+=(slope>=0)?slope:(-1*slope);	}}

##### Share on other sites
You probably don't want to store the slope as an integer.

And you probably do want to [google] "Bresenham's algorithm".

##### Share on other sites
This might be of use to you (rather than using GotoXY, using a buffer of CHAR_INFOs and writing those to the console in one go). As far as a line drawing algorithm goes, Bresenham's line drawing routine is possibly the best one for this job. (See the bottom of the page).

##### Share on other sites
Actually, that's what Im doing. If you read, I said I realized I couldn't use gotoxy to do this.

I was using a buffer, I've found one of those algorithms to draw a line but Im still trying to figure out why mine doesn't work. I think I'm really close and want to get it working.

##### Share on other sites
Actually, that's what Im doing. If you read, I said I realized I couldn't use gotoxy to do this.

I was using a buffer, I've found one of those algorithms to draw a line but Im still trying to figure out why mine doesn't work. I think I'm really close and want to get it working.

1. 1
Rutin
34
2. 2
3. 3
4. 4
5. 5

• 12
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
633337
• Total Posts
3011415
• ### Who's Online (See full list)

There are no registered users currently online

×