diff --git a/contents/jarvis_march/code/golang/jarvis.go b/contents/jarvis_march/code/golang/jarvis.go new file mode 100644 index 000000000..03cce3834 --- /dev/null +++ b/contents/jarvis_march/code/golang/jarvis.go @@ -0,0 +1,68 @@ +package main + +import ( + "fmt" +) + +type point struct { + x, y float64 +} + +func leftMostPoint(points []point) point { + ret := points[0] + + for _, p := range points { + if (p.x < ret.x) || (p.x == ret.x && p.y < ret.y) { + ret = p + } + } + + return ret +} + +func (p point) equal(o point) bool { + return p.x == o.x && p.y == o.x +} + +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 jarvisMarch(points []point) []point { + hullPoints := make([]point, 0) + hullPoint := leftMostPoint(points) + hullPoints = append(hullPoints, hullPoint) + + for { + endPoint := points[0] + + for _, p := range points[1:] { + if endPoint.equal(hullPoint) || !counterClockWise(p, hullPoints[len(hullPoints)-1], endPoint) { + endPoint = p + } + } + + hullPoint = endPoint + + if endPoint.equal(hullPoints[0]) { + break + } + + hullPoints = append(hullPoints, hullPoint) + } + return hullPoints +} + +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}, + } + + hullPoints := jarvisMarch(points) + fmt.Println("The hull points are:") + + for _, p := range hullPoints { + fmt.Printf("x=%f y=%f\n", p.x, p.y) + } +} diff --git a/contents/jarvis_march/jarvis_march.md b/contents/jarvis_march/jarvis_march.md index 4fc02a9dc..f7473d2d6 100644 --- a/contents/jarvis_march/jarvis_march.md +++ b/contents/jarvis_march/jarvis_march.md @@ -40,6 +40,8 @@ Since this algorithm, there have been many other algorithms that have advanced t [import, lang:"python"](code/python/jarvisMarch.py) {% sample lang="cpp" %} [import, lang:"c_cpp"](code/c++/jarvis_march.cpp) +{% sample lang="go" %} +[import, lang:"go"](code/golang/jarvis.go) {% endmethod %}