Drawing A Line In Console

Started by
5 comments, last by Surg AKA Kunark 18 years, 9 months ago
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.
~~Johnathan~~LTM my new fad, know it, use it, love it, LAUGHING TO MYSELF
Advertisement
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.
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] = m_cChar;</span><br>	<span class="cpp-comment">//	iRow += (slope &lt; 0)?slope*-1:slope;</span><br>	<span class="cpp-comment">//}</span><br><br>	<span class="cpp-keyword">int</span> slope = <span class="cpp-number">0</span>;<br>	<span class="cpp-keyword">if</span>(m_right.col != m_left.col)<br>		slope = (m_right.col - m_left.col)/(m_left.row - m_right.row);<br><br>	<span class="cpp-keyword">int</span> iCol = m_left.col;<br><br>	<span class="cpp-keyword">for</span>(<span class="cpp-keyword">int</span> i=m_left.row;(slope&gt;=<span class="cpp-number">0</span>)?(i&gt;m_right.row):(i&lt;m_right.row);(slope&gt;=<span class="cpp-number">0</span>)?i–:i++){<br>		<span class="cpp-comment">//for(int j=m_left.col;j&lt;m_right.col;j+=(slope&gt;=0)?slope:(-1*slope))</span><br>			<span class="cpp-keyword">for</span>(<span class="cpp-keyword">int</span> m=<span class="cpp-number">0</span>; (slope&gt;=<span class="cpp-number">0</span>)?(m&lt;slope):(m&lt;(slope*-<span class="cpp-number">1</span>)); m++)<br>				grid<span style="font-weight:bold;">[iCol+m] = m_cChar;		<br>			iCol+=(slope&gt;=<span class="cpp-number">0</span>)?slope:(-<span class="cpp-number">1</span>*slope);<br>	}<br>}<br><br></pre></div><!–ENDSCRIPT–> 
~~Johnathan~~LTM my new fad, know it, use it, love it, LAUGHING TO MYSELF
You probably don't want to store the slope as an integer.

And you probably do want to [google] "Bresenham's algorithm".
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).

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

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.
~~Johnathan~~LTM my new fad, know it, use it, love it, LAUGHING TO MYSELF
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.
~~Johnathan~~LTM my new fad, know it, use it, love it, LAUGHING TO MYSELF

This topic is closed to new replies.

Advertisement