Circles

One of my all time favourite films is “The Thomas Crown Affair” – the original NOT the remake. The films theme tune – Windmillls of your Mind – with its focus on circles got me thinking about geometrical calculations in general and circles in particular. I decided I’d create a library of such calculations – eventually it’ll be available on github – starting with circles.

Two calculations come to mind immediately for circles – area of a circle and its circumference:

New-Variable -Name pi -Value ([math]::PI) -Option Constant

#region CIRCLES
function Get-CircleArea {
   [CmdletBinding()]
   param (
     [double]$radius
   )

  $area = $pi * $radius * $radius

  [math]::Round($area, 3)

}

function Get-CircleCircumference  {
   [CmdletBinding()]
   param (
     [double]$radius
   )

  $ccmfrnce = 2 * $pi * $radius

  [math]::Round($ccmfrnce, 3)
}

#endregion CIRCLES

I started by creating a constant for the value of PI. As its a constant you can’t change or remove it. Defining it once saves time and effort.

Both functions take a single parameter – the radius of the circle. The area or circumference is calculated.

Area = pi * radius squared

Circumference = 2 * pi * radius

The output is  rounded to three decimal places.

You’ll notice that the function only contains the calculation. I’ve not added any pipeline ability, help, comments, error handling or any other “production level code”. This is deliberate as I want to concentrate on the calculations and most of these functions will be fairly simple. If you want the bells and whistles think of it as an opportunity <GRIN>

Next up will be spheres.

This entry was posted in Powershell. Bookmark the permalink.

Leave a comment