这概括了问题Shape任何 ASCII 艺术形状的接口,以及Spruce
记录云杉形状的位置和高度的类intersects(x,y)
确定云杉上是否有任何坐标。
然后传入list就可以更灵活的绘图了Spruce
并确定所有大小的并集的每个点的命中/未命中。 ASpruce
可以整齐地定义为JDK16记录:
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;
}
}
...以及一个简单的主线来绘制尽可能多的Spruce
/ 其他Shape
如所须:
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]
*
*** *
***** ***
******* *****
********* *******
*********** * *********
************* *** ***********
******************** *************
********************** ***************
************************ *****************