昨天调试代码上头,CSDN的2×2000推广券过期了,还是×1忘了,好心痛,恨...恨...恨..呜呜呜呜呜呜呜呜呜呜~


一、前言

伏羲先天六十四卦方圆图,是中国古代哲学与数学的瑰宝。它以阴阳二元为基础,通过六爻排列组合出64种卦象,排列成外圆内方的结构,蕴含了古人对宇宙规律的深刻理解。

作为一名技术开发者,我一直对如何用现代计算机技术来呈现这些古老的智慧符号感兴趣。于是,我尝试用纯C++(不依赖任何第三方图形库)手写了一幅完整的伏羲六十四卦方圆图,并输出为可直接查看的BMP图像文件。

这篇文章将分享我的实现过程,以及在这个过程中对“二进制”与“卦象”关系的重新思考。

二、六十四卦的二进制本质

伏羲六十四卦的核心是阴阳二元。每一卦由六爻组成,每一爻要么是阳爻(—),要么是阴爻(- -)。如果我们将阳爻记为1,阴爻记为0,那么六十四卦恰好对应了从000000到111111的64个6位二进制数。

这个对应关系不是巧合。早在17世纪,莱布尼茨就通过传教士白晋了解到伏羲六十四卦,并惊叹于其二进制思想与自己正在发展的二进制算术完全一致。可以说,伏羲六十四卦是人类历史上最早用二进制编码的完整符号系统。

在计算机中,二进制是数据的基础。因此,用C++来生成这幅图,本质上是在用现代计算机技术的原生语言,重新表达一种古老的二进制智慧。

三、技术实现:纯C++手写BMP图像

3.1 为什么不用Python或图形库?

Python有丰富的绘图库(如matplotlib),几行代码就能生成卦图。但我选择纯C++手写BMP文件格式,原因有二:

  1. 理解底层原理:BMP是最简单的图像格式之一,手写它的文件头和数据格式,能加深对数字图像存储方式的理解。

  2. 无依赖编译:代码只需要一个g++编译器,不需要安装任何第三方库,在任何平台上都能直接编译运行。

3.2 核心数据结构

64卦的二进制表是代码的核心。我将它存储为一个64×6的二维数组:

cpp

const int HEXAGRAMS[64][6] = {
    {1,1,1,1,1,1}, // 0: 乾为天
    {0,1,1,1,1,1}, // 1: 泽天夬
    {1,0,1,1,1,1}, // 2: 火天大有
    // ... 共64行
    {0,0,0,0,0,0}  // 63: 坤为地
};

3.3 绘制逻辑

  • 圆图:64卦按先天卦序沿圆周均匀排列。每卦的六爻从圆心向外排列,阳爻用实线表示,阴爻用中间断开的虚线表示。

  • 方图:在圆心处绘制一个8×8的方阵,同样按先天八卦序排列。

  • 卦序连线:在圆图上,按先天卦序用红色细线将相邻卦象连接起来,展示卦变流转的路径。

  • 阴阳渐变背景:在圆图背景上添加一个从暖色(阳)到冷色(阴)的渐变效果,直观展示阴阳消长。

3.4 BMP文件输出

BMP文件的生成是代码中最底层的部分。我手动构造了BMP文件头(54字节),然后逐行写入像素数据。BMP格式要求像素按BGR顺序排列,且每行数据需要4字节对齐。

四、最终效果

以下是生成图像的关键信息:

  • 图像尺寸:1000×1000像素

  • 文件格式:24位BMP

  • 包含元素:圆图(64卦沿圆周排列)、方图(8×8卦阵)、卦序连线(红色细线串联)、阴阳渐变背景

  • 编译环境:g++ (C++17),Windows 10

  • 编译命令g++ -O3 -std=c++17 -o fuxi_full fuxi_full.cpp

  • 运行fuxi_full.exe,生成fuxi_full.bmp

五、技术之外的思考

完成这幅图后,最让我感慨的不是代码本身,而是它背后的文化意义。

六千多年前,伏羲“仰则观象于天,俯则观法于地”,从自然规律中抽象出阴阳二元,再用这看似简单的二元推演出六十四卦——一个能够描述宇宙万物变化的完整符号体系。他没有计算机,没有二进制理论,但他创造的这套体系,却完美契合了现代计算机科学的底层逻辑。

用现代计算机技术重现这幅图,不是要证明什么,也不是要牵强附会。这只是一位程序员,用他最熟悉的工具,向一位六千年前的智者表达敬意。

