41223111

  • Home
    • SMap
    • reveal
    • blog
  • About
  • Work
  • Program
  • experience
  • Brython
Work << Previous Next >> experience

Program

程式對應圖片(建議是輸入的文件名稱)

單曲線圖(gnuolot_ex1.c)

// 包含標準輸出入程式庫的標頭文件
// https://blog.csdn.net/weixin_38468077/article/details/101069365
// http://www.gnuplot.info/demo/
// https://github.com/sysprog21/rv32emu
// https://github.com/sysprog21/semu 
// https://docs.google.com/presentation/d/14N0cWG2SnBSqhc2cLF0_2VerB9FF8JN3
// https://cs61c.org/fa23/
// https://greenteapress.com/wp/think-python-2e/
// https://github.com/ecalvadi/c99-examples
// https://github.com/gouravthakur39/beginners-C-program-examples
// https://github.com/ergenekonyigit/Numerical-Analysis-Examples
// https://www.che.ncku.edu.tw/facultyweb/changct/html/teaching/CPPandMATLAB/Past/pdf%20Files/Chap02-Ling.pdf
// https://gteceducation.com.sg/Brochures/PROGRAMMING/C%20PROGRAMMING%20FULL.pdf
// https://jsommers.github.io/cbook/cbook.pdf
// https://jsommers.github.io/cbook/index.html
// http://student.itee.uq.edu.au/courses/csse2310/CProgrammingNotes.pdf
// http://cslibrary.stanford.edu/101/EssentialC.pdf
// https://publications.gbdirect.co.uk/c_book/
// https://www.fossil-scm.org/fossil-book/doc/2ndEdition/fossilbook.pdf
// ***** execute on replit 
// cd downloads
// cc gnuplot_ex1.c -o gnuplot_ex1
// ./gnuplot_ex1
#include <stdio.h>

// 主函式
int main() {
    // Start a Gnuplot process using popen
    FILE *gnuplotPipe = popen("gnuplot -persistent", "w");
    if (!gnuplotPipe) {
        fprintf(stderr, "Failed to start Gnuplot.\n");
        return 1;
    }

    // Use Gnuplot plotting commands, specify font and output as PNG
    fprintf(gnuplotPipe, "set terminal png font 'default,10' size 800,400\n");
    fprintf(gnuplotPipe, "set output './../images/gnuplot_ex1.png'\n");
    fprintf(gnuplotPipe, "plot sin(x)");
    // Close popen
    pclose(gnuplotPipe);

    return 0;
}

雙曲線圖(gnuolot_ex2.c)

#include <stdio.h>

int main() {
    // Open a file to write displacement and velocity data
    FILE *outputFile = fopen("motion_data.txt", "w");
    if (!outputFile) {
        fprintf(stderr, "Failed to create data file.\n");
        return 1;
    }

    // Simulate motion for 10 seconds and calculate displacement and velocity, while writing data to the file
    double x = 0.2;  // Initial displacement
    double v = 0.0;  // Initial velocity
    double dt = 0.01; // Time step
    double t = 0.0;  // Time

    while (t <= 10.0) {
        double acceleration = (-10.0 * x - 0.5 * v) / 1.0; // Modified system parameters here
        v += acceleration * dt;
        x += v * dt;

        fprintf(outputFile, "%lf %lf %lf\n", t, x, v);

        t += dt;
    }

    // Close the data file
    fclose(outputFile);

    // Start a Gnuplot process using popen
    FILE *gnuplotPipe = popen("gnuplot -persistent", "w");
    if (!gnuplotPipe) {
        fprintf(stderr, "Failed to start Gnuplot.\n");
        return 1;
    }

    // Use Gnuplot plotting commands, specify font and output as PNG
    fprintf(gnuplotPipe, "set terminal pngcairo enhanced font 'default,10' size 800,400\n");
    fprintf(gnuplotPipe, "set output './../images/motion_plot.png'\n");
    fprintf(gnuplotPipe, "set title 'Displacement and Velocity vs. Time'\n");
    fprintf(gnuplotPipe, "set xlabel 'Time (s)'\n");
    fprintf(gnuplotPipe, "set ylabel 'Displacement (m)'\n");
    fprintf(gnuplotPipe, "plot 'motion_data.txt' using 1:2 with lines lw 2 title 'Displacement', \
                             'motion_data.txt' using 1:3 with lines lw 2 title 'Velocity'\n");

    // Close the Gnuplot process
    fprintf(gnuplotPipe, "exit\n");
    pclose(gnuplotPipe);

    return 0;
}

臺灣國旗(gd_roc_flag.c)

