Skip to content

add the graham scan in go #402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions contents/graham_scan/code/golang/graham.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"fmt"
"math"
"sort"
)

type point struct {
x, y int
}

func counterClockwise(p1, p2, p3 point) bool {
return (p3.y-p1.y)*(p2.x-p1.x) >= (p2.y-p1.y)*(p3.x-p1.x)
}

func polarAngle(ref, point point) float64 {
return math.Atan2(float64(point.y-ref.y), float64(point.x-ref.x))
}

func grahamScan(points []point) []point {
sort.Slice(points, func(a, b int) bool {
return points[a].y < points[b].y || (points[a].y == points[b].y && points[a].x < points[b].x)
})

start := points[0]
points = points[1:]

sort.Slice(points, func(a, b int) bool {
return polarAngle(start, points[a]) < polarAngle(start, points[b])
})

hull := []point{start, points[0], points[1]}
for _, p := range points[2:] {
for !counterClockwise(hull[len(hull)-2], hull[len(hull)-1], p) {
hull = hull[:len(hull)-1]
}
hull = append(hull, p)
}

return hull
}

func main() {
points := []point{{-5, 2}, {5, 7}, {-6, -12}, {-14, -14}, {9, 9},
{-1, -1}, {-10, 11}, {-6, 15}, {-6, -8}, {15, -9},
{7, -7}, {-2, -9}, {6, -5}, {0, 14}, {2, 8}}

fmt.Println("The points in the hull are:")
hull := grahamScan(points)
for _, p := range hull {
fmt.Println(p)
}
}
6 changes: 6 additions & 0 deletions contents/graham_scan/graham_scan.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ We can find whether a rotation is counter-clockwise with trigonometric functions
[import:36-38, lang:"javascript"](code/javascript/graham-scan.js)
{% sample lang="py" %}
[import:4-6, lang:"python"](code/python/grahamScan.py)
{% sample lang="go" %}
[import:13-15, lang:"go"](code/golang/graham.go)
{% sample lang="java" %}
[import:27-29, lang:"java"](code/java/GrahamScan.java)
{% endmethod %}
Expand All @@ -48,6 +50,8 @@ In the end, the code should look something like this:
[import:1-30, lang:"javascript"](code/javascript/graham-scan.js)
{% sample lang="py" %}
[import:14-27, lang:"python"](code/python/grahamScan.py)
{% sample lang="go" %}
[import:21-42, lang:"go"](code/golang/graham.go)
{% sample lang="java" %}
[import:35-70, lang:"java"](code/java/GrahamScan.java)
{% endmethod %}
Expand All @@ -69,6 +73,8 @@ In the end, the code should look something like this:
[import, lang:"javascript"](code/javascript/graham-scan.js)
{% sample lang="py" %}
[import, lang:"python"](code/python/grahamScan.py)
{% sample lang="go" %}
[import, lang:"go"](code/golang/graham.go)
{% sample lang="java" %}
[import, lang:"java"](code/java/GrahamScan.java)
{% endmethod %}
Expand Down