六、代码获取

完整的C++源码我就不上传GitHub仓库(反正放了也说我没放,不如不放,放置文尾你们方便),有兴趣的读者可以直接盘膝原地推演,或在此基础上改进。


作者:天赐范式(汪涣)

日期:2026年5月23日

标签:#C++ #计算机图形学 #中国传统文化 #伏羲六十四卦 #BMP图像生成


图1

// ============================================================================
// 天赐范式:伏羲先天六十四卦方圆图推演 (C++ BMP输出版)
// Tianci Paradigm: Fu Xi 64 Hexagrams Square & Circle Chart
// 核心算子:Ξ-锚定卦序, Φ-门控校验卦变, Ψ-跃迁生成图像, Λ-校验输出
// ============================================================================

#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

// ============================================================================
// Ξ-锚定:伏羲先天六十四卦二进制表 (1=阳爻, 0=阴爻)
// ============================================================================
const int HEXAGRAM_COUNT = 64;
const int LINES_PER_HEX = 6;

const int HEXAGRAMS[HEXAGRAM_COUNT][LINES_PER_HEX] = {
    {1,1,1,1,1,1}, // 0: 乾为天
    {0,1,1,1,1,1}, // 1: 泽天夬
    {1,0,1,1,1,1}, // 2: 火天大有
    {0,0,1,1,1,1}, // 3: 雷天大壮
    {1,1,0,1,1,1}, // 4: 风天小畜
    {0,1,0,1,1,1}, // 5: 水天需
    {1,0,0,1,1,1}, // 6: 山天大畜
    {0,0,0,1,1,1}, // 7: 地天泰
    {1,1,1,0,1,1}, // 8: 天泽履
    {0,1,1,0,1,1}, // 9: 兑为泽
    {1,0,1,0,1,1}, // 10: 火泽睽
    {0,0,1,0,1,1}, // 11: 雷泽归妹
    {1,1,0,0,1,1}, // 12: 风泽中孚
    {0,1,0,0,1,1}, // 13: 水泽节
    {1,0,0,0,1,1}, // 14: 山泽损
    {0,0,0,0,1,1}, // 15: 地泽临
    {1,1,1,1,0,1}, // 16: 天火同人
    {0,1,1,1,0,1}, // 17: 泽火革
    {1,0,1,1,0,1}, // 18: 火火离
    {0,0,1,1,0,1}, // 19: 雷火丰
    {1,1,0,1,0,1}, // 20: 风火家人
    {0,1,0,1,0,1}, // 21: 水火既济
    {1,0,0,1,0,1}, // 22: 山火贲
    {0,0,0,1,0,1}, // 23: 地火明夷
    {1,1,1,0,0,1}, // 24: 天雷无妄
    {0,1,1,0,0,1}, // 25: 泽雷随
    {1,0,1,0,0,1}, // 26: 火雷噬嗑
    {0,0,1,0,0,1}, // 27: 震为雷
    {1,1,0,0,0,1}, // 28: 风雷益
    {0,1,0,0,0,1}, // 29: 水雷屯
    {1,0,0,0,0,1}, // 30: 山雷颐
    {0,0,0,0,0,1}, // 31: 地雷复
    {1,1,1,1,1,0}, // 32: 天风姤
    {0,1,1,1,1,0}, // 33: 泽风大过
    {1,0,1,1,1,0}, // 34: 火风鼎
    {0,0,1,1,1,0}, // 35: 雷风恒
    {1,1,0,1,1,0}, // 36: 风风巽
    {0,1,0,1,1,0}, // 37: 水风井
    {1,0,0,1,1,0}, // 38: 山风蛊
    {0,0,0,1,1,0}, // 39: 地风升
    {1,1,1,0,1,0}, // 40: 天水讼
    {0,1,1,0,1,0}, // 41: 泽水困
    {1,0,1,0,1,0}, // 42: 火水未济
    {0,0,1,0,1,0}, // 43: 雷水解
    {1,1,0,0,1,0}, // 44: 风水涣
    {0,1,0,0,1,0}, // 45: 水水坎
    {1,0,0,0,1,0}, // 46: 山水蒙
    {0,0,0,0,1,0}, // 47: 地水师
    {1,1,1,1,0,0}, // 48: 天山遁
    {0,1,1,1,0,0}, // 49: 泽山咸
    {1,0,1,1,0,0}, // 50: 火山旅
    {0,0,1,1,0,0}, // 51: 雷山小过
    {1,1,0,1,0,0}, // 52: 风山渐
    {0,1,0,1,0,0}, // 53: 水山蹇
    {1,0,0,1,0,0}, // 54: 艮为山
    {0,0,0,1,0,0}, // 55: 地山谦
    {1,1,1,0,0,0}, // 56: 天地否
    {0,1,1,0,0,0}, // 57: 泽地萃
    {1,0,1,0,0,0}, // 58: 火地晋
    {0,0,1,0,0,0}, // 59: 雷地豫
    {1,1,0,0,0,0}, // 60: 风地观
    {0,1,0,0,0,0}, // 61: 水地比
    {1,0,0,0,0,0}, // 62: 山地剥
    {0,0,0,0,0,0}  // 63: 坤为地
};

