如何用 C 语言画「心形」?

如何用 C 语言画「心形」?在我们 IT 行业每天面对的就是敲代码 所以很多人无法接受这份工作 因为很无聊也很枯燥 长期工作会使人情绪低落 其实我们编程很多时候也有有趣的地方 接下来我就用一个简单的 c 语言作图来缓解一下气氛 新的一年开始了 是时候作出改变了

大家好,欢迎来到IT知识分享网。

在我们IT行业每天面对的就是敲代码,所以很多人无法接受这份工作,因为很无聊也很枯燥,长期工作会使人情绪低落,其实我们编程很多时候也有有趣的地方,接下来我就用一个简单的c语言作图来缓解一下气氛。

新的一年开始了,是时候作出改变了。

以下为用C语言画心形的三种方式(附代码)

画心1

如何用 C 语言画「心形」?

如何用 C 语言画「心形」?

关于%*.*s

小数点.后“*”表示输出位数,具体的数据来自参数表

printf格式字符串中,与宽度控制和精度控制有关的常量都可以换成变量,方法就是使用一个“*”代替那个常量,然后在后面提供变量给“*”。

同样,小数点.前也可以添加*,也要用户输入一个位宽值来代替,表示输出的字符所占位宽。

也就是说,前面定义输出总宽度,后面定义输出字符个数。

printf(“%*.*s\n”, 50, 3, a); // 50表示此次输出占位宽,

//3表示输出a数组的三个字符

画心2

如何用 C 语言画「心形」?

如何用 C 语言画「心形」?

画心3

如何用 C 语言画「心形」?

Linux上通过framebuffer将jpeg图片画在屏幕上

安装JPEG库

1.解压jpeg源码 tar -xzvf jpegsrc.v8a.tar.gz

2.在/home/xxx下新建jpeg目录 mkdir jpeg

3.进入jpeg源码目录jpeg-8a cd jpeg-8a

4.生成makefile脚本 ./configure –prefix=/home/xxx/jpeg

5.编译 make

6.安装 make install

安装完毕后就可以在/home/xxx/jpeg目录下看到 jpeg解码库

配置JPEG库

如何用 C 语言画「心形」?

如何用 C 语言画「心形」?

#include <unistd.h>

#include <stdio.h>

#include <stdlib.h>

#include <fcntl.h>

#include <linux/fb.h>

#include <sys/mman.h>

#include <sys/ioctl.h>

#include <errno.h>

#include <string.h>

#include “jpeglib.h”

typedef struct Tag_RGB

{

unsigned char ucRed;

unsigned char ucGreen;

unsigned char ucBlue;

}St_RGB;

int main (int agrc, char *argv[])

{

int fp=0;

struct fb_var_screeninfo vinfo;

struct fb_fix_screeninfo finfo;

fp = open(“/dev/fb0”,O_RDWR);

if (fp < 0)

{

printf(“Error : Can not open framebuffer device\n”);

exit(EXIT_FAILURE);

}

if (ioctl(fp,FBIOGET_FSCREENINFO,&finfo))

{

printf(“Error reading fixed information\n”);

exit(EXIT_FAILURE);

}

if (ioctl(fp,FBIOGET_VSCREENINFO,&vinfo))

{

printf(“Error reading variable information\n”);

exit(EXIT_FAILURE);

}

printf(“The mem is :%d\n”, finfo.smem_len);

printf(“The line_length is :%d\n”, finfo.line_length);

printf(“The xres is :%d\n”, vinfo.xres);

printf(“The yres is :%d\n”, vinfo.yres);

printf(“bits_per_pixel is :%d\n”, vinfo.bits_per_pixel);

unsigned long screensize = 0;

screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;

//这就是把fp所指的文件中从开始到screensize大小的内容给映射出来,

//得到一个指向这块空间的指针

unsigned char *fbp =(unsigned char *)mmap(0, screensize, PROT_READ|PROT_WRITE

, MAP_SHARED, fp, 0);

if (fbp == (unsigned char*)-1)

{

printf (“Error: failed to map framebuffer device to memory./n”);

exit (EXIT_FAILURE);

}

unsigned int location = 0;

struct jpeg_decompress_struct jinfo;

struct jpeg_error_mgr jerr;

/*Bind error handler*/

jinfo.err = jpeg_std_error(&jerr);

/*init jpeg decompress object*/

jpeg_create_decompress(&jinfo);

/*Bind jpg data object*/

FILE * pfJPG = fopen(argv[1], “rb”);

if(NULL == pfJPG)

{

printf(“open %s failed. error->%s\n”, argv[1], strerror(errno));

jpeg_destroy_decompress(&jinfo);

}

else

{

jpeg_stdio_src(&jinfo, pfJPG);

jpeg_read_header(&jinfo, TRUE);

/*Start decompressing*/

jpeg_start_decompress(&jinfo);

/*Only get decompress arguments*/

//jpeg_calc_output_dimensions(&jinfo);

printf(“Output Width = %d\n”,jinfo.output_width);

printf(“Output Height = %d\n”,jinfo.output_height);

//Color Channel

printf(“Output Components = %d\n”,jinfo.output_components);

unsigned char ppucRowData = NULL;

ppucRowData = (*jinfo.mem->alloc_sarray)((j_common_ptr) &jinfo, JPOOL_IMAGE

, jinfo.output_width * jinfo.output_components, 1);

unsigned int i = 0;

unsigned int j = 0;

St_RGB stColor = {0};

while (jinfo.output_scanline < jinfo.output_height) //jinfo.output_height

{

jpeg_read_scanlines(&jinfo, ppucRowData, 1);

for (i=0; i<jinfo.output_width; i++)

{

location = i*(vinfo.bits_per_pixel/8)+j*finfo.line_length;

stColor.ucRed = ppucRowData[0][i*jinfo.output_components];

stColor.ucGreen = ppucRowData[0][i*jinfo.output_components+1];

stColor.ucBlue = ppucRowData[0][i*jinfo.output_components+2];

/*直接赋值来改变屏幕上某点的颜色*/

*(fbp + location) = stColor.ucBlue;

*(fbp + location + 1) = stColor.ucGreen;

*(fbp + location + 2) = stColor.ucRed;

*(fbp + location + 3) = 0; /*是否透明*/

}

j++;

}

/*Finish Decompress*/

jpeg_finish_decompress(&jinfo);

/*Destroy Decompress Object*/

jpeg_destroy_decompress(&jinfo);

fclose(pfJPG);

}

munmap (fbp, screensize); /*解除映射*/

close (fp);

return 0;

}

编译:gcc heart.c -o ourheart -ljpeg

运行:./ourheart

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/181035.html

(0)
上一篇 2025-06-16 10:20
下一篇 2025-06-16 10:33

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信