用C语言画图

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);
	}
	struct fb_var_screeninfo info; 

	if ( (ioctl(fd, FBIOGET_VSCREENINFO, &info)) < 0){
		perror("ioctl 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");
		exit(EXIT_FAILURE);
	}

	int i,j;
	for(i=0; i<768; i++){						//不同的电脑和系统像素不同,可以根据情况改变1366*768两个数的大小。
		for(j=0; j<1366; j++)
			//*(addr+i*1366+j) = RGB888(255, 0, 0);		//此处为画图位置,根据不同的数学公式,就可以给1366*768个像素上色。形成不一样的颜色!
			*(addr+i*1366+j) = RGB565(255, 0, 0);		//此为两种规格的像素。3个数分别带面红绿蓝的色彩深度。
	}	
	printf("hello main \n");

	int ret = munmap(addr, len);
	if(ret < 0){
		perror("munmap err. \n");
		exit(EXIT_FAILURE);
	}

	return 0;
}


  • 19
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
好的,如果要使用C语言绘制图表,可以使用GNU plot库。需要安装GNU plot库后,在C语言中使用`popen`函数来调用GNU plot命令。以下是一个简单的例子: ``` #include <stdio.h> int main() { FILE *gnuplot = popen("gnuplot", "w"); fprintf(gnuplot, "plot sin(x)\n"); fflush(gnuplot); getchar(); pclose(gnuplot); return 0; } ``` 这个例子会绘制正弦函数的图像,并在按下回车键后关闭GNU plot窗口。 我们可以根据上面Python的代码,将每个流接收到的总字节和时间输出到文件中,然后使用GNU plot绘制图表。以下是示例代码: ``` #include <stdio.h> #include <stdlib.h> struct PacketInfo { double timestamp; int flowID; int size; }; int main() { // 读取数据包信息 FILE *fp = fopen("packet_info.txt", "r"); if (fp == NULL) { perror("fopen"); exit(1); } struct PacketInfo packet; int flow_count = 0; int *flow_bytes = NULL; double *flow_time = NULL; while (fscanf(fp, "%lf %d %d\n", &packet.timestamp, &packet.flowID, &packet.size) == 3) { if (packet.flowID >= flow_count) { flow_count = packet.flowID + 1; flow_bytes = realloc(flow_bytes, flow_count * sizeof(int)); flow_time = realloc(flow_time, flow_count * sizeof(double)); for (int i = 0; i < flow_count; i++) { flow_bytes[i] = 0; flow_time[i] = -1; } } if (flow_time[packet.flowID] < 0) { flow_time[packet.flowID] = packet.timestamp; } flow_bytes[packet.flowID] += packet.size; } fclose(fp); // 输出到文件 fp = fopen("plot_data.txt", "w"); if (fp == NULL) { perror("fopen"); exit(1); } for (int i = 0; i < flow_count; i++) { if (flow_time[i] < 0) { continue; } fprintf(fp, "# Flow %d\n", i); int bytes = 0; for (double t = 0; t < flow_time[i] + 10; t += 0.1) { while (bytes < flow_bytes[i] && flow_time[0] + t >= flow_time[i] + (double)bytes / flow_bytes[i]) { bytes++; } fprintf(fp, "%lf %d\n", t, bytes); } fprintf(fp, "\n"); } fclose(fp); // 绘制图表 FILE *gnuplot = popen("gnuplot", "w"); fprintf(gnuplot, "set title 'Bytes Received'\n"); fprintf(gnuplot, "set xlabel 'Time (s)'\n"); fprintf(gnuplot, "set ylabel 'Total Bytes Received'\n"); fprintf(gnuplot, "plot 'plot_data.txt' every ::1 using 1:2 with lines title 'Flow 0'"); for (int i = 1; i < flow_count; i++) { fprintf(gnuplot, ", '' every ::%d using 1:2 with lines title 'Flow %d'", i * 2 - 1, i); } fprintf(gnuplot, "\n"); fflush(gnuplot); getchar(); pclose(gnuplot); // 释放内存 free(flow_bytes); free(flow_time); return 0; } ``` 这个例子会读取之前输出到文件中的数据包信息,然后输出到另一个文件中,并使用GNU plot绘制图表。需要注意的是,为了使每个流的图像不重叠,我们在每个流的数据之间添加了一个空行,这样GNU plot会自动将它们分开。另外,由于GNU plot不支持在图例中使用变量,我们需要手动为每个流设置一个标题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值