// ============================================================================
// 图像参数
// ============================================================================
const int IMG_SIZE = 800;
const int CIRCLE_CENTER_X = 400;
const int CIRCLE_CENTER_Y = 400;
const int CIRCLE_RADIUS = 280;
const int SQUARE_SIZE = 280;
const int SQUARE_X = 400 - SQUARE_SIZE/2;
const int SQUARE_Y = 400 - SQUARE_SIZE/2;

// ============================================================================
// RGB 颜色结构
// ============================================================================
struct RGB {
    int r, g, b;
    RGB() : r(0), g(0), b(0) {}
    RGB(int r_, int g_, int b_) : r(r_), g(g_), b(b_) {}
};

// ============================================================================
// 画布类
// ============================================================================
class Canvas {
    vector<RGB> pixels;
    int w, h;
public:
    Canvas(int width, int height) : w(width), h(height), pixels(width * height) {}
    
    void set_pixel(int x, int y, const RGB& color) {
        if (x >= 0 && x < w && y >= 0 && y < h) {
            pixels[y * w + x] = color;
        }
    }
    
    void fill_circle(int cx, int cy, int r, const RGB& color) {
        for (int y = cy - r; y <= cy + r; y++) {
            for (int x = cx - r; x <= cx + r; x++) {
                if ((x - cx) * (x - cx) + (y - cy) * (y - cy) <= r * r) {
                    set_pixel(x, y, color);
                }
            }
        }
    }
    
    void fill_rect(int x0, int y0, int w, int h, const RGB& color) {
        for (int y = y0; y < y0 + h; y++) {
            for (int x = x0; x < x0 + w; x++) {
                set_pixel(x, y, color);
            }
        }
    }
    
    void draw_line(int x0, int y0, int x1, int y1, const RGB& color) {
        int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
        int dy = -abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
        int err = dx + dy, e2;
        while (true) {
            set_pixel(x0, y0, color);
            if (x0 == x1 && y0 == y1) break;
            e2 = 2 * err;
            if (e2 >= dy) { err += dy; x0 += sx; }
            if (e2 <= dx) { err += dx; y0 += sy; }
        }
    }
    
    // 输出为BMP格式 (Windows原生支持,可直接双击打开)
    void save_bmp(const string& filename) {
        ofstream f(filename, ios::binary);
        int row_padded = (w * 3 + 3) & (~3);
        int file_size = 54 + row_padded * h;
        unsigned char header[54] = {0};
        header[0] = 'B'; header[1] = 'M';
        *(int*)&header[2] = file_size;
        *(int*)&header[10] = 54;
        *(int*)&header[14] = 40;
        *(int*)&header[18] = w;
        *(int*)&header[22] = h;
        *(short*)&header[26] = 1;
        *(short*)&header[28] = 24;
        f.write((char*)header, 54);
        vector<unsigned char> row(row_padded, 0);
        for (int y = h - 1; y >= 0; y--) {
            for (int x = 0; x < w; x++) {
                const RGB& p = pixels[y * w + x];
                row[x*3] = p.b;
                row[x*3+1] = p.g;
                row[x*3+2] = p.r;
            }
            f.write((char*)row.data(), row_padded);
        }
        f.close();
        cout << "[Psi-Transition] Fu Xi 64 Hexagrams Chart saved: " << filename << endl;
    }
};

// ============================================================================
// Φ-门控:校验卦象合法性
// ============================================================================
bool phi_gate_check(int hexagram_index) {
    if (hexagram_index < 0 || hexagram_index >= HEXAGRAM_COUNT) {
        cout << "[Phi-Gating] Hexagram index out of range: " << hexagram_index << ", triggering fuse" << endl;
        return false;
    }
    for (int i = 0; i < LINES_PER_HEX; i++) {
        if (HEXAGRAMS[hexagram_index][i] != 0 && HEXAGRAMS[hexagram_index][i] != 1) {
            cout << "[Phi-Gating] Hexagram " << hexagram_index << " has invalid line value, triggering fuse" << endl;
            return false;
        }
    }
    return true;
}

