From 44656a253cb5c410b5d1aed0c84174f910e479ff Mon Sep 17 00:00:00 2001 From: PaddyKe <34421580+PaddyKe@users.noreply.github.com> Date: Sun, 4 Oct 2020 14:46:04 +0200 Subject: [PATCH 1/2] added GrahamScan in PowerShell --- .../code/powershell/GrahamScan.ps1 | 48 +++++++++++++++++++ contents/graham_scan/graham_scan.md | 6 +++ 2 files changed, 54 insertions(+) create mode 100644 contents/graham_scan/code/powershell/GrahamScan.ps1 diff --git a/contents/graham_scan/code/powershell/GrahamScan.ps1 b/contents/graham_scan/code/powershell/GrahamScan.ps1 new file mode 100644 index 000000000..4ea89225c --- /dev/null +++ b/contents/graham_scan/code/powershell/GrahamScan.ps1 @@ -0,0 +1,48 @@ +function New-Point($x, $y) { + return New-Object -TypeName PSObject -Property @{X = [int]$x; Y = [int]$y} +} + +function CounterClockWise($p1, $p2, $p3) { + return (($p3.Y - $p1.Y) * ($p2.X - $p1.X)) -ge (($p2.Y - $p1.Y) * ($p3.X - $p1.X)) +} + +function Graham-Scan([System.Collections.ArrayList] $gift) { + [System.Collections.ArrayList]$gift = $gift | Get-Unique -AsString + $start = $gift | Sort-Object -Property Y | Select-Object -First 1 + $gift.Remove($start) + + $s = $gift | Sort-Object -Property {[Math]::Atan2($_.Y - $start.Y, $_.X - $start.X)} + [System.Collections.ArrayList]$hull = @($start, $s[0], $s[1]) + + $s | Select-Object -Skip 2 | ForEach-Object { + while (-not (CounterClockWise $hull[-2] $hull[-1] $_)) { + $hull.RemoveAt($hull.Count - 1) + } + $hull.Add($_) | Out-Null + } + + return $hull +} + + +$testGift = @( + (New-Point -5 2) + (New-Point 5 7) + (New-Point -6 -12) + (New-Point -14 -14) + (New-Point 9 9) + (New-Point -1 -1) + (New-Point -10 11) + (New-Point -6 15) + (New-Point -6 -8) + (New-Point 15 -9) + (New-Point 7 -7) + (New-Point -2 -9) + (New-Point 6 -5) + (New-Point 0 14) + (New-Point 2 8) +) + +$hull = Graham-Scan $testGift + +Write-Host "The points in the hull are: $($hull | % {"($($_.X), $($_.Y)) "})" \ No newline at end of file diff --git a/contents/graham_scan/graham_scan.md b/contents/graham_scan/graham_scan.md index 586d209db..e0c84c723 100644 --- a/contents/graham_scan/graham_scan.md +++ b/contents/graham_scan/graham_scan.md @@ -32,6 +32,8 @@ We can find whether a rotation is counter-clockwise with trigonometric functions [import:18-20, lang="cpp"](code/c++/graham_scan.cpp) {% sample lang="coco" %} [import:4-8, lang="coconut"](code/coconut/graham_scan.coco) +{% sample lang="ps1" %} +[import:5-7, lang="coconut"](code/powershell/GrahamScan.ps1) {% endmethod %} If the output of this function is 0, the points are collinear. @@ -66,6 +68,8 @@ In the end, the code should look something like this: [import:26-62, lang="cpp"](code/c++/graham_scan.cpp) {% sample lang="coco" %} [import:17-30, lang="coconut"](code/coconut/graham_scan.coco) +{% sample lang="ps1" %} +[import:9-25, lang="coconut"](code/powershell/GrahamScan.ps1) {% endmethod %} ### Bibliography @@ -95,6 +99,8 @@ In the end, the code should look something like this: [import, lang="cpp"](code/c++/graham_scan.cpp) {%sample lang="coco" %} [import, lang="coconut"](code/coconut/graham_scan.coco) +{% sample lang="ps1" %} +[import, lang="coconut"](code/powershell/GrahamScan.ps1) {% endmethod %}