PowerShell is an administrative programming language that I’ve used at my job in IT. It’s usually used to get information about computers in an IT environment or to change settings in Windows. However, it can be used to do a variety of things as I’ll show in future posts. PowerShell commands can be run from a command line environment or as part of a script file. The Draw-Shapes function I made uses the Write-Host command in the command line window. Normally, the Write-Host command is used to display a string or a variable. I used it to display one space at a time:
When I set the background color, it fills in the whole square given to each character. This is the equivalent of a pixel in my “command line graphics”. I used two colors, both of which are parameters to the function: a foreground for the circle, and a background, which can be different from PowerShell’s default blue. I created a function called Draw-Shapes that uses this to draw circles, squares, and triangles. The parameters are shape, foregroundcolor, backgroundcolor, stretchconstant, radius (for the circle and square), and angle1, angle2, angle3 (for the triangle).
Circle
The equation of a circle is
I wanted to determine how many spaces I needed to write based on the row number. In this case, the row number was x, which is confusing since it’s in the vertical direction, and the spaces were y.
was the base equation from which I calculated the spaces needed.
In each row, I calculated how many spaces of background color I needed to write before I showed the space with the foreground color for the left side of the circle. Then more background color spaces were written until it reached the foreground color for the right half of the circle. The last part was writing background color spaces to fill in the line so it ended at the same position on the right. Then I would go to the next line and do the same thing. I had to break the circle into two halves, each of which is a function of x. I did the top half of the circle with one loop and the bottom half with a second loop.
Square
Creating a square was easy. If the row is the first or last, the entire row is foreground color spaces. This makes the top and bottom sides. For the in between rows, write a foreground color space for the left side, a bunch of background color spaces, and a foreground space for the right side.
Triangle
There are two ways the triangle could be laid out:
The first part was calculating the properties of the triangle. I set one side length to be 40 pixels and used the law of sines to calculate the other two side lengths.
The slope of side 1 was
The slope of side 2 is the inverse of that. For the line 1 endpoint, the x coordinate is
The y coordinate is
I found the x and y coordinates of the line 2 endpoint with the same two equations but with x and y switched. I then found the slope of line 3 with those two points:
Sides 1 and 2 cross the y axis at y=0. I would like to find where side 3 would cross the y axis if it were to continue past the triangle.
then
I already found the slope and I just need an x and y point on the line. That comes from the coordinates of side 1’s endpoint which is also side 3’s endpoint. The height of the triangle is the larger of the line 1 endpoint y value and the line 2 endpoint y value. Similarly, the width of the triangle is the larger of the line 1 endpoint x value and the line 2 endpoint x value. Those are all the general values of the triangle needed to draw it. We found:
- Side Lengths
- Side Slopes
- Triangle Vertices
- Side Y Intercepts
- Triangle Height
- Triangle Width
Now, I can use those values to make the triangle by calculating how many spaces are needed. Like the circle, I divide the triangle into two parts. In layout 1, the first section is from the first row to the row where side 1 ends. In that section, I draw side 2 and then side 1. In the second section, I draw side 2 then side 3. Layout 2 is similar.
Since I know the slope and y intercept of each side, I can find the x value, which is the spaces, for each y value, which is the current row:
This can be done for each of the three sides. For instance, the number of spaces between side 2 and side 1 in section 1 is the x value of side 1 minus the x value of side 2. In section 2, the number of spaces in the middle is the x value of side 3 minus the x value of side 2.
To run this yourself, download the script and run
using the path to the file. This will load the function into PowerShell and you can run the function as in the screenshots above. You can also open it in a text editor to see the code.