// ============================================================================
// Ψ-跃迁:生成方圆图
// ============================================================================
void generate_fuxi_chart(Canvas& canvas) {
    RGB bg_color(240, 235, 220);        // 背景色:古宣纸色
    RGB yang_color(30, 30, 30);          // 阳爻色:墨色
    RGB yin_color(180, 160, 130);        // 阴爻色:淡墨
    RGB circle_border(80, 60, 40);       // 圆图边框
    RGB square_border(80, 60, 40);       // 方图边框
    
    canvas.fill_rect(0, 0, IMG_SIZE, IMG_SIZE, bg_color);
    
    canvas.fill_circle(CIRCLE_CENTER_X, CIRCLE_CENTER_Y, CIRCLE_RADIUS + 5, circle_border);
    canvas.fill_circle(CIRCLE_CENTER_X, CIRCLE_CENTER_Y, CIRCLE_RADIUS, bg_color);
    
    // 圆图:64卦沿圆周排列
    for (int i = 0; i < HEXAGRAM_COUNT; i++) {
        if (!phi_gate_check(i)) continue;
        
        double angle = 2.0 * 3.141592653589793 * i / HEXAGRAM_COUNT - 3.141592653589793 / 2.0;
        double hex_center_x = CIRCLE_CENTER_X + CIRCLE_RADIUS * 0.75 * cos(angle);
        double hex_center_y = CIRCLE_CENTER_Y + CIRCLE_RADIUS * 0.75 * sin(angle);
        
        double line_length = 16;
        double line_spacing = 2.5;
        double hex_width = line_length;
        double hex_height = (LINES_PER_HEX - 1) * line_spacing;
        
        for (int line = 0; line < LINES_PER_HEX; line++) {
            double ly = hex_center_y - hex_height/2.0 + line * line_spacing;
            double lx0 = hex_center_x - hex_width/2.0;
            double lx1 = hex_center_x + hex_width/2.0;
            
            RGB line_color = HEXAGRAMS[i][line] ? yang_color : yin_color;
            
            if (HEXAGRAMS[i][line]) {
                canvas.draw_line((int)lx0, (int)ly, (int)lx1, (int)ly, line_color);
            } else {
                int gap = 4;
                canvas.draw_line((int)lx0, (int)ly, (int)(lx0 + hex_width/2 - gap/2), (int)ly, line_color);
                canvas.draw_line((int)(lx0 + hex_width/2 + gap/2), (int)ly, (int)lx1, (int)ly, line_color);
            }
        }
    }
    
    // 方图:8x8 卦阵在中心
    int square_margin = 20;
    int cell_size = (SQUARE_SIZE - 2 * square_margin) / 8;
    
    canvas.fill_rect(SQUARE_X - 2, SQUARE_Y - 2, SQUARE_SIZE + 4, SQUARE_SIZE + 4, square_border);
    canvas.fill_rect(SQUARE_X, SQUARE_Y, SQUARE_SIZE, SQUARE_SIZE, bg_color);
    
    int square_order[8] = {0, 1, 2, 3, 4, 5, 6, 7};
    
    for (int row = 0; row < 8; row++) {
        for (int col = 0; col < 8; col++) {
            int upper_trigram = square_order[row];
            int lower_trigram = square_order[col];
            int hex_index = upper_trigram * 8 + lower_trigram;
            
            if (!phi_gate_check(hex_index)) continue;
            
            int cell_x = SQUARE_X + square_margin + col * cell_size;
            int cell_y = SQUARE_Y + square_margin + row * cell_size;
            
            double line_len = cell_size * 0.7;
            double spacing = cell_size * 0.12;
            double start_x = cell_x + (cell_size - line_len) / 2.0;
            double start_y = cell_y + cell_size * 0.15;
            
            for (int line = 0; line < LINES_PER_HEX; line++) {
                double ly = start_y + line * spacing;
                double lx0 = start_x;
                double lx1 = start_x + line_len;
                
                RGB line_color = HEXAGRAMS[hex_index][line] ? yang_color : yin_color;
                
                if (HEXAGRAMS[hex_index][line]) {
                    canvas.draw_line((int)lx0, (int)ly, (int)lx1, (int)ly, line_color);
                } else {
                    int gap = 3;
                    double mid = start_x + line_len / 2.0;
                    canvas.draw_line((int)lx0, (int)ly, (int)(mid - gap), (int)ly, line_color);
                    canvas.draw_line((int)(mid + gap), (int)ly, (int)lx1, (int)ly, line_color);
                }
            }
        }
    }
}

