前几天在《为什么图形学人才这么少?》的回答中,引用了儿子2017年9月用 Scratch 画的一幅作品,当时还猜不到他是怎么画的。后来评论中
试出了可用
再通过取模并用色相(hue)显示,便可获得类似的图。
然后我搜到 Scratch 的 hue 范围 是 ,所以应该是简单地取模 200 的结果。那么就用 C 语言实现一下:
#include "svpng.inc"
#include <math.h>
#define W 800
#define H 800
unsigned char img[W * H * 3], *p = img;
int main() {
for (int y = -H / 2; y < H / 2; y++)
for (int x = -W / 2; x < W / 2; x++) {
double hue = fmod(pow(fabs(x * 0.1), 3) +
pow(fabs(y * 0.1), 3), 200) / 200 * 360;
for (int n = 5; n >= 1; n -= 2) { // hsv2rgb with s = v = 1
double k = fmod(n + hue / 60, 6);
*p++ = (1 - fmax(fmin(fmin(k, 4 - k), 1), 0)) * 255;
}
}
svpng(fopen("luasplot.png", "wb"), W, H, img, 0);
}得到接近的结果:
实际上 (1) 是 p-norm 去除了开 p 次方的版本,形状也是相似的。我在《如何用C语言画一个蘑菇?》介绍过 p-norm,却没联想到。
设 p = 4 可得这个效果:
计算机图形学有趣么?