// https://en.wikipedia.org/wiki/Flag_of_the_Republic_of_China
// 內政部國旗參考資料: https://www.moi.gov.tw/cp.aspx?n=10621
// cc roc_flag_in_gd.c -lgd -lm to link with gd and math library
// https://www.rapidtables.com/web/color/RGB_Color.html
// 幾何形狀著色與繪圖練習
// 以下 gd 繪圖程式嘗試畫出 ROC 國旗, 請根據下列程式內容完成後續的國旗繪圖
#include <stdio.h>
#include <gd.h>
#include <math.h>

void draw_roc_flag(gdImagePtr img);
void draw_white_sun(gdImagePtr img, int x, int y, int size, int color);

int main() {
// width 3: height 2
int width = 1200;
// 國旗長寬比為 3:2
int height = (int)(width*2.0 / 3.0);

gdImagePtr img = gdImageCreateTrueColor(width, height);
gdImageAlphaBlending(img, 0);

draw_roc_flag(img);

FILE *outputFile = fopen("./../images/roc_flag_in_gd.png", "wb");
if (outputFile == NULL) {
fprintf(stderr, "Error opening the output file.\n");
return 1;
}
gdImagePngEx(img, outputFile, 9);
fclose(outputFile);
gdImageDestroy(img);
return 0;
}

void draw_roc_flag(gdImagePtr img) {
int width = gdImageSX(img);
int height = gdImageSY(img);
int red, white, blue;
// 白日位於青天面積正中央, 因此中心點座標為長寬各 1/4 處
int center_x = (int)(width/4);
int center_y = (int)(height/4);
// gdImageFilledEllipse 需以長寬方向的 diameter 作圖
// 由於中央白日圓形的半徑為青天寬度的 1/8
// 因此中央白日圓形的直徑為青天寬度的 1/4, 也就是國旗寬度的 1/8
// 而且白日十二道光芒的外圍圓形其半徑也是國旗寬度的1/8
int sun_radius = (int)(width/8);
// 中央白日圓形的直徑等於十二道光芒外圍圓形的半徑
int white_circle_dia = sun_radius;
// 中央藍色圓形半徑為中央白日的 1又 2/15
int blue_circle_dia = white_circle_dia + white_circle_dia * 2 / 15;
// 根據 https://www.moi.gov.tw/cp.aspx?n=10621 訂定國旗三種顏色值
red = gdImageColorAllocate(img, 255, 0, 0); // 紅色
white = gdImageColorAllocate(img, 255, 255, 255); // 白色
blue = gdImageColorAllocate(img, 0, 0, 149); // 藍色
// 根據畫布大小塗上紅色長方形區域
gdImageFilledRectangle(img, 0, 0, width, height, red);
// 青天面積為整面國旗的 1/4, 也是採用長方形塗色
gdImageFilledRectangle(img, 0, 0, (int)(width/2.0), (int)(height/2.0), blue);
// 先設法以填色畫出六個白色堆疊菱形
draw_white_sun(img, center_x, center_y, sun_radius, white);
// 利用一個藍色大圓與白色小圓畫出藍色環狀
gdImageFilledEllipse(img, center_x, center_y, blue_circle_dia, blue_circle_dia, blue);
gdImageFilledEllipse(img, center_x, center_y, white_circle_dia, white_circle_dia, white);

}

void draw_white_sun(gdImagePtr img, int center_x, int center_y, int sun_radius, int color) {
// M_PI 大小定義於 math.h 標頭檔中, 因為三角函數中採用徑度為角度單位
// 因此定義將角度轉為徑度的轉換變數為 deg, 角度值乘上 deg 就可轉為徑度
float deg = M_PI/180;
// 根據十二道光芒的每一尖角的角度為 15 度, 求出其對應直角三角形的另一角度為 75 度
// 求出十二道光芒中任一菱形的 small radius, 也就是菱形的另一個對應小圓的半徑大小
float sr = sun_radius/tan(75*deg);
int ax, ay, bx, by, dx, dy, ex, ey;
gdPoint points[4];
/* 在塗上十二道光芒中的單一菱形區域之前, 先以座標點畫線測試是否正確
ax = center_x;
ay = center_y - sun_radius;
bx = center_x - sun_radius*tan(15*deg);
by = center_y;
ex = center_x;
ey = center_y + sun_radius;
dx = center_x + sun_radius*tan(15*deg);
dy = center_y;
// AB
gdImageLine(img, ax, ay, bx, by, color);
// BE
gdImageLine(img, bx, by, ex, ey, color);
// ED
gdImageLine(img, ex, ey, dx, dy, color);
// DA
gdImageLine(img, dx, dy, ax, ay, color);
*/
ax = center_x;
ay = center_y - sun_radius;
bx = center_x - sun_radius*tan(15*deg);
by = center_y;
ex = center_x;
ey = center_y + sun_radius;
dx = center_x + sun_radius*tan(15*deg);
dy = center_y;
// 確定單一菱形區域的塗色正確後, 利用迴圈每次轉動 30 度, 總共轉六次即可塗上十二道光芒區域
for (int i=1;i<=6;i++){
// A
points[0].x = ax+sun_radius*sin(30*deg*i);
points[0].y = ay+sun_radius-sun_radius*cos(30*deg*i);
// B
points[1].x = bx+sr-sr*cos(30*deg*i);
points[1].y = by-sr*sin(30*deg*i);
// E
points[2].x = ex-sun_radius*sin(30*deg*i);
points[2].y = ey-(sun_radius-sun_radius*cos(30*deg*i));
// D
points[3].x = dx-(sr-sr*cos(30*deg*i));
points[3].y = dy+sr*sin(30*deg*i);
// 對菱形區域範圍塗色
gdImageFilledPolygon(img, points, 4, color);
// 在菱形區域外圍畫線, 明確界定菱形範圍
gdImagePolygon(img, points, 4, color);
}
}