// ============================================================================
// 主程序
// ============================================================================
int main() {
    cout << "[Xi-Anchoring] Tianci Paradigm: Fu Xi 64 Hexagrams Chart Generation Started" << endl;
    cout << "[Theta-Trace] Hexagram source: Fu Xi binary table" << endl;
    cout << "[Phi-Gating] Starting hexagram validity check..." << endl;
    
    int valid_count = 0;
    for (int i = 0; i < HEXAGRAM_COUNT; i++) {
        if (phi_gate_check(i)) valid_count++;
    }
    cout << "[Phi-Gating] Check complete: valid hexagrams " << valid_count << "/" << HEXAGRAM_COUNT << endl;
    
    Canvas canvas(IMG_SIZE, IMG_SIZE);
    cout << "[Psi-Transition] Generating chart..." << endl;
    generate_fuxi_chart(canvas);
    
    canvas.save_bmp("fuxi_64.bmp");
    
    cout << "[Lambda-Check] Chart generation complete" << endl;
    cout << "[Done] Tianci Paradigm Operator Flow Closed-Loop Verification Passed" << endl;
    
    return 0;
}

图2

// ============================================================================
// 天赐范式:伏羲先天六十四卦方圆图完整版 (BMP输出)
// 包含:圆图 + 方图 + 卦序连线 + 阴阳渐变背景 + 卦名标注
// ============================================================================

#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

// ============================================================================
// Ξ-锚定:伏羲先天六十四卦二进制表 (1=阳爻, 0=阴爻)
// ============================================================================
const int HEXAGRAM_COUNT = 64;
const int LINES_PER_HEX = 6;

const int HEXAGRAMS[HEXAGRAM_COUNT][LINES_PER_HEX] = {
    {1,1,1,1,1,1}, {0,1,1,1,1,1}, {1,0,1,1,1,1}, {0,0,1,1,1,1},
    {1,1,0,1,1,1}, {0,1,0,1,1,1}, {1,0,0,1,1,1}, {0,0,0,1,1,1},
    {1,1,1,0,1,1}, {0,1,1,0,1,1}, {1,0,1,0,1,1}, {0,0,1,0,1,1},
    {1,1,0,0,1,1}, {0,1,0,0,1,1}, {1,0,0,0,1,1}, {0,0,0,0,1,1},
    {1,1,1,1,0,1}, {0,1,1,1,0,1}, {1,0,1,1,0,1}, {0,0,1,1,0,1},
    {1,1,0,1,0,1}, {0,1,0,1,0,1}, {1,0,0,1,0,1}, {0,0,0,1,0,1},
    {1,1,1,0,0,1}, {0,1,1,0,0,1}, {1,0,1,0,0,1}, {0,0,1,0,0,1},
    {1,1,0,0,0,1}, {0,1,0,0,0,1}, {1,0,0,0,0,1}, {0,0,0,0,0,1},
    {1,1,1,1,1,0}, {0,1,1,1,1,0}, {1,0,1,1,1,0}, {0,0,1,1,1,0},
    {1,1,0,1,1,0}, {0,1,0,1,1,0}, {1,0,0,1,1,0}, {0,0,0,1,1,0},
    {1,1,1,0,1,0}, {0,1,1,0,1,0}, {1,0,1,0,1,0}, {0,0,1,0,1,0},
    {1,1,0,0,1,0}, {0,1,0,0,1,0}, {1,0,0,0,1,0}, {0,0,0,0,1,0},
    {1,1,1,1,0,0}, {0,1,1,1,0,0}, {1,0,1,1,0,0}, {0,0,1,1,0,0},
    {1,1,0,1,0,0}, {0,1,0,1,0,0}, {1,0,0,1,0,0}, {0,0,0,1,0,0},
    {1,1,1,0,0,0}, {0,1,1,0,0,0}, {1,0,1,0,0,0}, {0,0,1,0,0,0},
    {1,1,0,0,0,0}, {0,1,0,0,0,0}, {1,0,0,0,0,0}, {0,0,0,0,0,0}
};

