首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绘制两棵特定高度的ASCII云杉树

绘制两棵特定高度的ASCII云杉树
EN

Stack Overflow用户
提问于 2015-11-20 05:53:53
回答 3查看 572关注 0票数 4

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

代码语言:javascript
复制
           *
          ***
         *****
        *******      *
       *********    ***
      ***********  *****
     ********************
    **********************
   ************************
  **************************
 ****************************
******************************

代码需要能够基于输入高度生成此形状。正如您从示例形状中看到的,我的代码使用线条高度12正确地生成了ASCII图片。它不能正确地生成ASCII形状。我试着自己调试它,但我找不到失败的线高度之间的共性,这阻止了我用我的算法解决问题。

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

代码语言:javascript
复制
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艺术作品?

EN

回答 3

Stack Overflow用户

发布于 2021-06-25 17:59:12

对于任何ASCII艺术形状都有一个Shape接口,Spruce类使用intersects(x,y)记录云杉形状的位置和高度,以确定是否有坐标位于云杉上,从而概括了这个问题。

然后,通过传入Spruce列表并确定所有大小联合的每个点上的命中/未命中,绘图可以更灵活。可以将记录整齐地定义为JDK16 Spruce

代码语言:javascript
复制
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 /其他Shape

代码语言:javascript
复制
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));
}

正在返回:

代码语言:javascript
复制
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]

           *                                  
          ***                        *        
         *****                      ***       
        *******                    *****      
       *********                  *******     
      ***********    *           *********    
     *************  ***         ***********   
    ********************       *************  
   **********************     *************** 
  ************************   *****************
票数 1
EN

Stack Overflow用户

发布于 2021-06-28 23:33:37

绘制ASCII云杉林

首先,指定图片的尺寸,然后指定云杉树顶点的坐标。

云杉林:

代码语言:javascript
复制
                                                       *                 *      
     *                                     *          ***      *        ***     
    ***                         *         ***        *****    ***      *****    
   *****      *                ***       *****      *******  *****    *******   
  *******    ***              *****     *******    ****************  *********  
 *********  *****     *      *******   *********  ***************************** 
******************   ***    ********* ******************************************
******************* *****  *****************************************************
********************************************************************************

Try it online!

代码语言:javascript
复制
class SpruceForest {
    int width, height, plat[][];
    public SpruceForest(int width, int height) {
        this.width = width;
        this.height = height;
        this.plat = new int[height][width];
    }
    public SpruceForest addSpruce(int x, int y) {
        for (int i = 0; i < height - y; i++)
            for (int j = -i; j <= i; j++)
                if (x + j >= 0 && x + j < width)
                    this.plat[y + i][x + j] = 1;
        return this;
    }
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (int[] row : this.plat) {
            for (int i : row)
                sb.append(i == 1 ? "*" : " ");
            sb.append("\n");
        }
        return sb.toString();
    }
    public static void main(String[] args) {
        SpruceForest forest = new SpruceForest(80, 9)
                .addSpruce(5, 1).addSpruce(14, 3).addSpruce(22, 5)
                .addSpruce(32, 2).addSpruce(43, 1).addSpruce(55, 0)
                .addSpruce(63, 1).addSpruce(73, 0);
        System.out.println(forest);
    }
}
票数 1
EN

Stack Overflow用户

发布于 2021-06-20 11:31:03

假设第二棵云杉比第一棵云杉小四分之一,第二棵云杉与第一棵云杉的高度相距。

代码语言:javascript
复制
public static void main(String[] args) {
    for (int h = 3; h <= 12; h = h + 3)
        drawTwoSpruces(h);
}
代码语言:javascript
复制
public static void drawTwoSpruces(int h1) {
    int h2 = h1 * 3 / 4;
    System.out.println("h1=" + h1 + "; h2=" + h2);
    for (int i = 0; i <= h1; i++) {
        for (int j = -h1; j <= h2 * 2 + 1; j++)
            if (Math.abs(j) <= i || Math.abs(j - h2 - 1) <= i - h1 + h2)
                System.out.print("*");
            else
                System.out.print(" ");
        System.out.println();
    }
}

输出:

代码语言:javascript
复制
h1=3; h2=2
   *     
  *** *  
 ******* 
*********
代码语言:javascript
复制
h1=6; h2=4
      *         
     ***        
    *****  *    
   **********   
  ************  
 ************** 
****************
代码语言:javascript
复制
h1=9; h2=6
         *             
        ***            
       *****           
      *******   *      
     ********* ***     
    ***************    
   *****************   
  *******************  
 ********************* 
***********************
代码语言:javascript
复制
h1=12; h2=9
            *                   
           ***                  
          *****                 
         *******      *         
        *********    ***        
       ***********  *****       
      ********************      
     **********************     
    ************************    
   **************************   
  ****************************  
 ****************************** 
********************************
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33815056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档