美國國旗(gd_usa_flag.c)

#include <stdio.h>
#include <gd.h>
#include <math.h>

void draw_usa_flag(gdImagePtr img);
void draw_star(gdImagePtr img, int x, int y, int size, int color, double rotation_angle);

int main() {
    int width = 800;
    int height = (int)(width / 1.9);

    gdImagePtr img = gdImageCreateTrueColor(width, height);
    gdImageAlphaBlending(img, 0);

    draw_usa_flag(img);

    FILE *outputFile = fopen("./../images/usa_flag.png", "wb");
    if (outputFile == NULL) {
        fprintf(stderr, "打开输出文件时出错。\n");
        return 1;
    }

    gdImagePngEx(img, outputFile, 9);
    fclose(outputFile);
    gdImageDestroy(img);

    return 0;
}

void draw_usa_flag(gdImagePtr img) {
    int width = gdImageSX(img);
    int height = gdImageSY(img);
    int red, white, blue;
    // 国旗颜色
    red = gdImageColorAllocate(img, 178, 34, 52); // 红色条纹
    white = gdImageColorAllocate(img, 255, 255, 255); // 白色条纹
    blue = gdImageColorAllocate(img, 60, 59, 110); // 蓝色矩形

    int stripe_height = height / 13;
    int stripe_width = width;
    int star_size = (int)(0.0308 * height); // 星星大小

    for (int y = 0; y < height; y += stripe_height) {
        if (y / stripe_height % 2 == 0) {
            gdImageFilledRectangle(img, 0, y, stripe_width, y + stripe_height, red);
        } else {
            gdImageFilledRectangle(img, 0, y, stripe_width, y + stripe_height, white);
        }
    }

    gdImageFilledRectangle(img, 0, 0, width * 2 / 5, stripe_height * 7, blue);

    int star_spacing_x = (int)(0.129 * height); // 横向星星之间的间距
    int star_spacing_y = (int)(0.054 * height); // 纵向星星之间的间距
    int star_start_x = (int)(0.125 * height); // 星星的起始X位置
    int star_start_y = (int)(0.0485 * height); // 星星的起始Y位置

    for (int row = 0; row < 9; row++) {
        int starsPerRow = (row % 2 == 0) ? 6 : 5;

        // 计算2、4、6和8排星星的偏移量
        int offset_x = (row % 2 == 0) ? star_spacing_x / -2 : 0;

        for (int star = 0; star < starsPerRow; star++) {
            int x = star_start_x + star * star_spacing_x + offset_x;

            // 旋转角度(以弧度为单位)
            double rotation_angle = M_PI / 5; // 忘記多少度的旋转

            int y = star_start_y + row * star_spacing_y;
            draw_star(img, x, y, star_size, white, rotation_angle);
        }
    }
}

void draw_star(gdImagePtr img, int x, int y, int size, int color, double rotation_angle) {
    gdPoint points[10];

    for (int i = 0; i < 10; i++) {
        double angle = M_PI / 2 + i * 2 * M_PI / 10 + rotation_angle;
        int radius = (i % 2 == 0) ? size : size / 2;
        points[i].x = x + radius * cos(angle);
        points[i].y = y + radius * sin(angle);
    }

    // 用指定的颜色填充星星
    gdImageFilledPolygon(img, points, 10, color);
}

日本國旗(gd_jp_flag.c)

