Skip to content

Commit ea07c96

Browse files
author
James Brundage
committed
feat: Turtle.BarGraph() ( Fixes #173 )
1 parent 5d73096 commit ea07c96

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

Types/Turtle/BarGraph.ps1

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<#
2+
.SYNOPSIS
3+
Draws a bar graph using turtle graphics.
4+
.DESCRIPTION
5+
This script uses turtle graphics to draw a bar graph based on the provided data.
6+
.EXAMPLE
7+
turtle barGraph 100 100 5 10 15 20 15 10 5
8+
.EXAMPLE
9+
turtle barGraph 200 200 (
10+
@(1..50;-1..-50) |
11+
Get-Random -Count (Get-Random -Minimum 5 -Maximum 20)
12+
) save ./RandomBarGraph.svg
13+
.EXAMPLE
14+
turtle rotate 90 barGraph 200 200 (
15+
@(1..50;-1..-50) |
16+
Get-Random -Count (Get-Random -Minimum 5 -Maximum 20)
17+
) save ./RandomVerticalBarGraph.svg
18+
.EXAMPLE
19+
turtle rotate 45 barGraph 200 200 (
20+
@(1..50;-1..-50) |
21+
Get-Random -Count (Get-Random -Minimum 5 -Maximum 20)
22+
) save ./RandomDiagonalBarGraph.svg
23+
.EXAMPLE
24+
$sourceData = @(1..50;-1..-50)
25+
$itemCount = (Get-Random -Minimum 5 -Maximum 20)
26+
$points = $sourceData | Get-Random -Count $itemCount
27+
turtle bargraph 200 200 $points morph @(
28+
turtle bargraph 200 200 $points
29+
turtle bargraph 200 200 ( $sourceData | Get-Random -Count $itemCount )
30+
turtle bargraph 200 200 $points
31+
) save ./RandomBarGraphMorph.svg
32+
#>
33+
param(
34+
# The width of the bar graph
35+
[double]$Width,
36+
# The height of the bar graph.
37+
# Please note that in the case of negative values, the effective height is twice this number.
38+
[double]$Height,
39+
40+
# The points in the bar graph.
41+
# Each point will be turned into a relative number and turned into an equal-width bar.
42+
[Parameter(ValueFromRemainingArguments)]
43+
[double[]]$Points
44+
)
45+
46+
47+
# If there were no points, we are drawing nothing, so return ourself.
48+
if (-not $points) { return $this}
49+
50+
# Divide the width by the number of points to get a very snug bar graph
51+
$barWidth = $width / $points.Length
52+
53+
# Find the maximum and minimum values in the points
54+
$min, $max = 0, 0
55+
foreach ($point in $points) {
56+
if ($point -gt $max) { $max = $point}
57+
if ($point -lt $min) { $min = $point}
58+
}
59+
60+
# This gives us the range.
61+
$range = $max - $min
62+
63+
# If the range is zero, we're drawing a flatline.
64+
if ($range -eq 0) {
65+
# so just draw that line and return.
66+
return $this.Forward($width)
67+
}
68+
69+
# Now that we've normalized the range, we can draw the bars.
70+
for ($pointIndex =0 ; $pointIndex -lt $points.Length; $pointIndex++) {
71+
# Each point is essentially telling us the height
72+
$point = $points[$pointIndex]
73+
# which we can turn into a relative value
74+
$relativeHeight = (
75+
# by subtracting the minimum and dividing by the range
76+
(($point - $min) / $range)
77+
) * $height
78+
# If the point was negative, we need to flip the height
79+
if ($point -lt 0) { $relativeHeight *= -1}
80+
# Now we can draw the bar
81+
$this = $this.
82+
# Turn outward and draw the side
83+
Rotate(-90).Forward($relativeHeight).
84+
# Turn and draw the top
85+
Rotate(90).Forward($barWidth)
86+
# Turn and draw the other side
87+
Rotate(90).Forward($relativeHeight).
88+
# Turn back to the original direction
89+
Rotate(-90)
90+
}
91+
return $this
92+
93+
94+

0 commit comments

Comments
 (0)