Drawing Geometric Shapes on a TFT Display, Using the SSD1289 as an Example.

In the previous article we learned to display characters and strings on a TFT display; in this one we'll learn to draw geometric shapes. There aren't that many geometric shapes useful for building a graphical interface; the main ones are the rectangle and the circle, and those are what we'll learn to draw, in two variants, filled and unfilled. I'll say right away that the article will describe in detail the drawing algorithms for only some geometric shapes, which should be enough to understand the general principles of building a raster image. Let's start with the simplest shape: a filled rectangle.
From the previous article we remember that if you set the coordinates of a point and then simply send colors, the SSD1289 will fill points by itself according to the algorithm selected during initialization. But there is one peculiarity: the controller moves to the next line only when it reaches the end of the current line.
The following registers will help us draw a filled rectangle.
Drawing Geometric Shapes on a TFT Display, Using the SSD1289 as an Example.

With these registers we can set the start and end of the area we're going to write to; then in a loop we send the color the required number of times, and the controller does everything itself according to the algorithm set during initialization. But now it will move to the next line when it reaches the end of the area we specified.
One register, R44, is used to set the boundaries of the area along X, and two registers, R45 and R46, along Y. Let's put the above into a function; for convenience, we'll move the code that selects the working area into a separate function, Set_Work_Area().

void Set_Work_Area(uint16_t y1, uint16_t x1, uint16_t x2, uint16_t y2)
{
	
	Lcd_Write_Reg(0x0044,((x2 << 8) | x1));
	Lcd_Write_Reg(0x0045,y1);
	Lcd_Write_Reg(0x0046,y2);
	Set_Cursor(x1, y1);
}
///////////////////////////////////////
void Draw_Area(uint16_t left, uint16_t top, uint16_t right, uint16_t bottom, uint16_t color)
{
	register uint16_t x,y;
	Set_Work_Area(left, top, right, bottom);
	
	for(y=top; y<=bottom; y++)
	{
		for(x=left; x<=right; x++)
		{
			Lcd_Write_Data(color);
	        }
	}	
	Set_Work_Area(0, 0, 319, 239);
}

Drawing Geometric Shapes on a TFT Display, Using the SSD1289 as an Example.

Now that we've learned to draw a filled rectangle, let's try to draw an unfilled one. But for that we first need to learn to draw lines. With the knowledge we already have, we can easily draw a horizontal or vertical line, but not a line at an angle to the horizontal. To draw a line at an angle we'll use Bresenham's raster algorithm, or rather a modification of it. The thing is, the original algorithm involves division and floating-point operations, which we'd like to avoid when writing microcontroller firmware. You can read how to do this on Wikipedia.
In short, Bresenham's algorithm works like this: we take a segment with starting coordinates x and y. In a loop we add one to x in the direction of the end of the segment, and at each step we calculate the error, the distance between the real coordinate at this point and the nearest grid cell. If the error doesn't exceed half the cell height, we fill the cell.

Drawing Geometric Shapes on a TFT Display, Using the SSD1289 as an Example.

In the picture above, the line before rasterization is shown in yellow, and the distances to the centers of the nearest cells in green and red.
And here is the code for drawing a line.

void Draw_Line (uint8_t size,uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,uint16_t color)
{
	int deltaX = abs(x2 - x1);
	int deltaY = abs(y2 - y1);
	int signX = x1 < x2 ? 1 : -1;
	int signY = y1 < y2 ? 1 : -1;
	int error = deltaX - deltaY;
	
	for (;;)
	{
		Draw_Point(size,x1,y1,color);
		
		if(x1 == x2 && y1 == y2)
		break;
		
		int error2 = error * 2;
		
		if(error2 > -deltaY)
		{
			error -= deltaY;
			x1 += signX;
		}
		
		if(error2 < deltaX)
		{
			error += deltaX;
			y1 += signY;
		}
	}
}

To draw a rectangle we'll need 4 lines; for convenience, we'll move drawing horizontal and vertical lines into separate functions,