#include <stdio.h>
#include <gd.h>
#include <math.h>

void draw_japan_flag(gdImagePtr img);
void draw_circle(gdImagePtr img, int x, int y, int radius, int color);

int main() {
    int width = 1200;
    int height = (int)(width * 2.0 / 3.0);

    gdImagePtr img = gdImageCreateTrueColor(width, height);
    gdImageAlphaBlending(img, 0);

    draw_japan_flag(img);

    FILE *outputFile = fopen("/tmp/japan_flag.png", "wb");  // 保存到 /tmp 目录
    if (outputFile == NULL) {
        fprintf(stderr, "Error opening the output file.\n");
        return 1;
    }

    gdImagePngEx(img, outputFile, 9);
    fclose(outputFile);
    gdImageDestroy(img);

    return 0;
}

void draw_japan_flag(gdImagePtr img) {
    int width = gdImageSX(img);
    int height = gdImageSY(img);
    int white, red;

    int circle_radius = width / 5;
    int circle_x = width / 2;
    int circle_y = height / 2;

    white = gdImageColorAllocate(img, 255, 255, 255); // 白色
    red = gdImageColorAllocate(img, 255, 0, 0);       // 红色

    // 画白色背景
    gdImageFilledRectangle(img, 0, 0, width, height, white);

    // 画红色圆
    draw_circle(img, circle_x, circle_y, circle_radius, red);
}

void draw_circle(gdImagePtr img, int x, int y, int radius, int color) {
    gdImageFilledEllipse(img, x, y, radius * 2, radius * 2, color);
}

gdhello

#include <stdio.h>
#include <gd.h>
#include <math.h>

int main() {
int width = 800;
int height = 600;

gdImagePtr img = gdImageCreateTrueColor(width, height);
gdImageAlphaBlending(img, 0);

FILE *outputFile = fopen("hellogd.png", "wb");
if (outputFile == NULL) {

fprintf(stderr, "Error opening the output file.\n");

return 1;
}

int red = gdImageColorAllocate(img, 255, 0, 0);
int blue = gdImageColorAllocate(img, 0, 0, 255);
int black = gdImageColorAllocate(img, 0, 0, 0);
int white = gdImageColorAllocate(img, 255, 255, 255);
// 長方形塗色
gdImageFilledRectangle(img, 0, 0, width, height, white);
gdImageFilledRectangle(img, 0, 0, (int)width/4, (int)height/4, blue);
// 橢圓形塗色
gdImageFilledEllipse(img, (int)width*3/4, (int)height/4, (int)width/4, (int)width/4, red);
// 橢圓形畫線
gdImageEllipse(img, (int)width*3/4, (int)height*3/4, (int)width/4, (int)width/4, red);
// 畫直線
gdImageLine(img, (int)width/2, (int)height/2, (int)width/2, (int)height/2 + 100, blue);

// 多邊形畫線
gdPoint points[4];
points[0].x = (int)width/4;
points[0].y = (int)height*3/4;
points[1].x = points[0].x + 100;
points[1].y = points[0].y;
points[2].x = points[1].x;
points[2].y = points[1].y + 100;
points[3].x = points[2].x - 100;
points[3].y = points[2].y;
gdImagePolygon(img, points, 4, black);

// 多邊形塗色
gdPoint points2[4];
points2[0].x = (int)width/3;
points2[0].y = (int)height/2;
points2[1].x = points2[0].x + 100;
points2[1].y = points2[0].y;
points2[2].x = points2[1].x;
points2[2].y = points2[1].y + 100;
points2[3].x = points2[2].x - 150;
points2[3].y = points2[2].y;
gdImageFilledPolygon(img, points2, 4, red);

gdImagePngEx(img, outputFile, 9);
fclose(outputFile);
gdImageDestroy(img);
return 0;
}

replit.nix必要的繪圖程式:

{ pkgs }: {
    deps = [
      pkgs.libev
      pkgs.gnuplot
      pkgs.ncurses.dev
      pkgs.gd
    ];
  env = {
    PYTHON_LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath [
      pkgs.libev
    ];
  };
}

繪製過程:

在shell打上cd downloads

意思是進入downloads檔案

再打上 cc 文件名稱.c -lgd -lm

在這個命令中: 文件名稱.c 是你的 C 語言原始碼檔案的名稱

-lgd 和 -lm 分別指定連結 GD 圖形庫和數學庫

最後再打./a.out

運行這個可執行文件

例:

cd downloads

cc gd_jp_flag.c -lgd -lm

./a.out

這樣就可以繪製出圖片了


Work << Previous Next >> experience

Copyright © All rights reserved | This template is made with by Colorlib