基本图形算法
画圆参考 http://blog.csdn.net/hgy2011/article/details/7604374
直线参考 http://blog.csdn.net/hgy2011/article/details/7604376
/***************************************************************************************************
LCD画图函数
文 件:LcdDraw.h
依 赖:GlDisPoint(x,y,c) 低层画点函数
编 者: 张永辉 2012年12月25日
***************************************************************************************************/
#ifndef __LCDDRAW__H
#define __LCDDRAW__H
//**************************************************************************************************
void LcdDrawCircle (int xc,int yc,int r,unsigned short color); //画圆
void LcdDrawCircleFast(int xc,int yc,int r,unsigned short color); //画圆
void LcdDrawLine(int x1, int y1, int x2, int y2,unsigned short color); //画线
//**************************************************************************************************
#endif
/***************************************************************************************************
LCD画图函数
文 件:LcdDraw.c
编 者:张永辉 2012年12月25日
所有的函数仅基于描点函数 GlDisPoint()其在外部实现
***************************************************************************************************/
#define __LCDDRAW__C
/**************************************************************************************************/
#include "LcdDraw.h"
#include "OpenglLcd.h"
void static Draw8points(int xc,int yc,int x,int y,u16 c);
int static abs(int a);
void static SwapInt(int *x, int *y);
//==================================================================================================
//--------------------------------------画圆函数----------------------------------------------------
//方法:改进的中点画圆法-Bresenham算法
//参数:xc,yc 圆心坐标
// radius 半径
// color 颜色
void LcdDrawCircle(int xc,int yc,int radius,u16 color)
{
int x,y,p;
x=0;
y=radius;
p=3-2*radius;
while(x<y)
{
Draw8points(xc,yc,x,y,color);
if(p<0)
{
p=p+4*x+6;
}else
{
p=p+4*(x-y)+10;
y-=1;
}
x+=1;
}
if(x==y)
{
Draw8points(xc,yc,x,y,color);
}
}
//方法:快速画圆
//参数:xc,yc 圆心坐标
// radius 半径
// color 颜色
void LcdDrawCircleFast(int xc , int yc , int r,u16 color)
{
int x, y, d;
x = 0;
y = r;
d = -r / 2;
Draw8points(xc , yc , x , y,color);
if(r % 2 == 0)
{
while(x < y)
{
x++;
if(d < 0)
d += x;
else
{
y--;
d += x - y;
}
Draw8points(xc , yc , x , y,color);
}
}else
{
while(x < y)
{
x++;
if(d < 0)
d += x + 1;
else
{
y--;
d += x - y + 1;
}
Draw8points(xc , yc , x , y,color);
}
}
}
//按8分法,每次调用将画出8个对称的点
void static Draw8points(int xc,int yc,int x,int y,u16 c)
{
GlDisPoint(xc+x,yc+y,c);
GlDisPoint(xc-x,yc+y,c);
GlDisPoint(xc+x,yc-y,c);
GlDisPoint(xc-x,yc-y,c);
GlDisPoint(xc+y,yc+x,c);
GlDisPoint(xc-y,yc+x,c);
GlDisPoint(xc+y,yc-x,c);
GlDisPoint(xc-y,yc-x,c);
}
//--------------------------------------画直线------------------------------------------------------
//方法:Bresenham算法
//参数:x1 y1 第一个点
// x2 y2 第二个点
// color 颜色
void LcdDrawLine(int x1, int y1, int x2, int y2,u16 color)
{
int dx,dy,p,const1,const2,x,y,inc;
int steep = (abs(y2 - y1) > abs(x2 - x1)) ? 1 : 0;
if(steep == 1)
{
SwapInt(&x1, &y1);
SwapInt(&x2, &y2);
}
if(x1 > x2)
{
SwapInt(&x1, &x2);
SwapInt(&y1, &y2);
}
dx = abs(x2 - x1);
dy = abs(y2 - y1);
p = 2 * dy - dx;
const1 = 2 * dy;
const2 = 2 * (dy - dx);
x = x1;
y = y1;
inc = (y1 < y2) ? 1 : -1;
while(x <= x2)
{
if(steep == 1)
GlDisPoint(y, x,color);
else
GlDisPoint(x, y,color);
x++;
if(p<0)
p += const1;
else
{
p += const2;
y += inc;
}
}
}
//绝对值函数
int static abs(int a)
{
if(a<0)
{
return -a;
}
return a;
}
//值交换函数
void static SwapInt(int *x, int *y)
{
int a = *x;
*x = *y;
*y = a;
}