【问题标题】:C - How to draw outline of adjacent strips of rectangles in asterisksC - 如何在星号中绘制相邻矩形条的轮廓
【发布时间】:2017-03-11 16:46:14
【问题描述】:

输出应该是这样的:

                   *********************
                   *                   *
                   *                   *
                   *                   *
                   *                   *
**********  ********                   *
*        *  *                          *
*        *  *                          *    
*        ****                          *
*                                      *
*                                      *  
*                                      *
*                                      *
*                                      *
*                                      *

输入是坐标列表(开始,高度),其中开始代表左 x 坐标,高度是条带的高度。我编码的内容:

//Input: ((1, 10), (10, 7), (13, 16), (20, 15), (40, 0))
int beginArr[NUM_STRIPS];//sorted array of the x-coordinates
int heights[NUM_STRIPS]; //height/y-coordinates
//Assume that the tallest strip is 40
for(int i = 0; i < 40; i++){
    for(int j = 0; j < beginArr[NUM_STRIPS-1]; j++){
        for(int k = 0; k < NUM_STRIPS-1; k++){
            if(isElementOf(beginArr, j) && i >= heights[k] && i <= heights[k+1]){
                printf("*");
            }else if(isElementOf(heights, i) && j >= beginArr[k] && j <= beginArr[k+1]){
                printf("*");
            }else{
                printf(" ");
            }
         }
    }
    printf("\n");
}

我的代码非常不正确,因为它没有考虑新片段的开始或终止时间和地点。逻辑部分是我卡住的地方,因此非常感谢任何帮助。

编辑:

x,y 坐标可以想象成一个二维数组。在上面的例子中:

int xy[][2] = {{1, 10}, {10, 7}, {13, 16}, {20, 15}, {40, 0}};

根据我的解释,最左边的条形矩形从 x = 1 ({1, 10}) 开始并保持高度为 10 直到 x = 10 ({10, 7}),这意味着 x =从 1 到 10,高度 = 10。第二条带从 x = 10 ({10, 7}) 开始,并保持高度为 7,直到 x = 13 ({13, 16}),依此类推。

【问题讨论】:

  • 嗯,如果您不包含完整的最小示例,则很难通过您的代码进行推理。不过我会试试的。
  • 嗯,你怎么知道每个条的宽度?您似乎没有将其存储在任何地方,但从您的图像来看,您似乎需要它。
  • @DanFarrell,请看我的编辑 :)
  • 宽度可以从下一条开始,除了最后一条。那么最后一个条带是否总是需要具有 0 高度?
  • 是的,它总是 0。

标签: c arrays for-loop plot printing


【解决方案1】:

这就是我想出的。希望内联 cmets 应该很清楚这里发生了什么。

我已经填充了矩形,将其留作练习,您可以仅将它们的开头和结尾填充到数据中,或者仅绘制 空白区域旁边的矩形。

您还必须更改输入。

我认为我正在做的事情和你正在做的事情之间的最大区别在于我对数据进行了几次不同的传递,首先计算每个矩形的宽度(使未来的计算更简单、更直接)和情节的大小,第二个在内存中填写情节。请注意,我将其反向绘制,然后先绘制顶线,以简化绘制逻辑,但这可以更改。最后,我先将数据绘制到内存中,然后在屏幕上绘制,这样可以简化在绘制星号之前插入多少空格的逻辑。

输出,对 stderr 提供有用的信息:

$ gcc -o rects t.c --std=c99 && ./rects
Plot is 20 by 16
Strip 1/8, 2 x 3 @ 0
Strip 2/8, 3 x 10 @ 2
Strip 3/8, 4 x 16 @ 5
Strip 4/8, 2 x 7 @ 9
Strip 5/8, 4 x 1 @ 11
Strip 6/8, 2 x 12 @ 15
Strip 7/8, 3 x 6 @ 17
Strip 8/8, 0 x 0 @ 20
_____****___________
_____****___________
_____****___________
_____****___________
_____****______**___
_____****______**___
__*******______**___
__*******______**___
__*******______**___
__*********____**___
__*********____*****
__*********____*****
__*********____*****
***********____*****
***********____*****
********************

最后,好东西:

#include <stdio.h>
#include <stdlib.h>

/*
instead of drawing from the top down,
wouldn't it be easier to plot the image into
memory upside down, and then print the lines
in reverse?
*/

// the strip is assumed to end when the next strip starts
// width can therefore be precomputed which will
// simplify the display logic significantly.
typedef struct {
    unsigned int start;
    unsigned int height;
    unsigned int width;
} strip;

// for simplicity, I'll assume these are in order of
// starting position.  They could be sorted to avoid
// this assumption
int main(int argc, char **argv) {
    // we could compute this if we were
    // taking dynamic input
    unsigned int striplen = 8;
    strip strips[] = {
        { 0,  3, 0 },
        { 2, 10, 0 },
        { 5, 16, 0 },
        { 9,  7, 0 },
        { 11, 1, 0 },
        { 15,12, 0 },
        { 17, 6, 0 },
        { 20, 0, 0 }
    };
        // the width of the last strip is 0 and its height must be 0 to 'finish' the strips
        // otherwise we'd actually need to konw the widths of each strip ( which would
        // be a more complete model for the data, but we'll make do without it  by
        // making the assumption that the last strip is 0)

    // we'll discover the "size" of the full plot
    // and set up the widths
    unsigned int plot_width = 0;
    unsigned int plot_height = 0;
    unsigned int laststart = 0;
    for( unsigned int i = 0; i < striplen; i++){
        if( plot_height < strips[i].height ) {
            plot_height = strips[i].height;
        }
        if( i > 0 ){
            // set the width of the previous strip from the difference of their
            // starting positions
            strips[i-1].width = strips[i].start - strips[i-1].start;
            // we can now add the width to the total.  Since the
            // width and height of the last strip are required to be 0,
            // we don't need to worry about the fact that the last width isn't
            // taken into account.
            plot_width += strips[i-1].width;
        }
    }
    // plot the rectangles filled, because it's easier to deal with
    // their intersections that way.
    // when we draw them, we can use the in-memory plot to decide
    // whether an asterisk is an edge or fill
  fprintf( stderr, "Plot is %u by %u\n", plot_width, plot_height );
    char* plot_data = calloc( plot_height * plot_width, sizeof(char) );
    for( unsigned int i = 0; i < striplen; i++){
    fprintf( stderr, "Strip %u/%u, %u x %u @ %u \n", i+1, striplen, strips[i].width, strips[i].height, strips[i].start );
        for( unsigned int x = strips[i].start; x < strips[i].start + strips[i].width; x++){
            for( unsigned int y = 0; y < strips[i].height; y++){
                plot_data[plot_width * y + x] = '*';
            }
        }
    }
    // now we can finally draw it, in reverse order to make it right side up
    for( signed int y = plot_height - 1; y >= 0; y--){
        for( unsigned int x = 0; x < plot_width; x++){
            printf("%c", ( plot_data[y * plot_width + x] == 0 ? '_' : '*' ) );
      }
      printf("\n");
  }
    return 0;
}

【讨论】:

    猜你喜欢
    • 2013-12-03
    • 1970-01-01
    • 2012-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 2012-03-16
    • 1970-01-01
    相关资源
    最近更新 更多