(在C中绘制简单点

问题描述 投票:0回答:1

我来这里是为了了解在给定此简单代码的情况下如何在C中绘制简单点。我只想构造plot_points方法。尽管我没有完整的代码,但是代码想要做的是

1得到数字n

随机修复2个n点

终端中有3个打印点

并且经过漫长的搜索,我错过了第三步。

感谢阅读

typedef struct
{
int x;
int y;
} point;


srand( time(NULL));
for (int j = 0; j < n; j++)
{
    x = rand() % RANGE + 1;
    y = rand() % RANGE + 1;

    p[j].x = x;
    p[j].y = y;
}

void print_points( point *p, int n)
{
}
c plot terminal points
1个回答
0
投票

我们可以在2D数组上绘制图形,然后将数组刷新到屏幕的最后。这是终端中绘制的图形的最简单实现。假设point.x和point.y介于0到MAXWIDTH和MAXHEIGHT之间。我们可以通过将点归一化到有限平面上来将这个想法扩展到巨大的点,但是需要进行精确的权衡。

#include <stdio.h>

#define MAXWIDTH 30
#define MAXHEIGHT 30
#define NPTS 3

int main() {
    /* our limited plane */
    char G[MAXHEIGHT][MAXWIDTH];

    /* points to be plotted */
    int pts[NPTS][2] = {
        {20,3},
        {4,29},
        {15, 18}
    };

    /* clean up the plane */
    for(int i=0; i<MAXHEIGHT; i++)
        for(int j=0; j<MAXWIDTH; j++)
            G[i][j]=' ';

    /* render the points on plane here, normalization must be applied here */
    for(int i=0; i<NPTS; i++) {
        G[pts[i][0]][pts[i][1]] = '*';
    }

    /* render to screen */
    for(int i=0; i<MAXHEIGHT; i++) {
        for(int j=0; j<MAXWIDTH; j++) {
            putchar(G[i][j]);
        }
        putchar('\n');
    }
}

[如果需要,可以通过将SVG写入文件来生成HTML图形。我在这里做了小的实现https://github.com/mmpataki/mchartjs/tree/master/cport

© www.soinside.com 2019 - 2024. All rights reserved.