绘制两棵特定高度的 ASCII 云杉树 [英] Draw two ASCII spruce trees of specific heights

查看:51
本文介绍了绘制两棵特定高度的 ASCII 云杉树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写生成这种形状的 ASCII 艺术的代码:

I am trying to write code which generates ASCII art of this shape:

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

代码需要能够根据输入高度生成此形状.正如您从示例形状中看到的那样,我的代码使用 12 的行高正确生成了 ASCII 艺术.但是,对于 3、5、6、7、10、11、15 的行高,......它没有正确生成 ASCII 形状.我试图自己调试这个,但我找不到失败的行高之间的共性,这使我无法用我的算法确定问题.

The code needs to be able to generate this shape based upon an input height. As you can see from the example shape, my code correctly generates the ASCII art using a line height of 12. However, for line heights of 3, 5, 6, 7, 10, 11, 15,... it doesn't correctly generate the ASCII shape. I have tried to debug this myself, but I can't find a commonality between the failing line heights, which is preventing me from nailing down the problem with my algorithm.

这是我用来生成 ASCII 艺术形状的 Java 代码(当然没有硬编码的 12 行高):

This is the Java code which I am using to generate the ASCII art shape (though without a hard-coded line height of 12 of course):

int h = 12;
for (int i = 0; i < h; i++) {
    for (int j = 0; j < h - i; j++) {
        System.out.print(" ");
    }
    for (int j = 0; j < i; j++) {
        System.out.print("*");
    }
    for (int j = i; j >= 0; j--) {
        System.out.print("*");
    }
    for (int j = 0; j < h / 2 - i; j++) {
        System.out.print(" ");
    }
    for (int j = h / 2 - i; j > 0; j--) {
        System.out.print(" ");
    }
    for (int j = 0; j <= h / 2; j++) {
        if (i >= h / 2)
            System.out.print("*");
    }
    for (int j = 0; j < i - h / 5; j++) {
        if (i >= h / 4 && i < h / 2)
            System.out.print("*");
    }
    for (int j = i - h / 5 - 1; j > 0; j--) {
        if (i >= h / 4 && i < h / 2)
            System.out.print("*");
    }
    System.out.println();
}

究竟是什么导致我的 ASCII 艺术生成器代码在某些行高上失败,我该如何解决代码的问题,以便它正确地为任何正整数生成 ASCII 艺术?

What exactly is causing my ASCII art generator code to fail for certain line heights, and how do I fix that problem with the code so that it correctly generates ASCII art for any positive integer?

推荐答案

这通过使用 Shape 接口用于任何 ASCII 艺术形状,以及 Spruce 类来记录云杉形状的位置和高度与 intersects(x,y) 以确定是否有任何坐标位于云杉上.

This generalises the problem by having a Shape interface for any ASCII art shape, and Spruce class to record position and height of a spruce shape with intersects(x,y) to determine if any coordinates lie on the spruce.

然后通过传入Spruce列表并确定所有尺寸联合的每个点的命中/未命中,可以更灵活地绘制.一个 Spruce 可以整齐地定义为 JDK16 记录:

Then the drawing can be more flexible by passing in list of Spruce and determine the hit/miss at each point of the union of the sizes of all. A Spruce can be neatly defined as JDK16 record:

public record Spruce(int left, int height) implements Shape {
    public int right() { return left+2*height-2; }
    public int top()   { return height;          }

    // Check any coordinate hits / misses this Spruce:
    public boolean intersects(int x, int y) {
        return 1 <= y && y <= height && x >= left + y - 1 && x <= left + 2 * height - 1 - y;
    }
}

... 和一个简单的 main 来根据需要绘制尽可能多的 Spruce/other Shape:

... and a simple main to draw as many Spruce / other Shape as needed:

public static void main(String[] args) {
    Shape.draw(new Spruce(1, 4), new Spruce(6, 6));
    Shape.draw(new Spruce(2, 6), new Spruce(10, 4));
    Shape.draw(new Spruce(3, 10), new Spruce(30, 9), new Spruce(18, 5));
}

返回:

Spruce[left=1, height=4]
Spruce[left=6, height=6]

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

Spruce[left=2, height=6]
Spruce[left=10, height=4]

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

Spruce[left=3, height=10]
Spruce[left=30, height=9]
Spruce[left=18, height=5]

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

这篇关于绘制两棵特定高度的 ASCII 云杉树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