【问题标题】:Plotting simple points绘制简单点
【发布时间】:2019-10-05 12:47:21
【问题描述】:

我来这里是为了了解如何使用这个简单的代码在 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)
{
}

【问题讨论】:

  • 实现这一点的一种方法是在二维数组中渲染完整的图形并打印二维数组。
  • 请详细说明“终端打印点”的含义。你会对“x”和“”的ascii艺术感到满意吗?这可能会将RANGE 限制在 50 以下。
  • @Yunnosch 我们可以通过将点标准化到所需平面来渲染有限平面中的任何点
  • @Yunnosch 我详细说明了一点。我的意思是打印图片
  • 我还不清楚。也许只是说明 Madhusoodan P 的答案所描述的印刷 ascii 艺术是否是你想要的。我问,因为我有一种感觉,你想要比这更高的分辨率。您的编辑“看起来像图表”可以双向进行。如果答案是您想要的,请考虑接受它。 stackoverflow.com/help/someone-answers 否则评论它并解释它缺少什么来满足你。

标签: c plot terminal points


【解决方案1】:

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

编辑:我也添加了缩放代码。

#include <stdio.h>

#define MAXWIDTH    30
#define MAXHEIGHT   30

#define MAXX        100
#define MAXY        100

#define NPTS 3

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

    /* points to be plotted */
    int pts[NPTS][2] = {
        {20,90},
        {94,39},
        {15, 5}
    };

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

    /* find out the scaling factor */
    double scalex=(double)MAXWIDTH/MAXX;
    double scaley=(double)MAXHEIGHT/MAXY;

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

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

    /* print the bottom line */
    printf("\n     ");
    for(int i=0; i<MAXWIDTH; i++)
        putchar('|');
    putchar('\n');
}

输出(如果需要,您可以添加更多装饰性内容):

   +                              
   +    *                         
   +                              
   +                              
   +                              
   +                              
   +                              
   +                              
   +                              
   +                              
   +                              
   +                            * 
   +                              
   +                              
   +                              
   +                              
   +                              
   +                              
   +                              
   +                              
   +                              
   +                              
   +                              
   +                              
   +                              
   +                              
   +                              
   +      *                       
   +                              
   +                              
     ||||||||||||||||||||||||||||||

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

【讨论】:

  • 对我来说,你的规范化概念需要更多解释,也许有例子和没有例子。
  • 对不起,我的意思是缩放。例如,在大小为 100x100 的平面中的 (20,40) 可以渲染为在 50x50 的平面中的 (10,20)。
猜你喜欢
  • 2013-12-25
  • 2016-01-17
  • 2013-04-05
  • 2012-09-07
  • 2015-12-15
  • 2010-10-26
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
相关资源
最近更新 更多