void Draw_Horizont_Line(uint8_t size,uint16_t x1,uint16_t y1,uint16_t y2,uint16_t color)
{
	Draw_Line( size, x1, y1, x1, y2, color);
}
///////////////////////////////
void Draw_Vertical_Line(uint8_t size,uint16_t x1,uint16_t x2,uint16_t y1,uint16_t color)
{
	Draw_Line( size, x1, y1, x2, y1, color);
}
///////////////////////////////
void Draw_Reactangle(uint8_t size,uint16_t left,uint16_t top,uint16_t right,uint16_t bottom,uint16_t color)
{
	Draw_Horizont_Line( size, top, left, right, color);
	Draw_Horizont_Line( size, bottom, left, right, color);
	Draw_Vertical_Line( size, top, bottom, left, color);
	Draw_Vertical_Line( size, top, bottom, right, color);
		
}

Drawing Geometric Shapes on a TFT Display, Using the SSD1289 as an Example.

And to draw a triangle you only need 3 lines)))

void Draw_Triangle( uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t x3, uint16_t y3, uint8_t size, uint16_t color)
{
	Draw_Line(size, x1, y1, x2, y2, color);
	Draw_Line(size, x2, y2, x3, y3, color);
	Draw_Line(size, x3, y3, x1, y1, color);
}

Drawing Geometric Shapes on a TFT Display, Using the SSD1289 as an Example.

A circle can also be drawn using Bresenham's algorithm. At each step of the algorithm, three points are considered, and the best one is found by comparing the distance from the center of the circle to the selected point with the circle's radius.

void Draw_Circle (uint8_t size,uint16_t x0,uint16_t y0,uint16_t radius,uint16_t color)
{
	int x = 0;
	int y = radius;
	int delta = 2 - 2 * radius;
	int error = 0;
	while(y >= 0)
	{
		Draw_Point(size,x0 + x, y0 + y,color);
		Draw_Point(size,x0 + x, y0 - y,color);
		Draw_Point(size,x0 - x, y0 + y,color);
		Draw_Point(size,x0 - x, y0 - y,color);
		error = 2 * (delta + y) - 1;
		if(delta < 0 && error <= 0)
		{
			++x;
			delta += 2 * x + 1;
			continue;
		}
		error = 2 * (delta - x) - 1;
		if(delta > 0 && error > 0)
		{
			--y;
			delta += 1 - 2 * y;
			continue;
		}
		++x;
		delta += 2 * (x - y);
		--y;
	}
}

Drawing Geometric Shapes on a TFT Display, Using the SSD1289 as an Example.

Toward the end of writing the article I found a function that draws filled and unfilled circles; the fill argument controls this.

void Draw_Circle1(unsigned int x,unsigned int y,char radius,char fill, char size, unsigned int color)
{
	int a_,b_,P;
	a_ = 0;
	b_ = radius;
	P = 1 - radius;
	while (a_ <= b_)
	{
		if(fill == 1)
		{
                        Draw_Area(y-a_,x-b_,y+a_,x+b_,color);
			Draw_Area(y-b_,x-a_,y+b_,x+a_,color);
		}
		else
		{
			Draw_Point(size, a_+x, b_+y, color);
			Draw_Point(size, b_+x, a_+y, color);
			Draw_Point(size, x-a_, b_+y, color);
			Draw_Point(size, x-b_, a_+y, color);
			Draw_Point(size, b_+x, y-a_, color);
			Draw_Point(size, a_+x, y-b_, color);
			Draw_Point(size, x-a_, y-b_, color);
			Draw_Point(size, x-b_, y-a_, color);
		}
		if (P < 0 )
		{
			P = (P + 3) + (2* a_);
			a_ ++;
		}
		else
		{
			P = (P + 5) + (2* (a_ - b_));
			a_ ++;
			b_ --;
		}
	}
}
////////////////////////////////////

Drawing Geometric Shapes on a TFT Display, Using the SSD1289 as an Example.

That's probably all I wanted to say about drawing geometric shapes, and in the next article we'll find out how a resistive touch screen works.
The link to the code written for the SSD1289 articles is here.
The Atmega16 project for Atmel Studio 6.2 is in the archive ssd1289_i8080_geometric.rar