标签:
今天打算用STM32驱动TFTLCD屏显示显示一个画扇形的程序,这样就需要我们有一个画圆弧的程序,我尝试了很多方法,其中有一种方法就是使用三角函数来确定圆弧的点的坐标,即:
x=radius*cos(angle);
y=radius*sin(angle);
下面是当时的计算过程:我们先把角度变成弧度,我们在这里使用了ToRad()函数来实现,程序十分简单。
#include <math.h> #define pi 3.141592653f int shu; float ToRad(float degree) { return (degree*pi/180.0); }
知道了坐标之后一切就简单了,在后面我就开始使用下面的函数划线等。
LCD_DrawLine(roundx-radius, roundy, roundx+radius,roundy);
在观看效果时,由于需要的操作太多(上面的划线函数要进行数百次),这样不能实时显示当前外设的状态,通过百度得知,在使用sin等三角函数时,计算一次就需要很长时间(有网友通过示波器测出了准确时间),这位网友在论坛里提出一个建议,就是使用三角函数的查表法。
我们都知道脚函数的关系:
(正弦) Sin θ = 对边A / 斜边C (余弦) Cosθ = 邻边B / 斜边C (正切) Tanθ = 对边A / 邻边B
而在嵌入式系统中,如果直接用三角函数计算COS和SIN会占用大量机器周期,这里提供的数组可以用来立即查出COS和SIN值。
我获取函数结果的过程是使用程序,具体如下:
使用STM32单片机的USART串口功能将程序生成的函数结果打印到串口助手,而程序如下所示:
printf("\n\r www.ciast.net\n 以下是生成的三角函数值(sin,cos,tan),各361个,从0-360,开始@zgc261:\n"); for(shu=0;shu<361;shu++){ printf("%f",tan(ToRad(shu))); printf("%s",","); Delay_ms(10); }
我使用了循环语句循环打印0-360共361个值的三角函数结果,这里只有tan(),我们将其替换成其他三角函数即可,串口截图和最终结果数组如下:
最终数组,要是需要其他的可以自己生成即可,这里结果精确到小数点后6位。
sin[361]={0.000000,0.017452,0.034899,0.052336,0.069756,0.087156,0.104528,0.121869,0.139173,0.156434,0.173648,0.190809,0.207912,0.224951,0.241922,0.258819,0.275637,0.292372,0.309017,0.325568,0.342020,0.358368,0.374607,0.390731,0.406737,0.422618,0.438371,0.453991,0.469472,0.484810,0.500000,0.515038,0.529919,0.544639,0.559193,0.573576,0.587785,0.601815,0.615661,0.629320,0.642788,0.656059,0.669131,0.681998,0.694658,0.707107,0.719340,0.731354,0.743145,0.754710,0.766044,0.777146,0.788011,0.798636,0.809017,0.819152,0.829038,0.838671,0.848048,0.857167,0.866025,0.874620,0.882948,0.891007,0.898794,0.906308,0.913545,0.920505,0.927184,0.933580,0.939693,0.945519,0.951057,0.956305,0.961262,0.965926,0.970296,0.974370,0.978148,0.981627,0.984808,0.987688,0.990268,0.992546,0.994522,0.996195,0.997564,0.998630,0.999391,0.999848,1.000000,0.999848,0.999391,0.998630,0.997564,0.996195,0.994522,0.992546,0.990268,0.987688,0.984808,0.981627,0.978148,0.974370,0.970296,0.965926,0.961262,0.956305,0.951057,0.945519,0.939693,0.933580,0.927184,0.920505,0.913545,0.906308,0.898794,0.891006,0.882948,0.874620,0.866025,0.857167,0.848048,0.838671,0.829037,0.819152,0.809017,0.798635,0.788011,0.777146,0.766044,0.754710,0.743145,0.731354,0.719340,0.707107,0.694658,0.681998,0.669131,0.656059,0.642787,0.629320,0.615661,0.601815,0.587785,0.573576,0.559193,0.544639,0.529919,0.515038,0.500000,0.484810,0.469472,0.453990,0.438371,0.422618,0.406737,0.390731,0.374606,0.358368,0.342020,0.325568,0.309017,0.292372,0.275637,0.258819,0.241922,0.224951,0.207912,0.190809,0.173648,0.156434,0.139173,0.121869,0.104528,0.087156,0.069756,0.052336,0.034899,0.017452,-0.000000,-0.017453,-0.034900,-0.052336,-0.069756,-0.087156,-0.104528,-0.121869,-0.139173,-0.156435,-0.173648,-0.190809,-0.207912,-0.224951,-0.241922,-0.258819,-0.275638,-0.292372,-0.309017,-0.325568,-0.342020,-0.358368,-0.374607,-0.390731,-0.406737,-0.422618,-0.438371,-0.453991,-0.469472,-0.484810,-0.500000,-0.515038,-0.529919,-0.544639,-0.559193,-0.573577,-0.587785,-0.601815,-0.615661,-0.629320,-0.642788,-0.656059,-0.669131,-0.681998,-0.694659,-0.707107,-0.719340,-0.731354,-0.743145,-0.754710,-0.766045,-0.777146,-0.788011,-0.798635,-0.809017,-0.819152,-0.829038,-0.838671,-0.848048,-0.857167,-0.866025,-0.874620,-0.882948,-0.891007,-0.898794,-0.906308,-0.913545,-0.920505,-0.927184,-0.933580,-0.939693,-0.945519,-0.951057,-0.956305,-0.961262,-0.965926,-0.970296,-0.974370,-0.978148,-0.981627,-0.984808,-0.987688,-0.990268,-0.992546,-0.994522,-0.996195,-0.997564,-0.998630,-0.999391,-0.999848,-1.000000,-0.999848,-0.999391,-0.998630,-0.997564,-0.996195,-0.994522,-0.992546,-0.990268,-0.987688,-0.984808,-0.981627,-0.978148,-0.974370,-0.970296,-0.965926,-0.961262,-0.956305,-0.951056,-0.945519,-0.939693,-0.933580,-0.927184,-0.920505,-0.913545,-0.906308,-0.898794,-0.891006,-0.882948,-0.874620,-0.866025,-0.857167,-0.848048,-0.838671,-0.829038,-0.819152,-0.809017,-0.798635,-0.788011,-0.777146,-0.766045,-0.754709,-0.743145,-0.731354,-0.719340,-0.707107,-0.694658,-0.681998,-0.669131,-0.656059,-0.642787,-0.629320,-0.615661,-0.601815,-0.587785,-0.573576,-0.559193,-0.544639,-0.529919,-0.515038,-0.500000,-0.484809,-0.469471,-0.453991,-0.438371,-0.422618,-0.406736,-0.390731,-0.374606,-0.358368,-0.342020,-0.325568,-0.309017,-0.292372,-0.275638,-0.258819,-0.241922,-0.224951,-0.207911,-0.190809,-0.173648,-0.156434,-0.139173,-0.121869,-0.104529,-0.087156,-0.069756,-0.052336,-0.034900,-0.017452,0.000000}; cos[361]={1.000000,0.999848,0.999391,0.998630,0.997564,0.996195,0.994522,0.992546,0.990268,0.987688,0.984808,0.981627,0.978148,0.974370,0.970296,0.965926,0.961262,0.956305,0.951057,0.945519,0.939693,0.933580,0.927184,0.920505,0.913545,0.906308,0.898794,0.891007,0.882948,0.874620,0.866025,0.857167,0.848048,0.838671,0.829038,0.819152,0.809017,0.798635,0.788011,0.777146,0.766044,0.754710,0.743145,0.731354,0.719340,0.707107,0.694658,0.681998,0.669131,0.656059,0.642788,0.629320,0.615661,0.601815,0.587785,0.573576,0.559193,0.544639,0.529919,0.515038,0.500000,0.484810,0.469472,0.453990,0.438371,0.422618,0.406737,0.390731,0.374607,0.358368,0.342020,0.325568,0.309017,0.292372,0.275637,0.258819,0.241922,0.224951,0.207912,0.190809,0.173648,0.156434,0.139173,0.121869,0.104528,0.087156,0.069757,0.052336,0.034899,0.017452,-0.000000,-0.017452,-0.034899,-0.052336,-0.069757,-0.087156,-0.104529,-0.121869,-0.139173,-0.156434,-0.173648,-0.190809,-0.207912,-0.224951,-0.241922,-0.258819,-0.275637,-0.292372,-0.309017,-0.325568,-0.342020,-0.358368,-0.374607,-0.390731,-0.406737,-0.422618,-0.438371,-0.453991,-0.469472,-0.484810,-0.500000,-0.515038,-0.529919,-0.544639,-0.559193,-0.573576,-0.587785,-0.601815,-0.615661,-0.629320,-0.642788,-0.656059,-0.669131,-0.681999,-0.694658,-0.707107,-0.719340,-0.731354,-0.743145,-0.754710,-0.766045,-0.777146,-0.788011,-0.798636,-0.809017,-0.819152,-0.829038,-0.838671,-0.848048,-0.857167,-0.866025,-0.874620,-0.882948,-0.891007,-0.898794,-0.906308,-0.913545,-0.920505,-0.927184,-0.933580,-0.939693,-0.945519,-0.951057,-0.956305,-0.961262,-0.965926,-0.970296,-0.974370,-0.978148,-0.981627,-0.984808,-0.987688,-0.990268,-0.992546,-0.994522,-0.996195,-0.997564,-0.998630,-0.999391,-0.999848,-1.000000,-0.999848,-0.999391,-0.998630,-0.997564,-0.996195,-0.994522,-0.992546,-0.990268,-0.987688,-0.984808,-0.981627,-0.978148,-0.974370,-0.970296,-0.965926,-0.961262,-0.956305,-0.951057,-0.945519,-0.939693,-0.933580,-0.927184,-0.920505,-0.913545,-0.906308,-0.898794,-0.891007,-0.882948,-0.874620,-0.866025,-0.857167,-0.848048,-0.838671,-0.829038,-0.819152,-0.809017,-0.798635,-0.788011,-0.777146,-0.766044,-0.754710,-0.743145,-0.731354,-0.719340,-0.707107,-0.694658,-0.681998,-0.669131,-0.656059,-0.642787,-0.629320,-0.615662,-0.601815,-0.587785,-0.573576,-0.559193,-0.544639,-0.529919,-0.515038,-0.500000,-0.484810,-0.469472,-0.453990,-0.438371,-0.422618,-0.406737,-0.390731,-0.374606,-0.358368,-0.342020,-0.325568,-0.309017,-0.292372,-0.275637,-0.258819,-0.241922,-0.224951,-0.207911,-0.190809,-0.173648,-0.156435,-0.139173,-0.121869,-0.104528,-0.087156,-0.069756,-0.052336,-0.034899,-0.017452,0.000000,0.017453,0.034900,0.052336,0.069757,0.087156,0.104528,0.121870,0.139173,0.156435,0.173649,0.190809,0.207912,0.224951,0.241922,0.258819,0.275638,0.292372,0.309017,0.325568,0.342020,0.358368,0.374607,0.390731,0.406737,0.422618,0.438371,0.453991,0.469472,0.484810,0.500000,0.515038,0.529919,0.544639,0.559193,0.573577,0.587785,0.601815,0.615662,0.629320,0.642788,0.656059,0.669131,0.681998,0.694659,0.707107,0.719340,0.731354,0.743145,0.754710,0.766045,0.777146,0.788011,0.798636,0.809017,0.819152,0.829037,0.838671,0.848048,0.857167,0.866026,0.874620,0.882948,0.891007,0.898794,0.906308,0.913546,0.920505,0.927184,0.933581,0.939693,0.945519,0.951057,0.956305,0.961262,0.965926,0.970296,0.974370,0.978148,0.981627,0.984808,0.987688,0.990268,0.992546,0.994522,0.996195,0.997564,0.998630,0.999391,0.999848,1.000000}; tan[361]={0.000000,0.017455,0.034921,0.052408,0.069927,0.087489,0.105104,0.122785,0.140541,0.158384,0.176327,0.194380,0.212557,0.230868,0.249328,0.267949,0.286745,0.305731,0.324920,0.344328,0.363970,0.383864,0.404026,0.424475,0.445229,0.466308,0.487733,0.509525,0.531709,0.554309,0.577350,0.600861,0.624869,0.649408,0.674509,0.700208,0.726543,0.753554,0.781286,0.809784,0.839100,0.869287,0.900404,0.932515,0.965689,1.000000,1.035530,1.072369,1.110613,1.150369,1.191754,1.234897,1.279942,1.327045,1.376382,1.428148,1.482561,1.539865,1.600334,1.664280,1.732051,1.804048,1.880727,1.962611,2.050304,2.144507,2.246037,2.355853,2.475087,2.605089,2.747478,2.904211,3.077684,3.270853,3.487415,3.732052,4.010781,4.331476,4.704631,5.144556,5.671284,6.313751,7.115370,8.144348,9.514368,11.430045,14.300659,19.081174,28.636354,57.290038,-22877332.428856,-57.289751,-28.636282,-19.081142,-14.300641,-11.430033,-9.514360,-8.144342,-7.115365,-6.313752,-5.671282,-5.144553,-4.704626,-4.331474,-4.010779,-3.732049,-3.487414,-3.270852,-3.077683,-2.904211,-2.747478,-2.605088,-2.475086,-2.355852,-2.246036,-2.144506,-2.050304,-1.962610,-1.880726,-1.804047,-1.732051,-1.664280,-1.600334,-1.539865,-1.482561,-1.428148,-1.376381,-1.327045,-1.279942,-1.234897,-1.191754,-1.150369,-1.110612,-1.072368,-1.035530,-1.000000,-0.965688,-0.932515,-0.900404,-0.869287,-0.839099,-0.809784,-0.781286,-0.753554,-0.726542,-0.700208,-0.674508,-0.649407,-0.624869,-0.600861,-0.577350,-0.554309,-0.531709,-0.509525,-0.487733,-0.466308,-0.445229,-0.424475,-0.404026,-0.383864,-0.363970,-0.344328,-0.324920,-0.305731,-0.286745,-0.267949,-0.249328,-0.230868,-0.212556,-0.194380,-0.176327,-0.158384,-0.140541,-0.122784,-0.105104,-0.087489,-0.069927,-0.052408,-0.034921,-0.017455,0.000000,0.017455,0.034921,0.052408,0.069927,0.087489,0.105104,0.122785,0.140541,0.158385,0.176327,0.194380,0.212557,0.230868,0.249328,0.267949,0.286746,0.305731,0.324920,0.344328,0.363970,0.383864,0.404026,0.424475,0.445229,0.466308,0.487733,0.509526,0.531710,0.554309,0.577351,0.600861,0.624869,0.649407,0.674509,0.700208,0.726543,0.753554,0.781285,0.809784,0.839100,0.869287,0.900404,0.932515,0.965689,1.000000,1.035530,1.072369,1.110613,1.150369,1.191754,1.234897,1.279942,1.327044,1.376383,1.428148,1.482561,1.539866,1.600336,1.664278,1.732051,1.804048,1.880726,1.962612,2.050305,2.144507,2.246037,2.355852,2.475089,2.605090,2.747478,2.904211,3.077687,3.270850,3.487417,3.732052,4.010780,4.331483,4.704636,5.144558,5.671283,6.313749,7.115362,8.144362,9.514376,11.430056,14.300750,19.081075,28.636425,57.290325,-83858283.006684,-57.288682,-28.636014,-19.081067,-14.300647,-11.430053,-9.514374,-8.144328,-7.115361,-6.313748,-5.671267,-5.144557,-4.704624,-4.331473,-4.010780,-3.732044,-3.487410,-3.270850,-3.077682,-2.904210,-2.747478,-2.605087,-2.475085,-2.355852,-2.246034,-2.144507,-2.050302,-1.962610,-1.880726,-1.804048,-1.732049,-1.664280,-1.600334,-1.539865,-1.482561,-1.428147,-1.376381,-1.327044,-1.279941,-1.234897,-1.191754,-1.150368,-1.110612,-1.072369,-1.035529,-1.000000,-0.965688,-0.932515,-0.900404,-0.869286,-0.839099,-0.809784,-0.781285,-0.753554,-0.726543,-0.700207,-0.674509,-0.649407,-0.624869,-0.600861,-0.577350,-0.554309,-0.531709,-0.509525,-0.487732,-0.466307,-0.445228,-0.424474,-0.404026,-0.383864,-0.363970,-0.344327,-0.324920,-0.305731,-0.286746,-0.267949,-0.249328,-0.230868,-0.212556,-0.194380,-0.176327,-0.158384,-0.140540,-0.122785,-0.105104,-0.087489,-0.069927,-0.052408,-0.034921,-0.017455,0.000000};
[完 ] 选自:http://www.ciast.net/post/20151228.html CIAST.NET @zgc261
标签:
原文地址:http://www.cnblogs.com/zitech/p/5037387.html