Ding

Funny triangle

000

测试画布尺寸:width = height * 2 - 1, height >= 2

001

虽然名字是 Funny triangle,但是,真的,这!篇!文!章!一!点!都!不!好!玩!可能是我觉得从学校毕业两年之后写这种文章,实在是有点丢人 orz.

但是!

如果在一个神奇的背景之下,比如玩某行世界,遇见一张用 会动的耳朵 的交换卡,我祭出一招 输出倒三角 与之匹配, ding 匹配成功,所以那就开始吧~

010

静态的

其实要教一个不是计算机专业也不懂写程序的人来说,输出倒三角也没什么难度的,比如可以这样:

static triangle

1
2
3
4
5
6
private void staticTriangle() {
System.out.println("* * * * * * *");
System.out.println(" * * * * * ");
System.out.println(" * * *");
System.out.println(" *");
}

惊不惊喜!意不意外!

静态进阶版

本着为人民负责,为我 X 负责的精神,我就稍微努力一下吧:

static triangle2

1
2
3
4
5
6
7
8
9
10
11
12
13
private void anotherStaticTriangle() {
for (int i = 0; i < 4; i++) {
if (i > 0) {
for (int x = i; x > 0; x--) {
System.out.print(" ");
}
}
for (int j = (7 - i * 2); j > 0; j--) {
System.out.print("* ");
}
System.out.println();
}
}

如果…

如果这篇文章被我的小伙伴不小心看到了的话…那我 某大人某老湿 的称号怎么办,那以后的同学聚会我是去还是不去了…

幸好之前看过 Milo yip如何用 C 语言画这个图 文章,赶紧翻翻去…

outline

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private void iLoveMiloYip() {
int width = 23;
int height = 12;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
System.out.print(foo(x, y) ? "* " : " ");
}
System.out.println();
}
}
private boolean foo(int x, int y) {
return x == y || Math.abs(x - 22) == y;
}

上面那样并不完美 v1.0.0

可是上面那个,嗯,并没有底边,而且只是一个 outline,那么,剩下的就只能自己来喽…

solid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private void mTriangle() {
int width = 23;
int height = 12;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (x == y) {
while (true) {
if (Math.abs(x - 22) == y) {
System.out.print("* ");
break;
}
System.out.print("* ");
x++;
}
}
System.out.print(" ");
}
System.out.println();
}
}

上面那样并不完美 v2.0.0

你也觉得上面那种代码,丑陋,对吧?可是怎么样才能像 Milo yip 那样优雅呢?我想这一定是有办法的:

solid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private void iLoveMiloYip() {
int width = 23;
int height = 12;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
System.out.print(foo(x, y) ? "* " : " ");
}
System.out.println();
}
}
private boolean foo(int x, int y) {
return Math.abs(x - 22) >= y && x >= y;
}

题外话

outline 那里,利用 x == y || Math.abs(x - 22) == y 成功的在 二维平面 上画出了一个比较漂亮的倒三角,可是它没有底边,并且知识一个 边界,之后用程序思维也就是 v1.0.0 将那个 倒三角的边框给填满 了,但是代码太丑陋了,不是么?

那么如何优雅如 Milo yip 呢,也就是找一个函数式来表达这个三角形?既然我们知道边界了:

x == y || Math.abs(x - 22) == y

我们要做的就是把这个边界内的 像素给填满喽

Math.abs(x - 22) >= y && x >= y

当然,菜如狗的我就需要经历 v1.0.0 那样的过程了…

既然如此完美,那我们就玩些好玩的吧

Okay..我就像发现新大陆了一样,反正周末也无聊,下周一还是要去 擦(补)屁(公)股(文),找些乐子吧…

测试画布尺寸:width = height * 2 - 1, height >= 2

solid

solid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
private static final String DEFAULT_PIXEL = "* ";
private static final String BLANK = " ";
private enum Direction {
NORTH,
SOUTH,
EAST,
WEST
}
private void paintIsoscelesTriangle(int width, int height, Direction direction, boolean isSolid) {
switch (direction) {
case NORTH:
northDirection(width, height, isSolid);
break;
case SOUTH:
southDirection(width, height, isSolid);
break;
case EAST:
eastDirection(width, height, isSolid);
break;
case WEST:
westDirection(width, height, isSolid);
break;
}
}
private boolean outlinePainter(int x, int y, int offset) {
return x == y || Math.abs(x - offset) == y;
}
private boolean solidPainter(int x, int y, int offset) {
return Math.abs(x - offset) >= y && x >= y;
}
private void northDirection(int width, int height, boolean isSolid) {
int x, y;
for (y = height - 1; y > 0; y--) {
for (x = 0; x < width; x++) {
System.out.print(
(isSolid ? solidPainter(x, y, width - 1) : outlinePainter(x, y, width - 1))
? DEFAULT_PIXEL : BLANK);
}
System.out.println();
}
}
private void southDirection(int width, int height, boolean isSolid) {
int x, y;
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
System.out.print(
(isSolid ? solidPainter(x, y, width - 1) : outlinePainter(x, y, width - 1))
? DEFAULT_PIXEL : BLANK);
}
System.out.println();
}
}
private void eastDirection(int width, int height, boolean isSolid) {
int x, y;
for (y = 0; y < width; y++) {
for (x = 0; x < height; x++) {
System.out.print(
(isSolid ? solidPainter(y, x, width - 1) : outlinePainter(x, y, width - 1))
? DEFAULT_PIXEL : BLANK);
}
System.out.println();
}
}
private void westDirection(int width, int height, boolean isSolid) {
int x, y;
for (y = 0; y < width; y++) {
for (x = height - 1; x >= 0; x--) {
System.out.print(
(isSolid ? solidPainter(y, x, width - 1) : outlinePainter(x, y, width - 1))
? DEFAULT_PIXEL : BLANK);
}
System.out.println();
}
}

当然..horizontal并不完美..而且我还有一个大胆的想法..就

未完待续了~

P.S. 我也会动耳朵的 :)

你的认可是我最大的动力!