-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangle.java
More file actions
125 lines (115 loc) · 4.41 KB
/
Copy pathTriangle.java
File metadata and controls
125 lines (115 loc) · 4.41 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package Assignment_1;
import java.util.Scanner;
public class Triangle {
private int triLength;
private int canvasWidth;
private int canvasHeight;
private DrawingCanvas canvas;
private int minLength; // shorter length of canvas
private int maxLength;
private String printingCharacter;
private int[] topLeftCoordinate = {0,0}; // longer length of canvas
/**
* Looping to draw the triangle on the Canvas space.
* @param triLength is the length of the triangle.
* @param canvasWidth
* @param canvasHeight
* w: Width; h: Height, to understand the loop easier.
*/
public Triangle(int triLength, int canvasWidth, int canvasHeight, String printingCharacter) {
this.triLength = triLength;
this.canvasWidth = canvasWidth;
this.canvasHeight = canvasHeight;
this.printingCharacter = printingCharacter;
}
public int canvasLength;
// Checking which sides of the canvas is longer or if it has same length
// true: canvasWidth > canvasHeight
//if (canvas.checkLength) {
// minLength = canvasHeight;
// maxLength = canvasWidth;
// false: canvasWidth < canvasHeight
//} else {
// minLength = canvasWidth;
// maxLength = canvasHeight;
//}
// Method to draw the triangle
public void drawingTriangle(String backgroundCharacter) {
int triLength = this.triLength;
for(int h=0; h < this.canvasHeight; h++) {
String line = "";
for(int w=0; w < this.canvasWidth; w++) {
if (w <triLength) {
line += this.printingCharacter;
}
else {
line += backgroundCharacter;
}
}
System.out.println(line);
triLength--;
}
}
public void moveTriangle(String move, String backgroundCharacter) {
if (move.equalsIgnoreCase("a")){
topLeftCoordinate[0] -= 1;
System.out.println(topLeftCoordinate[0]);
if (topLeftCoordinate[0] < 0) {
topLeftCoordinate[0] = 0;
System.out.println("You cannot move this triangle outside of the drawing canvas!");
}
}
if (move.equalsIgnoreCase("s")){
topLeftCoordinate[0] += 1;
System.out.println(topLeftCoordinate[0]);
if (topLeftCoordinate[0] > (this.canvasWidth - this.triLength)) {
topLeftCoordinate[0] -= 1;
System.out.println("You cannot move this triangle outside of the drawing canvas!");
}
}
if (move.equalsIgnoreCase("w")){
topLeftCoordinate[1] -= 1;
if (topLeftCoordinate[1] < 0) {
topLeftCoordinate[1] = 0;
System.out.println("You cannot move this triangle outside of the drawing canvas!");
}
}
if (move.equalsIgnoreCase("z")){
topLeftCoordinate[1] += 1;
if (topLeftCoordinate[1] > (this.canvasHeight - this.triLength)) {
topLeftCoordinate[1] -= 1;
System.out.println("You cannot move this triangle outside of the drawing canvas!");
}
}
int triLength = this.triLength;
for(int h=0; h < this.canvasHeight; h++) {
String line = "";
for(int w=0; w < this.canvasWidth; w++) {
if (h < topLeftCoordinate[1]) {
line += backgroundCharacter;
continue;
}
if (w < topLeftCoordinate[0]) {
line += backgroundCharacter;
continue;
}
if (w <triLength + topLeftCoordinate[0]) {
line += this.printingCharacter;
}
else {
line += backgroundCharacter;
}
}
System.out.println(line);
if (!line.equals(backgroundCharacter.repeat(canvasWidth))) {
triLength--;
}
}
}
public void setTriLength(int triLength) {
this.triLength = triLength;
}
public int getTriLength() {
return this.triLength;
}
}