[英]Graphics library in C
I was wondering if there were any good free graphics libraries for C that are easy to use? 我想知道是否有任何易于使用的C自由图形库? It's for plotting 2d and 3d graphs and then saving to a file.
它用于绘制2d和3d图形,然后保存到文件。 It's on a Linux system and there's no gnuplot on the system right now.
它在Linux系统上,现在系统上没有gnuplot。
Or would it just be simpler to switch to another language, and if so which one would be easy to learn? 或者只是更简单地切换到另一种语言,如果是这样,哪一个很容易学习?
I like the Cairo library . 我喜欢开罗图书馆 。 It has a nice interface to C and it can output in many formats.
它有一个很好的C接口,它可以输出多种格式。
There's Clutter . 有杂乱 。 Here are a few snippets from the about page:
以下是关于页面的几个片段:
"Clutter is an open source software library for creating fast, visually rich, portable and animated graphical user interfaces." “Clutter是一个开源软件库,用于创建快速,视觉丰富,可移植和动画的图形用户界面。”
"Clutter aims to be non specific — it implements no particular User Interface style, but rather provides a rich generic foundation that facilitates rapid and easy creation of higher level tool kits tailored to specific needs." “Clutter的目标是非特定的 - 它没有实现特定的用户界面风格,而是提供了丰富的通用基础,有助于快速轻松地创建针对特定需求量身定制的更高级别的工具包。”
"Developed in C, with language bindings for Perl, Python, C#, C++, Vala and Ruby." “在C语言中开发,具有Perl,Python,C#,C ++,Vala和Ruby的语言绑定。”
"Scene-graph of layered 2D interface elements manipulated in 3D space via position, grouping, transparency, scaling, clipping and rotation." “通过位置,分组,透明度,缩放,剪切和旋转在3D空间中操作的分层2D界面元素的场景图。”
I haven't tried it out myself, but it seems pretty flexible if you are looking for something just to play around with. 我自己没有尝试过,但如果你正在寻找可以玩的东西,它似乎很灵活。
I've used netpbm format a few time when I needed something simple. 当我需要简单的东西时,我已经使用过netpbm格式了几次。
That's how I found out that qsort()
(in my implementation and for the data provided) performs a merge sort! 这就是我发现
qsort()
(在我的实现和提供的数据中)执行合并排序的方式!
Source code: 源代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define ARRAY_SIZE 20
#define MAX_VALUE 10
unsigned char arr[ARRAY_SIZE];
void print_array(const void *left, const void *right) {
static int imgs = 0;
int k, j;
FILE *img;
char fname[100];
char rgb[100];
if (++imgs > 9999) return;
sprintf(fname, "img/img%04d.ppm", imgs);
/* create image in "img/" directory */
img = fopen(fname, "w");
if (img) {
fprintf(img, "P3\n%d %d\n255\n", ARRAY_SIZE, MAX_VALUE);
for (j=0; j<MAX_VALUE; j++) {
for (k=0; k<ARRAY_SIZE; k++) {
int colour = 0;
if (left && left == arr+k) colour = 2;
if (right && right == arr+k) colour = 2;
if (arr[k] == MAX_VALUE - j - 1) colour = 1;
switch (colour) {
default: sprintf(rgb, "%d %d %d", 255, 255, 255); break;
case 1: sprintf(rgb, "%d %d %d", 0, 0, 0); break;
case 2: sprintf(rgb, "%d %d %d", 255, 0, 0); break;
}
}
fprintf(img, "%s\n", rgb);
}
}
fclose(img);
} else {
perror("img fopen");
}
}
int cmp(const void *left, const void *right) {
const unsigned char a = *(const unsigned char*)left;
const unsigned char b = *(const unsigned char*)right;
print_array(left, right);
if (a < b) return -1;
if (a == b) return 0;
return 1;
}
int main(void) {
int k;
unsigned int seed = 0; /* or time(0) */
srand(seed);
for (k=0; k<ARRAY_SIZE; k++) {
arr[k] = rand() % MAX_VALUE;
}
print_array(NULL, NULL);
qsort(arr, (size_t)ARRAY_SIZE, sizeof *arr, cmp);
print_array(NULL, NULL);
/* use imagemagick to convert group of files to .gif */
system("convert -delay 0"
" img/img*.ppm"
" -loop 1 img/libc-qsort2.gif");
/* remove .ppm files */
system("rm img/" "*ppm"); /* ... my editor does not like a
slash and a star together,
even inside quotes */
return 0;
}
大多数人使用gd库从C渲染,但你必须实现“数学绘图”部分。
Take a look at PGPLOT. 看看PGPLOT。 It's old but works great and should be in the repos.
它虽然陈旧,但效果很好,应该在回购中。 PLPLOT is also an option, it's similar and newer and should also be readily available in the repos.
PLPLOT也是一种选择,它是相似的和更新的,也应该在回购中随时可用。 They're both extremely powerful and can do what you specified.
他们都非常强大,可以做你指定的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.