#include<stdlib.h>
#include<stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include<linux/fb.h>
#include<string.h>
#include<fcntl.h>
#include<math.h>
#include<time.h>
#define pi 3.1415926
struct fb_var_screeninfo vinfo;
long int offset=0;
int fd;
long int screensize=0;
unsigned char *fbp=0;
int x;
int y;
void init()
{
fd=open("/dev/fb0",O_RDWR);
if(fd<0){
printf("cannot open framebuffer.");
exit(1);
}
if(ioctl(fd,FBIOGET_VSCREENINFO,&vinfo)){
printf("cannot read variable information.\n");
exit(1);
}
printf("varinfo-xres :%d\n",vinfo.xres);
printf("varinfo-yres :%d\n",vinfo.yres);
printf("varinfo-xresvirtual :%d\n",vinfo.xres_virtual);
printf("varinfo-yresvirtual :%d\n",vinfo.yres_virtual);
printf("varinfo-xoffset :%d\n",vinfo.xoffset);
printf("varinfo-yoffset :%d\n",vinfo.yoffset);
printf("varinfo-bits_per_pixel :%d\n",vinfo.bits_per_pixel);
screensize=vinfo.xres_virtual*vinfo.yres_virtual*vinfo.bits_per_pixel/8;
fbp=(unsigned char *)mmap(0,screensize,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if(fbp<0){
printf("cannot map.\n");
exit(1);
}
memset(fbp,0,screensize);
}
int draw_point(int x,int y,int r,int g,int b)
{
offset = (x+y * vinfo.xres_virtual)*vinfo.bits_per_pixel/8;
*(unsigned char *)(fbp+offset+0)=b;
*(unsigned char *)(fbp+offset+1)=g;
*(unsigned char *)(fbp+offset+2)=r;
*(unsigned char *)(fbp+offset+3)=0;
}
int draw_circle(int a,int c,int p,int r,int g,int b){
double t=0;
for(t=0;t<2*pi;t=t+0.03){
x=p*cos(t)+a;
y=p*sin(t)+c;
draw_point(x,y,r,g,b);
}
}
int draw_any_angle_circle(int a,int c, int p,double i,double j,int r, int g,int b){
double t=0;
for(t=i;t<j;t=t+0.03){
x=p*cos(t)+a;
y=p*sin(t)+c;
draw_point(x,y,r,g,b);
}
}
// ji zuo biao huan yuan
int draw_any_angle_round(int a,int c, int p,double i,double j,int r, int g,int b ){
double t=0;
for(t=i;t<j;t=t+0.03){
draw_line(a,c,p,t,r,g,b);}
}
// zhi jiao zuo biao draw round
int draw_round(int a,int c,int p,int r,int g,int b){
for(x=0;x<800;x++){
for(y=0;y<480;y++){
if(sqrt(pow(x-a,2)+pow(y-c,2))<p)
draw_point(x,y,r,g,b);
}
}
}
//huan yuan huan
int draw_round_ring(int a,int c,int p1,int p2,int r,int g,int b){
for(x=0;x<800;x++){
for(y=0;y<480;y++){
if(sqrt(pow(x-a,2)+pow(y-c,2))<p2){
draw_point(x,y,r,g,b);}
if(sqrt(pow(x-a,2)+pow(y-c,2))<p1){
draw_point(x,y,255-r,255-g,255-b);}
}
}
}
//draw_round xiao chu
int draw_round_del(int a,int c,int p,int r,int g,int b){
for(x=0;x<800;x++){
for(y=0;y<480;y++){
if(p<sqrt(pow(x-a,2)+pow(y-c,2)))
draw_point(x,y,r,g,b);
}
}
}
// chan sheng sui ji shu
int r(int t){
srand(time(0));
return rand()%t+1;
}
// draw line
int draw_line(int x1,int y1,int l,double s,int r,int g,int b){
for(l;l>0;l--){
x =l*cos(s)+x1;
y =l*sin(s)+y1;
draw_point(x,y,r,g,b);
}
}
void release(){
munmap(fbp,screensize);
close(fd);
}