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

The algorithms for drawing geometric shapes on a TFT display were covered earlier here, so in this article I'll just give the implementation of the functions for the ILI9341.

To fill the screen with the desired color in the previous article, we used the TFT_Fill_Screen() function; the same function can fill a rectangle or square of the required size if you set the coordinates of the working area correctly.

void TFT_Fill_Rectangle(uint16_t x, uint16_t y, uint16_t length, uint16_t width, uint16_t color)
{
	if(length  && width)
	{
		TFT_Fill_Screen(x, x+length - 1, y, y+width - 1, color);
	}
}

Someone may wonder where the -1 in the expression (x + length - 1) comes from: it's because when drawing a single pixel, the start and end of the working area are the same coordinate.

Lines are still drawn using Bresenham's algorithm.

void TFT_Draw_Line( uint16_t x0,uint16_t y0,uint16_t x1, uint16_t y1,uint16_t color)
{

	int x = x1-x0;
	int y = y1-y0;
	int dx = abs(x), sx = x0<x1 ? 1 : -1;
	int dy = -abs(y), sy = y0<y1 ? 1 : -1;
	int err = dx+dy, e2;
	for (;;){
		TFT_Draw_Pixel(x0,y0,color);
		e2 = 2*err;
		if (e2 >= dy) {
			if (x0 == x1) break;
			err += dy; x0 += sx;
		}
		if (e2 <= dx) {
			if (y0 == y1) break;
			err += dx; y0 += sy;
		}
	}
}


Horizontal and vertical lines can be used to draw a rectangle.

void  TFT_Draw_Horizontal_Line( uint16_t x, uint16_t y, uint16_t length,uint16_t color)
{
	TFT_Set_Column(x,x + length);
	TFT_Set_Page(y,y);
	TFT_Send_Cmd(0x2c);
	for(int i=0; i<length; i++)
	TFT_Write_Data16(color);
}

void TFT_Draw_Vertical_Line( uint16_t x, uint16_t y, uint16_t length,uint16_t color)
{
	TFT_Set_Column(x,x);
	TFT_Set_Page(y,y+length);
	TFT_Send_Cmd(0x2c);
	for(int i=0; i<length; i++)
	TFT_Write_Data16(color);
}

void TFT_Draw_Rectangle(uint16_t x, uint16_t y, uint16_t length, uint16_t width,uint16_t color)
{
	TFT_Draw_Horizontal_Line(x, y, length, color);
	TFT_Draw_Horizontal_Line(x, y + width-1, length, color);
	TFT_Draw_Vertical_Line(x, y, width,color);
	TFT_Draw_Vertical_Line(x + length -1, y, width,color);
}


A triangle is drawn just as easily.

void TFT_Draw_Triangle( uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t x3, uint16_t y3, uint16_t color)
{
	TFT_Draw_Line( x1, y1, x2, y2, color);
	TFT_Draw_Line( x2, y2, x3, y3, color);
	TFT_Draw_Line( x3, y3, x1, y1, color);
}


And finally, circles: outlined and filled.

void TFT_Draw_Circle(uint16_t pos_x, uint16_t pos_y, uint8_t r,uint16_t color)
{
	int x = -r, y = 0, err = 2-2*r, e2;
	do {
		TFT_Draw_Pixel(pos_x-x, pos_y+y,color);
		TFT_Draw_Pixel(pos_x+x, pos_y+y,color);
		TFT_Draw_Pixel(pos_x+x, pos_y-y,color);
		TFT_Draw_Pixel(pos_x-x, pos_y-y,color);
		e2 = err;
		if (e2 <= y) {
			err += ++y*2+1;
			if (-x == y && e2 <= x) e2 = 0;
		}
		if (e2 > x) err += ++x*2+1;
	} while (x <= 0);
}

void TFT_Fill_Circle(uint16_t pos_x, uint16_t pos_y, uint8_t r,uint16_t color)
{
	int x = -r, y = 0, err = 2-2*r, e2;
	do {

		TFT_Draw_Vertical_Line(pos_x-x, pos_y-y, 2*y, color);
		TFT_Draw_Vertical_Line(pos_x+x, pos_y-y, 2*y, color);

		e2 = err;
		if (e2 <= y) {
			err += ++y*2+1;
			if (-x == y && e2 <= x) e2 = 0;
		}
		if (e2 > x) err += ++x*2+1;
	} while (x <= 0);

}

Drawing Geometric Shapes on a TFT Display, Using the ILI9341 as an Example.
That's all; the Atmega16 project for Atmel Studio 6.2 is in the archive ili9341_i8080_geometric.rar.