[ create a new paste ] login | about

Link: http://codepad.org/7KeZyfAm    [ raw code | output | fork ]

C++, pasted on Mar 14:
// 0<m<1
void Bresenham_Line1(int x, int y, int dX, int dY, int x1, int y1, int x2, int y2, SDL_Renderer *ren)
{
	if (x1 > x2)
	{
		int q = x1;
		x1 = x2;
		x2 = q;
	}
	int p = 2 * dY - dX;
	SDL_RenderDrawPoint(ren, x, y);
	{
		while (x < x2 || x == x2)
		{
			if (p > 0)
			{
				x++; y++; p += 2 * dY - 2 * dX;
			}
			else
			{
				x++; p += 2 * dY;
			}
			SDL_RenderDrawPoint(ren, x, y);
		}
	}
}

//m>1
void Bresenham_Line2(int x, int y, int dX, int dY, int x1, int y1, int x2, int y2, SDL_Renderer *ren)
{
	if (y1 > y2)
	{
		int q = y1;
		y1 = y2;
		y2 = q;
	}
	int p = 2 * dX - dY;
	SDL_RenderDrawPoint(ren, x, y);
	while (y < y2 || y == y2)
	{
		if (p > 0)
		{
			x++; y++; p += 2 * dX - 2 * dY;
		}
		else
		{
			y++; p += 2 * dX;
		}
	}
}

//-1<m<0
void Bresenham_Line3(int x, int y, int dX, int dY, int x1, int y1, int x2, int y2, SDL_Renderer *ren)
{
	if (x1 > x2)
	{
		int q = x1;
		x1 = x2;
		x2 = q;
	}
	int p = -2 * dY - dX;
	SDL_RenderDrawPoint(ren, x, y);
	while (x < x2 || x == x2)
	{
		if (p > 0)
		{
			x++; y--; 
			p += -2 * dX - 2 * dY;
		}
		else
		{
			x++;
			p += -2 * dY;
		}
	}
}

//m<-1
void Bresenham_Line4(int x, int y, int dX, int dY, int x1, int y1, int x2, int y2, SDL_Renderer *ren)
{
	if (y1 < y2)
	{
		int q = y1;
		y1 = y2;
		y2 = q;
	}
	int p = -2 * dX - dY;
	SDL_RenderDrawPoint(ren, x, y);
	while (y > y2 || y == y2)
	{
		if (p > 0)
		{
			x++; y--;
			p += -2 * dX - 2 * dY;
		}
		else
		{
			y--;
			p += -2 * dX;
		}
	}
}
void Bresenham_Line(int x1, int y1, int x2, int y2, SDL_Renderer *ren)
{
	int dX = x2 - x1, dY = y2 - y1, x = x1, y = y1;
	float m = float(dY) / dX;
	if (m > 0 && m < 1)
		Bresenham_Line1(x, y, dX, dY, x1, y1, x2, y2, ren);
	else
	{
		if (m > 1)
			Bresenham_Line2(x, y, dX, dY, x1, y1, x2, y2, ren);
		else
		{
			if (m > -1 && m < 0)
				Bresenham_Line3(x, y, dX, dY, x1, y1, x2, y2, ren);
			else
				Bresenham_Line4(x, y, dX, dY, x1, y1, x2, y2, ren);
		}
	}
}


Output:
1
2
Line 2: error: 'SDL_Renderer' has not been declared
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: