@@ -212,6 +212,106 @@ $Distance = 10
212212
213213$this.Forward($Distance * -1)
214214
215+ </Script >
216+ </ScriptMethod >
217+ <ScriptMethod >
218+ <Name >BarGraph</Name >
219+ <Script >
220+ < #
221+ .SYNOPSIS
222+ Draws a bar graph using turtle graphics.
223+ .DESCRIPTION
224+ This script uses turtle graphics to draw a bar graph based on the provided data.
225+ .EXAMPLE
226+ turtle barGraph 100 100 5 10 15 20 15 10 5
227+ .EXAMPLE
228+ turtle barGraph 200 200 (
229+ @(1..50;-1..-50) |
230+ Get-Random -Count (Get-Random -Minimum 5 -Maximum 20)
231+ ) save ./RandomBarGraph.svg
232+ .EXAMPLE
233+ turtle rotate 90 barGraph 200 200 (
234+ @(1..50;-1..-50) |
235+ Get-Random -Count (Get-Random -Minimum 5 -Maximum 20)
236+ ) save ./RandomVerticalBarGraph.svg
237+ .EXAMPLE
238+ turtle rotate 45 barGraph 200 200 (
239+ @(1..50;-1..-50) |
240+ Get-Random -Count (Get-Random -Minimum 5 -Maximum 20)
241+ ) save ./RandomDiagonalBarGraph.svg
242+ .EXAMPLE
243+ $sourceData = @(1..50;-1..-50)
244+ $itemCount = (Get-Random -Minimum 5 -Maximum 20)
245+ $points = $sourceData | Get-Random -Count $itemCount
246+ turtle bargraph 200 200 $points morph @(
247+ turtle bargraph 200 200 $points
248+ turtle bargraph 200 200 ( $sourceData | Get-Random -Count $itemCount )
249+ turtle bargraph 200 200 $points
250+ ) save ./RandomBarGraphMorph.svg
251+ #>
252+ param(
253+ # The width of the bar graph
254+ [double]$Width,
255+ # The height of the bar graph.
256+ # Please note that in the case of negative values, the effective height is twice this number.
257+ [double]$Height,
258+
259+ # The points in the bar graph.
260+ # Each point will be turned into a relative number and turned into an equal-width bar.
261+ [Parameter(ValueFromRemainingArguments)]
262+ [double[]]$Points
263+ )
264+
265+
266+ # If there were no points, we are drawing nothing, so return ourself.
267+ if (-not $points) { return $this}
268+
269+ # Divide the width by the number of points to get a very snug bar graph
270+ $barWidth = $width / $points.Length
271+
272+ # Find the maximum and minimum values in the points
273+ $min, $max = 0, 0
274+ foreach ($point in $points) {
275+ if ($point -gt $max) { $max = $point}
276+ if ($point -lt $min) { $min = $point}
277+ }
278+
279+ # This gives us the range.
280+ $range = $max - $min
281+
282+ # If the range is zero, we're drawing a flatline.
283+ if ($range -eq 0) {
284+ # so just draw that line and return.
285+ return $this.Forward($width)
286+ }
287+
288+ # Now that we've normalized the range, we can draw the bars.
289+ for ($pointIndex =0 ; $pointIndex -lt $points.Length; $pointIndex++) {
290+ # Each point is essentially telling us the height
291+ $point = $points[$pointIndex]
292+ # which we can turn into a relative value
293+ $relativeHeight = (
294+ # by subtracting the minimum and dividing by the range
295+ (($point - $min) / $range)
296+ ) * $height
297+ # If the point was negative, we need to flip the height
298+ if ($point -lt 0) { $relativeHeight *= -1}
299+ # Now we can draw the bar
300+ $this = $this.
301+ # Turn outward and draw the side
302+ Rotate(-90).Forward($relativeHeight).
303+ # Turn and draw the top
304+ Rotate(90).Forward($barWidth)
305+ # Turn and draw the other side
306+ Rotate(90).Forward($relativeHeight).
307+ # Turn back to the original direction
308+ Rotate(-90)
309+ }
310+ return $this
311+
312+
313+
314+
215315 </Script >
216316 </ScriptMethod >
217317 <ScriptMethod >
0 commit comments