My professor gave use the code to a function that draws a line in the grid. I have made my class function but I am not sure how to use it with this function he gave us.

void drawLineInGrid(char grid[][MAX_Y], const Line &line)
{
	int xDifference, yDifference, xIncrement, yIncrement, error;
	int point1X = line.getX1(), point1Y = line.getY1(), point2X = line.getX2(), point2Y = line.getY2();
	// find the difference between the x & y coordinates of the points.
	xDifference = point2X - point1X;
	yDifference = point2Y - point1Y;
	// set the increment to 1 or -1 depending on if you need to count up/down/left/right to get from
	// point1X to point2X and point1Y to point2Y
	xIncrement = xDifference > 0 ? 1 : -1;
	yIncrement = yDifference > 0 ? 1 : -1;
	// get the absolute value of the differences
	xDifference = abs(xDifference);
	yDifference = abs(yDifference);
	if(xDifference >= yDifference) // printing mostly horizontal lines 
	{	
		yDifference <<= 1;
		error = yDifference - xDifference;  // how fast you move up/down
		xDifference <<= 1;
		while (point1X != point2X) 
		{
			grid[point1X][point1Y] = 'o';
			if(error >= 0) 
			{
				point1Y += yIncrement;
				error-= xDifference;
			}
			error += yDifference; 
			point1X += xIncrement;
		}
	} 
	else // printing more vertical (steeper) lines
	{   
		xDifference <<= 1;
		error = xDifference - yDifference; // how fast you move left/right
		yDifference <<= 1;
		while (point1Y != point2Y) 
		{
			grid[point1X][point1Y] = 'o';
			if(error >= 0) 
			{
				point1X += xIncrement;
				error -= yDifference;
			}
			error += xDifference; 
			point1Y += yIncrement;
		}
	}
	//grid[point1X][point1Y] = 'o'; // print last point (not needed because we put 'B' there.
	grid[line.getX1()][line.getY1()] = 'A'; // Should be global constants, but
	grid[line.getX2()][line.getY2()] = 'B'; // since I'm giving you just one function...
}

This is what I am trying to do for it to draw the first line but it is not working.

Line myLines[5];
drawLineInGrid(grid[][MAX_Y], Line &myLines[0])

The first parameter is a two dimenaional array of characters, declared something like this: char grid[20][MAX_Y]; . The second parameter is a single Line object passed by reference.

char grid[5][MAX_Y];
Line line;
drawLineInGrid(grid,line)

I assume you need to initialize the grid with some values before calling that function.

This article has been dead for over six months. Start a new discussion instead.