zoukankan      html  css  js  c++  java
  • 在固定长方形里产生渐变

    /* CPSC 2100: redgreen.c

    This program creates a ppm file for a 600x400 box, that is red
    in the left half and green in the right half.

    The ppm image is written to the file specified on the command
    line, e.g. the program is initiated as:

    ./redgreen output-filename

    which would create the PPM image file specified by output-filename

    */

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <memory.h>
    #include <assert.h>
    #include "rgb.h"


    int main(int argc, char *argv[]) {
    pixel_t *img; /* image array */
    //int rowNdx, colNdx; /* Row and column indices */
    FILE *imageFP; /* PPM output file */

    /* Open the PPM output file */
    if (argc != 4)
    {
    fprintf(stderr, "Usage: ./redgreen columns rows output-filename ");
    exit(1);
    }

    imageFP = fopen(argv[3], "w");
    assert(imageFP != NULL);
    int rows = 400;
    int columns = 600;
    rows = atoi(argv[2]);
    columns = atoi(argv[1]);
    double ratio = columns/rows;

    double maxdistance = sqrt((ratio*(rows - 1)) * (ratio*(rows - 1)) + (columns - 1) * (columns - 1));
    double distance;


    img = (pixel_t *) malloc(sizeof(pixel_t) * columns * rows);
    pixel_t *pixaddr = NULL;


    /* Write the ppm header */
    char ss[100];
    memset(ss, 0, sizeof(char) * 100);
    sprintf(ss, "P6 %d %d 255 ", columns, rows);

    fprintf(imageFP, ss);
    int r,c;
    for (r = 0; r < rows; r++)
    {
    for (c = 0; c < columns; c++)
    {
    distance = sqrt((ratio*r) * (ratio*r) + c * c);
    pixaddr = img + r * columns + c;
    pixaddr->red = (1 - distance/maxdistance)*255;
    pixaddr->blue = distance/maxdistance*255;
    pixaddr->green = 0;
    }
    }

    /* Write out the pixel */
    fwrite(img, sizeof(pixel_t), columns * rows, imageFP);
    free(img);
    return 0;
    }

    rgb.h

    typedef struct pixel_type
    {
    unsigned char red;
    unsigned char green;
    unsigned char blue;
    } pixel_t;

    参考

    http://people.cs.clemson.edu/~rlowe/cs2100/homework/spr14/hw1/hw1.shtml

  • 相关阅读:
    redux dispatch、action、reduce 执行流程
    react中使用react-redux
    npm 全局安装默认地址
    react 组件外js文件路由跳转
    withRouter的作用和适用场景
    react 自定义高阶组件,实现路由拦截,子路由渲染
    移动端原生js使用touch事件监听滑动方向
    Vue.js中this.$nextTick()的使用与理解
    域名等级划分介绍
    nodejs 实现一个账号只能一台设备登录
  • 原文地址:https://www.cnblogs.com/yuanxiaoping_21cn_com/p/3647438.html
Copyright © 2011-2022 走看看