C语言如何画图

Python015

C语言如何画图,第1张

framebuffer(帧缓冲)。

帧的最低数量为24(人肉眼可见)(低于24则感觉到画面不流畅)。

显卡与帧的关系:由cpu调节其数据传输速率来输出其三基色的配比。

三基色:RGB(红绿蓝)。

在没有桌面和图形文件的系统界面,可以通过C语言的编程来实现在黑色背景上画图!

用下面的代码,在需要的地方(有注释)适当修改,就能画出自己喜欢的图形!

PS:同样要编译运行后才能出效果。

#include <stdio.h>

#include <sys/mman.h>

#include <fcntl.h>

#include <linux/fb.h>

#include <stdlib.h>

#define RGB888(r,g,b) ((r &0xff) <<16 | (g &0xff) <<8 | (b &0xff))

#define RGB565(r,g,b) ((r &0x1f) <<11 | (g &0x3f) <<5 | (b &0x1f))

int main()

{

int fd = open("/dev/fb0", O_RDWR)

if(fd <0){

perror("open err. \n")

exit(EXIT_FAILURE)

printf("xres: %d\n", info.xres)

printf("yres: %d\n", info.yres)

printf("bits_per_pixel: %d\n", info.bits_per_pixel)

size_t len = info.xres*info.yres*info.bits_per_pixel >>3

unsigned long* addr = NULL

addr = mmap(NULL, len, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0)

if(addr == (void*)-1){

perror("mmap err. \n")

下面举一个用drawpoly()函数画箭头的例子。#include

#include

int main()

{

int gdriver, gmode, i

int arw[16]={200,102,300,102,300,107,330,<br/>100,300,93,300,98,200,98,200,102}

gdriver=DETECT

initgraph(&gdriver, &gmode, "c:\\caic\\bgi")

setbkcolor(BLUE)

cleardevice()

setcolor(12)/*设置作图颜色*/

drawpoly(8, arw)/*画一箭头*/

getch()

closegraph()

return 0

}

设定线型函数

在没有对线的特性进行设定之前,TURBO C 用其默认值,即一点宽的实线,但TURBO C 也提供了可以改变线型的函数。线型包括:宽度和形状。其中宽度只有两种选择:一点宽和三点宽。而线的形状则有五种。下面介绍有关线型的设置函数。

void far setlinestyle(intlinestyle,unsigned upattern,int thickness)该函数用来设置线的有关信息,其中linestyle是线形状的规定,

见下表:

有关线的形状(linestyle)

━━━━━━━━━━━━━━━━━━━━━━━━━

符号常数 数值 含义

─────────────────────────

SOLID_LINE 0 实线

DOTTED_LINE 1 点线

CENTER_LINE 2 中心线

DASHED_LINE 3 点画线

USERBIT_LINE 4 用户定义线

━━━━━━━━━━━━━━━━━━━━━━━━━

有关线宽(thickness)

thickness是线的宽度,见下表。

━━━━━━━━━━━━━━━━━━━━━━━━━

符号常数 数值 含义

─────────────────────────

NORM_WIDTH 1 一点宽

THIC_WIDTH 3 三点宽

━━━━━━━━━━━━━━━━━━━━━━━━━

对于upattern,只有linestyle选USERBIT_LINE 时才有意义 (选其它线型,uppattern取0即可)。此进uppattern的16位二进制数的每一位代表一个象元,如果那位为1,则该象元打开,否则该象元关闭。 void far getlinesettings(struct linesettingstypefar *lineinfo)该函数将有关线的信息存放到由lineinfo 指向的结构中,表中linesettingstype的结构如下:

 struct linesettingstype

 {

 int linestyle

 unsigned upattern

 int thickness

 }

我先给个例子,#include <graphics.h>

#include<stdlib.h>

#include<stdio.h>

#include "conio.h"

#include"math.h"

void main()

{

int gdriver=DETECT,gmode /*图形模式设定*/

char f

int m,n,x,y,t

int color

x=320y=240

m=0n=0

initgraph(&gdriver,&gmode,"c: \\tc")

printf("qweadzxc stand for eight direction,p to exit,and the number stand for color when paint\n")

printf("input anykey to start\n")

getch()

printf("PLEASE INPUT THE COLOR\n")

scanf("%d",&color)

loop:

while(!kbhit())

{

putpixel(x,y,color)

x=x+my=y+n

for(t=0t<=50t++)

delay(100)

}

f=getch()

if(f=='w'||f=='W')

{

m=0n=-1

goto loop

}

if(f=='x'||f=='X')

{

m=0n=1

goto loop

}

if(f=='a'||f=='A')

{

m=-1n=0

goto loop

}

if(f=='d'||f=='D')

{

m=1n=0

goto loop

}

if(f=='q'||f=='Q')

{

m=-1n=-1

goto loop

}

if(f=='e'||f=='E')

{

m=1n=-1

goto loop

}

if(f=='z'||f=='Z')

{

m=-1n=1

goto loop

}

if(f=='c'||f=='C')

{

m=1n=1

goto loop

}

if(f==' ')

{

m=0n=0

getch()

goto loop

}

if(f=='p'||f=='P')

goto end

if(f=='s'||f=='S')

{

printf("please input the color you want to set\n")

scanf("%d",&color)

goto loop

}

else

{

printf("ERROR!\n")

goto loop

}

end:

getch()

closegraph() /*退出图形模式*/

}