// 卦名 (简体中文,用于标注)
const char* HEXAGRAM_NAMES[HEXAGRAM_COUNT] = {
    "乾", "夬", "大有", "大壮", "小畜", "需", "大畜", "泰",
    "履", "兑", "睽", "归妹", "中孚", "节", "损", "临",
    "同人", "革", "离", "丰", "家人", "既济", "贲", "明夷",
    "无妄", "随", "噬嗑", "震", "益", "屯", "颐", "复",
    "姤", "大过", "鼎", "恒", "巽", "井", "蛊", "升",
    "讼", "困", "未济", "解", "涣", "坎", "蒙", "师",
    "遁", "咸", "旅", "小过", "渐", "蹇", "艮", "谦",
    "否", "萃", "晋", "豫", "观", "比", "剥", "坤"
};

// ============================================================================
// 图像参数
// ============================================================================
const int IMG_SIZE = 1000;
const int CIRCLE_CENTER_X = IMG_SIZE / 2;
const int CIRCLE_CENTER_Y = IMG_SIZE / 2;
const int CIRCLE_RADIUS = 300;
const int SQUARE_SIZE = 240;
const int SQUARE_X = IMG_SIZE / 2 - SQUARE_SIZE/2;
const int SQUARE_Y = IMG_SIZE / 2 - SQUARE_SIZE/2;

// ============================================================================
// RGB 颜色结构
// ============================================================================
struct RGB {
    int r, g, b;
    RGB() : r(0), g(0), b(0) {}
    RGB(int r_, int g_, int b_) : r(r_), g(g_), b(b_) {}
};

// ============================================================================
// 画布类
// ============================================================================
class Canvas {
    vector<RGB> pixels;
    int w, h;
public:
    Canvas(int width, int height) : w(width), h(height), pixels(width * height, RGB(240, 235, 220)) {}
    
    void set_pixel(int x, int y, const RGB& color) {
        if (x >= 0 && x < w && y >= 0 && y < h) {
            pixels[y * w + x] = color;
        }
    }
    
    RGB get_pixel(int x, int y) const {
        if (x >= 0 && x < w && y >= 0 && y < h) {
            return pixels[y * w + x];
        }
        return RGB(240, 235, 220);
    }
    
    void fill_circle(int cx, int cy, int r, const RGB& color) {
        for (int y = cy - r; y <= cy + r; y++) {
            for (int x = cx - r; x <= cx + r; x++) {
                if ((x - cx) * (x - cx) + (y - cy) * (y - cy) <= r * r) {
                    set_pixel(x, y, color);
                }
            }
        }
    }
    
    void fill_rect(int x0, int y0, int w, int h, const RGB& color) {
        for (int y = y0; y < y0 + h; y++) {
            for (int x = x0; x < x0 + w; x++) {
                set_pixel(x, y, color);
            }
        }
    }
    
    void draw_line(int x0, int y0, int x1, int y1, const RGB& color) {
        int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
        int dy = -abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
        int err = dx + dy, e2;
        while (true) {
            set_pixel(x0, y0, color);
            if (x0 == x1 && y0 == y1) break;
            e2 = 2 * err;
            if (e2 >= dy) { err += dy; x0 += sx; }
            if (e2 <= dx) { err += dx; y0 += sy; }
        }
    }
    
    void save_bmp(const string& filename) {
        ofstream f(filename, ios::binary);
        int row_padded = (w * 3 + 3) & (~3);
        int file_size = 54 + row_padded * h;
        unsigned char header[54] = {0};
        header[0] = 'B'; header[1] = 'M';
        *(int*)&header[2] = file_size;
        *(int*)&header[10] = 54;
        *(int*)&header[14] = 40;
        *(int*)&header[18] = w;
        *(int*)&header[22] = h;
        *(short*)&header[26] = 1;
        *(short*)&header[28] = 24;
        f.write((char*)header, 54);
        vector<unsigned char> row(row_padded, 0);
        for (int y = h - 1; y >= 0; y--) {
            for (int x = 0; x < w; x++) {
                const RGB& p = pixels[y * w + x];
                row[x*3] = p.b;
                row[x*3+1] = p.g;
                row[x*3+2] = p.r;
            }
            f.write((char*)row.data(), row_padded);
        }
        f.close();
        cout << "[Psi-Transition] Complete Fuxi chart saved: " << filename << endl;
    }
};

// ============================================================================
// Φ-门控:校验卦象合法性
// ============================================================================
bool phi_gate_check(int hexagram_index) {
    if (hexagram_index < 0 || hexagram_index >= HEXAGRAM_COUNT) return false;
    for (int i = 0; i < LINES_PER_HEX; i++) {
        if (HEXAGRAMS[hexagram_index][i] != 0 && HEXAGRAMS[hexagram_index][i] != 1) return false;
    }
    return true;
}

