求一个暂定大小的ASCII艺术程序的whitespace算法

Finding the white space algorithm for a tentative size ASCII art program

所以我们要设计一本 ASCII 艺术书,我快完成了,但我想不出一件小事:“Building Java Programs”两边的间距

Here is what the book needs to look like

到目前为止,这是我的代码(为了便于帮助,我只展示了需要间距帮助的方法。假设 drawLine() 将虚线均匀地绘制到 SIZE 常量)

//constant SIZE = 8
public static void drawBottom() {
    //Dash line on top of the bottom portion of the book
    drawLine();
    //Printing first set of rightmost "/"'s
    for (int i = 1; i <= SIZE; i++)
        System.out.print("/");
    System.out.println();
    for (int i = 1; i <= SIZE / 2; i++) {
        //Leftmost pipe
        System.out.print("|");
    //    TO DO: Code label of book
    //    for (int j = 1; j <= ; j++) {
    //
    //    }
        //This loop is only here for example.
        //To show I can fill the space but need
        //the words in the space
        for (int j = 1; j <= SIZE * 3; j++) {
            System.out.print(" ");
        }
        //Rightmost pipe
        System.out.print("|");
        //"Pages" to right of label
        for (int j = 1; j <= -2 * i + (SIZE + 2); j++) {
            System.out.print("/");
        }
        //Move to draw next row
        System.out.println();
    }
    //Dash line on very bottom of entire drawing
    drawLine();
}

Here is my output (when SIZE = 8)

如何确定“构建 Java 程序”文本块的左右间距?

我只知道当SIZE = 8时,两边各有一个space

当SIZE = 10时,两边各有4个space

当SIZE = 13时,两边各有8个space

什么算法可以帮到我?

每行可分为三个区域:第一个区域由spaces组成,位于包含索引A到包含索引B之间。第二个区域包含从包含索引 C 到包含索引 D 的文本。第三个区域再次由 space 组成,位于从包含索引 E 到包含索引 F 之间。侧翼管道位于索引 0width + 1:

|A......BC......DE......F|

第一个和第三个区域的长度为width/2 - text/2,其中text表示文本的长度。

则索引为:

Index A: 1
Index B: width/2 - text/2  
Index C: B + 1
Index D: width/2 + text/2
Index E: D + 1
Index F: width

在循环中,可以在相应区域显示所需的字符:

// Code: label of book
int width = 3 * SIZE; 
width = (width % 2 == 0) ? width : width - 1;           // if the width is odd, choose the next smallest even number 
String text = "Building Java Programs";
for(int j = 1; j <= width; j++) {
    if (j <= width / 2 - text.length() / 2) {           // j <= Index B
        System.out.print(" ");
    }
    else if (j >= width / 2 + text.length() / 2 + 1) {  // j >= Index E = Index D + 1    
        System.out.print(" ");
    }
    else {
        System.out.print(text);
        j = width / 2 + text.length() / 2;              // j = Index D
    }
}

当然,第一条和第二条if语句也可以组合实现

输出(SIZE = 8width = 24):

Without text...
|                        |////////
|                        |//////
|                        |////
|                        |//

With text...
| Building Java Programs |////////
| Building Java Programs |//////
| Building Java Programs |////
| Building Java Programs |//

两边各有一个space(1 + 22 + 1 = 24)。

输出(SIZE = 10width = 30):

Without text...
|                              |//////////
|                              |////////
|                              |//////
|                              |////
|                              |//

With text...
|    Building Java Programs    |//////////
|    Building Java Programs    |////////
|    Building Java Programs    |//////
|    Building Java Programs    |////
|    Building Java Programs    |//

两边各有四个space(4 + 22 + 4 = 30)。

输出(SIZE = 13width = 38):

Without text...
|                                      |/////////////
|                                      |///////////
|                                      |/////////
|                                      |///////
|                                      |/////
|                                      |///

With text...
|        Building Java Programs        |/////////////
|        Building Java Programs        |///////////
|        Building Java Programs        |/////////
|        Building Java Programs        |///////
|        Building Java Programs        |/////
|        Building Java Programs        |///

两边各有八个space(8 + 22 + 8 = 38)。

我想通了!我需要使用的方程一直是斜截距!

尺寸 = 8,space = 1

尺寸 = 10,space = 4

将它们转化为积分 (8, 1) 和 (10, 4)

记住斜截式 y = mx + b

求 m m = (y2 - y1)/(x2 - x1)

米 = (4 - 1)/(10 - 8)

米=3/2

使用任一点求解 b。我将使用 (8, 1)

1 = 3/2 x 8 + b

b = 1 - (3/2)(8)

b = -11

利润! y = 3/2x - 11

或者在我们的例子中...

for(int j = 1; j <= (3*SIZE)/2 - 11; j++)
{
    System.out.print(" ");
}

System.out.print("Building Java Programs");

//put same loop here again