CanvasC 是一个使用 ANSI C 编写的图形库,提供了一些基本的绘图函数,所有图形将会在一张 .ppm 格式的图片上生成。
CanvasC 使用一个结构体来表示绘图设备,结构体定义如下:
struct CANVAS {
int width;
int height;
int originX;
int originY;
};其中,width 和 height 分别表示绘图设备的宽度和高度,originX 和 originY 分别表示绘图设备的原点坐标。
CanvasC 使用一个二维坐标系来表示绘图设备的坐标,原点位于绘图设备的左上角,x 轴向右为正方向,y 轴向下为正方向。
CanvasC 使用一个结构体来表示颜色,结构体定义如下:
struct COLOR {
unsigned char R;
unsigned char G;
unsigned char B;
};其中,R、G、B 分别表示红色、绿色、蓝色的强度,取值范围是 0 到 255。
将 RGB 值转换为 const struct COLOR 类型。
#define RGB(r, g, b) \
((const struct COLOR){(r), (g), (b)})CanvasC 提供了一些全局颜色,可以在绘图函数中使用:
默认为黑色。
struct COLOR backColor = { 0, 0, 0 };
默认为白色。
struct COLOR lineColor = { 255, 255, 255 };
默认为白色。
struct COLOR fillColor = { 255, 255, 255 };
默认为白色。
struct COLOR textColor = { 255, 255, 255 };
CanvasC 提供了一些默认颜色,可以在绘图函数中使用:
| 颜色名称 | COLOR NAME | 颜色值RGB |
|---|---|---|
| 红色 | RED |
(255, 0, 0) |
| 绿色 | GREEN |
( 0, 255, 0) |
| 蓝色 | BLUE |
( 0, 0, 255) |
| 黑色 | BLACK |
( 0, 0, 0) |
| 白色 | WHITE |
(255, 255, 255) |
| 黄色 | YELLOW |
(255, 255, 0) |
| 紫色 | PURPLE |
(128, 0, 128) |
| 青色 | CYAN |
( 0, 255, 255) |
| 品红色 | MAGENTA |
(255, 0, 255) |
| 橘色 | ORANGE |
(255, 165, 0) |
| 灰色 | GRAY |
(128, 128, 128) |
注:默认颜色均为 const struct COLOR 类型,不能修改。
void initCanvas(
int width,
int height,
const char* filename
);注:只有调用该函数时,绘图设备上的所有图形才会被保存到文件中。
void closeCanvas();注:清空绘图设备实际上是将绘图设备上的所有像素点设置为背景颜色。
void clearCanvas();void setCanvasOrigin(
int x,
int y
);void setBackColor(struct COLOR color);void setLineColor(struct COLOR color);void setFillColor(struct COLOR color);void setTextColor(struct COLOR color);void putPixel(
int x,
int y,
struct COLOR color
);void line(
int x1,
int y1,
int x2,
int y2,
struct COLOR color);void circle(
int x,
int y,
int radius
);void solidCircle(
int x,
int y,
int radius
);void fillCircle(
int x,
int y,
int radius
);void ellipse(
int left,
int top,
int right,
int bottom
);void solidEllipse(
int left,
int top,
int right,
int bottom
);void fillEllipse(
int left,
int top,
int right,
int bottom
);void rectangle(
int left,
int top,
int right,
int bottom
);void solidRectangle(
int left,
int top,
int right,
int bottom
);void fillRectangle(
int left,
int top,
int right,
int bottom
);注:当前文字仅支持大写英文字母。
void putChar(
int x,
int y,
char c
);注:支持换行符 \n。
void putText(
int x,
int y,
const char* str
);RGB 的最大值。
#define COLOR_MAX 255将 RGB 值转换为 const struct COLOR 类型。
#define RGB(r, g, b) \
((const struct COLOR){(r), (g), (b)})返回 a 和 b 中的较大值。
#define Max(a, b) \
((a) > (b) ? (a) : (b))返回 a 和 b 中的较小值。
#define Min(a, b) \
((a) < (b) ? (a) : (b))返回 x 的绝对值。
#define Abs(x) \
((x) < 0 ? -(x) : (x))返回 x 的平方。
#define Sq(x) \
((x) * (x))交换 a 和 b 的值。
#define Swap(a, b, Type) \
do { \
Type temp = (a); \
(a) = (b); \
(b) = temp; \
} while (0)