// ============================================================================
// 生成阴阳渐变背景色环
// ============================================================================
void draw_yinyang_gradient(Canvas& canvas) {
    for (int y = 0; y < IMG_SIZE; y++) {
        for (int x = 0; x < IMG_SIZE; x++) {
            double dx = x - CIRCLE_CENTER_X;
            double dy = y - CIRCLE_CENTER_Y;
            double dist = sqrt(dx * dx + dy * dy);
            double angle = atan2(dy, dx);
            if (angle < 0) angle += 2.0 * 3.141592653589793;
            
            // 角度映射到阴阳比例:0=纯阳(右), PI=纯阴(左)
            double yang_ratio = cos(angle) * 0.5 + 0.5;
            
            // 距离映射到饱和度:中心浓,边缘淡
            double saturation = 1.0 - dist / (CIRCLE_RADIUS * 1.2);
            saturation = max(0.0, min(1.0, saturation));
            
            // 阳色:暖金,阴色:冷蓝
            int r = (int)(240 * yang_ratio + 180 * (1.0 - yang_ratio) * saturation);
            int g = (int)(220 * yang_ratio + 200 * (1.0 - yang_ratio) * saturation);
            int b = (int)(180 * yang_ratio + 220 * (1.0 - yang_ratio) * saturation);
            
            RGB bg(r, g, b);
            canvas.set_pixel(x, y, bg);
        }
    }
}

