三角形像素填充绘制算法 基于搭载ILI9341的嵌入式平台显示

开发使用的是搭载了ILI9341显示屏的指南者F103平台,除了用图片显示多边形之外,也可以直接进行绘制。

平时都是用SD卡直接显示图片,于是想到用它的像素点填充的功能直接绘制简单图形。例程里没有现成的三角形显示程序,于是自己做了一个。能够显示任意位置任意颜色的直角三角形(两直角边比例可以控制)
一共有四种显示模式,基本涵盖了所需的任何绘制的方向

	至于颜色,如果没有使用的必要,则在程序的
LCD_SetColors(triangle_colour,ILI9341_GetPointPixel(0,0));
											处进行一定的修改即可
/*
* @功能:TFT显示屏任意比例直角三角形绘制程序
* @代码量:190行,包含有效代码160余行
*	@参数:输入三角形定位坐标值(X,Y) 输入两个直角边的最小值(大小控制值)  输入方向状态模式  输入颜色  输入两直角边比例
*	@变量:start_point_x 、 start_point_y 、 waist_length 、 triangle_state 、triangle_colour、Bilateral_length_ratio
* @特别说明:本段程序由我另一个等腰三角形绘制程序(那个比较简单)改写而来,基本原理是XY的行扫和列扫,本段程序对于正整数倍的比例可以实现完全显示
*            如果输入的Bilateral_length_ratio(双直角边的比例)为正整数,则显示较为理想,如果采用小数倍,则只可完整显示
*	         1/2 、1/5、1/10的直角三角形,原因是由于浮点运算的问题
*						对于不同的设备显示的效果不同,就秉火原装的TFT显示屏由于屏幕大小限制,使用时注意最小值的合理设置
*	* 参考使用的例程中的程序为ILI9341_SetPointPixel,所以它基本就是很多的像素点进行算法填充
* @最后修改日期:2021年3月18日
* @修改人:YZE
*/
void ILI9341_DrawRightTriangle(uint16_t start_point_x,uint16_t start_point_y,uint16_t waist_length,uint8_t triangle_state,uint16_t triangle_colour,float Bilateral_length_ratio)
{	
	unsigned int pixelpointer_X,pixelpointer_Y,waist_location_x,waist_location_y,Repeat_draw_count;
	Repeat_draw_count = Bilateral_length_ratio;
	waist_location_x = waist_length+start_point_x;
	waist_location_y = waist_length+start_point_y;
	pixelpointer_X=start_point_x;
	pixelpointer_Y=start_point_y;
	LCD_SetColors(triangle_colour,ILI9341_GetPointPixel(0,0));
	if(triangle_state == 1)
	{
		if(Bilateral_length_ratio>=1)
		{
			waist_location_y = waist_length*Bilateral_length_ratio+start_point_y;
			while(pixelpointer_Y!=waist_location_y){
				while(Repeat_draw_count!=0){
					while((pixelpointer_X!=waist_location_x))
					{
						ILI9341_SetPointPixel(pixelpointer_X,pixelpointer_Y);
						pixelpointer_X++;
					}
					start_point_y++;
					pixelpointer_Y=start_point_y; //刷新像素针Y位置
					pixelpointer_X=start_point_x; //刷新像素针X位置
					Repeat_draw_count--;
				}
				start_point_x++;
				pixelpointer_X=start_point_x;
				Repeat_draw_count = Bilateral_length_ratio;
			}
		}
		else if(Bilateral_length_ratio < 1)
		{
			Bilateral_length_ratio = 1.0/Bilateral_length_ratio;
			waist_location_x = waist_length*1.0*Bilateral_length_ratio+1.0*start_point_x;
			pixelpointer_X=start_point_x;
			while(pixelpointer_Y!=waist_location_y){
				while((pixelpointer_X!=waist_location_x))
				{
					ILI9341_SetPointPixel(pixelpointer_X,pixelpointer_Y);
					pixelpointer_X++;
				}
				start_point_y++;
				pixelpointer_Y=start_point_y;
				start_point_x = start_point_x + Bilateral_length_ratio;
				pixelpointer_X=start_point_x;
			}
		}
	}
	else if(triangle_state == 2)
	{
		if(Bilateral_length_ratio>=1)
		{
			waist_location_x = waist_length*Bilateral_length_ratio+start_point_x;
			while(pixelpointer_X!=waist_location_x){
				while(Repeat_draw_count!=0){
					while((pixelpointer_Y!=waist_location_y))
					{		
						ILI9341_SetPointPixel(pixelpointer_X,pixelpointer_Y);
						pixelpointer_Y++;
					}
					start_point_x++;
					pixelpointer_Y=start_point_y; //刷新像素针Y位置
					pixelpointer_X=start_point_x; //刷新像素针X位置
					Repeat_draw_count--;
				}
				start_point_y++;
				pixelpointer_Y=start_point_y;
				Repeat_draw_count = Bilateral_length_ratio;
			}
		}
		else if(Bilateral_length_ratio < 1)
		{
			Bilateral_length_ratio = 1.0/Bilateral_length_ratio;
			waist_location_y = waist_length*1.0*Bilateral_length_ratio+1.0*start_point_y;
			pixelpointer_Y=start_point_y;
			while(pixelpointer_X!=waist_location_x){
				while((pixelpointer_Y!=waist_location_y))
				{
					ILI9341_SetPointPixel(pixelpointer_X,pixelpointer_Y);
					pixelpointer_Y++;
				}
				start_point_y = start_point_y + Bilateral_length_ratio;
				pixelpointer_Y=start_point_y;
				start_point_x++;
				pixelpointer_X=start_point_x;
			}	
		}
	}
	else if(triangle_state == 3)
	{
		waist_location_y = start_point_y; //对角重定位
		start_point_y = start_point_y + waist_length; //对角重定位
		if(Bilateral_length_ratio>=1)
		{
			waist_location_x = waist_length*Bilateral_length_ratio+start_point_x;
			while(pixelpointer_X!=waist_location_x){
				while(Repeat_draw_count!=0){
					while((pixelpointer_Y!=waist_location_y))
					{		
						ILI9341_SetPointPixel(pixelpointer_X,pixelpointer_Y);
						pixelpointer_Y--;
					}
					
					start_point_x++;
					pixelpointer_X=start_point_x;
					pixelpointer_Y=start_point_y;
					Repeat_draw_count--;
				}
				start_point_y--;
				pixelpointer_Y=start_point_y;
				Repeat_draw_count = Bilateral_length_ratio;
			}
		}
		else if(Bilateral_length_ratio < 1)
		{
			waist_location_y = start_point_y; //对角重定位
			start_point_y = start_point_y + waist_length*2; //对角重定位
			Bilateral_length_ratio = 1.0/Bilateral_length_ratio;
			pixelpointer_Y=start_point_y;
			pixelpointer_X=start_point_x;
			while(pixelpointer_X!=waist_location_x){
				while((pixelpointer_Y!=waist_location_y))
				{
					ILI9341_SetPointPixel(pixelpointer_X,pixelpointer_Y);
					pixelpointer_Y--;
				}
				start_point_y = start_point_y-Bilateral_length_ratio;
				pixelpointer_Y=start_point_y;
				start_point_x++;
				pixelpointer_X=start_point_x;
			}
		}
	}
	else if(triangle_state == 4)
	{
			waist_location_y = start_point_y; //对角重定位
			//start_point_y = start_point_y + waist_length; //对角重定位 //此处弃用
			pixelpointer_Y=start_point_y;//由于循环嵌套方式的改变,此语句用于冲出第一次循环判断
		
			if(Bilateral_length_ratio>=1)
			{
				start_point_y = waist_length*Bilateral_length_ratio+start_point_y; //多倍对角重定位
				pixelpointer_Y = start_point_y;
				while(pixelpointer_Y!=waist_location_Y){
					while(Repeat_draw_count!=0){
						while((pixelpointer_X!=waist_location_x))
						{
							ILI9341_SetPointPixel(pixelpointer_X,pixelpointer_Y);
							pixelpointer_X++;
						}
						start_point_y--;
						pixelpointer_X=start_point_x;
						pixelpointer_Y=start_point_y;
						Repeat_draw_count--;
					}
					start_point_x++;
					pixelpointer_X=start_point_x;
					Repeat_draw_count = Bilateral_length_ratio;
				}
			}
			else if(Bilateral_length_ratio < 1)
			{
				waist_location_y = start_point_y; //对角重定位
				start_point_y = start_point_y + waist_length; //对角重定位
				Bilateral_length_ratio = 1.0/Bilateral_length_ratio;
				waist_location_x = waist_length*Bilateral_length_ratio+start_point_x;
				pixelpointer_Y=start_point_y;
				pixelpointer_X=start_point_x;
				while(pixelpointer_Y!=waist_location_y){
					while((pixelpointer_X!=waist_location_x))
					{
						ILI9341_SetPointPixel(pixelpointer_X,pixelpointer_Y);
						pixelpointer_X++;
					}
					start_point_x = start_point_x+Bilateral_length_ratio;
					pixelpointer_X=start_point_x;
					start_point_y--;
					pixelpointer_Y=start_point_y;
				}

			}	
	}
}

在对应程序中使用即可
样例显示的效果如下
显示效果

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值