I’ve learned how to print 2D shapes with ” * ” symbols by using for loops, thanks to a project I’ve been working on but I wanted to level up a bit. I want to create 3D shapes by using and combining 2D shapes.
For example, its easy to print a 2D square but I want to print a 3D cube. Same with rectangular prisms. I can print rectangles but can’t print rectangular prisms.
I know its not quite possible to print 3D things on a 2D terminal but it can at least look like its 3D by just adding some other shapes to a 2D shape.
I couldn’t find any solutions or ideas on how to print these things. It looks like it can be solved by some basic for loops but no matter what I tried, it didn’t work.
My idea is here:
Printing a square on the screen, then putting 2 different parallelograms on the side and the top so that it looks like its a 3D cube.
Same idea goes for rectangular prisms. First print a rectangle, then print 2 different parallelograms on the side and the top of it so it looks like its 3D.
First I tried the rectangular prism. I can’t print another parallelogram on the side that has the same lenght as the parallelogram on the top and the same height as the rectangle.
If I can learn how to print this one, I’ll try printing other 3D shapes that I can think of.
I could only make it this far and I can’t go further. What I’ve tried is here:
import java.util.Scanner;
public class Empty {
public static void rectangle() {
int a = 5;
int b = 8;
for (int i = 0; i < a; i++) {
if (i == 0) continue;
for (int j = 0; j < b; j++) {
if (j == 0 || j == (b - 1) || i == (a - 1)) {
System.out.print(" * ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
public static void parallelogram() {
int i, j, k;
int height = 5;
int width = 8;
for (i = 0; i < height; i++) {
for (j = 0; j < height - i - 1; j++) {
System.out.print(" ");
}
for (k = 0; k < width; k++) {
if (i == 0 || i == height - 1 || k == 0 || k == width - 1) {
System.out.print(" * ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
public static void main(String[] args) {
parallelogram();
rectangle();
}
}
This code block provides a shape that contains a parallelogram and a rectangle under it. What Im trying to make is connect right-top corner of the paralellogram and the right-bottom corner of the rectangle by using ” * ” symbols.
Its simply adding another parallelogram. Here’s the output:
* * * * * * * *
* *
* *
* *
* * * * * * * *
* *
* *
* *
* * * * * * * *
What I want is here:
Completely Zero. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1