// ============================================================================
// Ψ-跃迁:生成完整方圆图
// ============================================================================
void generate_full_fuxi_chart(Canvas& canvas) {
    RGB yang_color(10, 10, 10);              // 阳爻:浓墨
    RGB yin_color(160, 140, 110);            // 阴爻:淡墨
    RGB circle_border(60, 40, 20);           // 圆图边框
    RGB square_border(60, 40, 20);           // 方图边框
    RGB line_color(180, 50, 50);             // 卦序连线:朱砂红
    RGB name_color(80, 40, 20);              // 卦名颜色
    
    // 阴阳渐变背景
    cout << "[Psi-Transition] Drawing Yin-Yang gradient background..." << endl;
    draw_yinyang_gradient(canvas);
    
    // 圆图边框
    canvas.fill_circle(CIRCLE_CENTER_X, CIRCLE_CENTER_Y, CIRCLE_RADIUS + 8, circle_border);
    
    // 卦序连线 (先天卦序从0到63按圆周排列)
    cout << "[Psi-Transition] Drawing hexagram sequence lines..." << endl;
    for (int i = 0; i < HEXAGRAM_COUNT; i++) {
        int j = (i + 1) % HEXAGRAM_COUNT;
        double angle_i = 2.0 * 3.141592653589793 * i / HEXAGRAM_COUNT - 3.141592653589793 / 2.0;
        double angle_j = 2.0 * 3.141592653589793 * j / HEXAGRAM_COUNT - 3.141592653589793 / 2.0;
        
        int x0 = (int)(CIRCLE_CENTER_X + CIRCLE_RADIUS * 0.75 * cos(angle_i));
        int y0 = (int)(CIRCLE_CENTER_Y + CIRCLE_RADIUS * 0.75 * sin(angle_i));
        int x1 = (int)(CIRCLE_CENTER_X + CIRCLE_RADIUS * 0.75 * cos(angle_j));
        int y1 = (int)(CIRCLE_CENTER_Y + CIRCLE_RADIUS * 0.75 * sin(angle_j));
        
        canvas.draw_line(x0, y0, x1, y1, line_color);
    }
    
    // 圆图:64卦沿圆周排列
    cout << "[Psi-Transition] Drawing 64 hexagrams on circle..." << endl;
    for (int i = 0; i < HEXAGRAM_COUNT; i++) {
        if (!phi_gate_check(i)) continue;
        
        double angle = 2.0 * 3.141592653589793 * i / HEXAGRAM_COUNT - 3.141592653589793 / 2.0;
        double hex_center_x = CIRCLE_CENTER_X + CIRCLE_RADIUS * 0.75 * cos(angle);
        double hex_center_y = CIRCLE_CENTER_Y + CIRCLE_RADIUS * 0.75 * sin(angle);
        
        double line_length = 18;
        double line_spacing = 3.0;
        double hex_height = (LINES_PER_HEX - 1) * line_spacing;
        
        for (int line = 0; line < LINES_PER_HEX; line++) {
            double ly = hex_center_y - hex_height/2.0 + line * line_spacing;
            double lx0 = hex_center_x - line_length/2.0;
            double lx1 = hex_center_x + line_length/2.0;
            
            RGB lc = HEXAGRAMS[i][line] ? yang_color : yin_color;
            
            if (HEXAGRAMS[i][line]) {
                canvas.draw_line((int)lx0, (int)ly, (int)lx1, (int)ly, lc);
            } else {
                int gap = 5;
                double mid = hex_center_x;
                canvas.draw_line((int)lx0, (int)ly, (int)(mid - gap/2), (int)ly, lc);
                canvas.draw_line((int)(mid + gap/2), (int)ly, (int)lx1, (int)ly, lc);
            }
        }
        
        // 卦名标注(在卦象外侧)
        double name_x = CIRCLE_CENTER_X + (CIRCLE_RADIUS * 0.88) * cos(angle);
        double name_y = CIRCLE_CENTER_Y + (CIRCLE_RADIUS * 0.88) * sin(angle);
        // 简化:用一个像素点标记位置(C++无字体渲染,用点代替)
        canvas.set_pixel((int)name_x, (int)name_y, name_color);
        canvas.set_pixel((int)name_x+1, (int)name_y, name_color);
        canvas.set_pixel((int)name_x, (int)name_y+1, name_color);
    }
    
    // 方图:8x8 卦阵在中心
    cout << "[Psi-Transition] Drawing square chart..." << endl;
    int square_margin = 25;
    int cell_size = (SQUARE_SIZE - 2 * square_margin) / 8;
    
    canvas.fill_rect(SQUARE_X - 2, SQUARE_Y - 2, SQUARE_SIZE + 4, SQUARE_SIZE + 4, square_border);
    
    int square_order[8] = {0, 1, 2, 3, 4, 5, 6, 7};
    
    for (int row = 0; row < 8; row++) {
        for (int col = 0; col < 8; col++) {
            int upper_trigram = square_order[row];
            int lower_trigram = square_order[col];
            int hex_index = upper_trigram * 8 + lower_trigram;
            
            if (!phi_gate_check(hex_index)) continue;
            
            int cell_x = SQUARE_X + square_margin + col * cell_size;
            int cell_y = SQUARE_Y + square_margin + row * cell_size;
            
            double line_len = cell_size * 0.7;
            double spacing = cell_size * 0.13;
            double start_x = cell_x + (cell_size - line_len) / 2.0;
            double start_y = cell_y + cell_size * 0.15;
            
            for (int line = 0; line < LINES_PER_HEX; line++) {
                double ly = start_y + line * spacing;
                double lx0 = start_x;
                double lx1 = start_x + line_len;
                
                RGB lc = HEXAGRAMS[hex_index][line] ? yang_color : yin_color;
                
                if (HEXAGRAMS[hex_index][line]) {
                    canvas.draw_line((int)lx0, (int)ly, (int)lx1, (int)ly, lc);
                } else {
                    int gap = 3;
                    double mid = start_x + line_len / 2.0;
                    canvas.draw_line((int)lx0, (int)ly, (int)(mid - gap), (int)ly, lc);
                    canvas.draw_line((int)(mid + gap), (int)ly, (int)lx1, (int)ly, lc);
                }
            }
        }
    }
}

// ============================================================================
// 主程序
// ============================================================================
int main() {
    cout << "[Xi-Anchoring] Tianci Paradigm: Complete Fu Xi 64 Hexagrams Chart" << endl;
    cout << "[Theta-Trace] Hexagram source: Fu Xi binary table" << endl;
    cout << "[Phi-Gating] Validating all hexagrams..." << endl;
    
    int valid_count = 0;
    for (int i = 0; i < HEXAGRAM_COUNT; i++) {
        if (phi_gate_check(i)) valid_count++;
    }
    cout << "[Phi-Gating] Valid hexagrams: " << valid_count << "/" << HEXAGRAM_COUNT << endl;
    
    Canvas canvas(IMG_SIZE, IMG_SIZE);
    generate_full_fuxi_chart(canvas);
    
    canvas.save_bmp("fuxi_full.bmp");
    
    cout << "[Lambda-Check] Complete chart generation finished" << endl;
    cout << "[Done] Tianci Paradigm Operator Flow Closed-Loop Verification Passed" << endl;
    
    return 0;
}

Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