How to resolve the algorithm Convex hull step by step in the Go programming language
How to resolve the algorithm Convex hull step by step in the Go programming language
Table of Contents
Problem Statement
Find the points which form a convex hull from a set of arbitrary two dimensional points. For example, given the points (16,3), (12,17), (0,6), (-4,-6), (16,6), (16,-7), (16,-3), (17,-4), (5,19), (19,-8), (3,16), (12,13), (3,-4), (17,5), (-3,15), (-3,-9), (0,11), (-9,-3), (-4,-2) and (12,10) the convex hull would be (-9,-3), (-3,-9), (19,-8), (17,5), (12,17), (5,19) and (-3,15).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Convex hull step by step in the Go programming language
The provided Go code defines a ConvexHull
function for finding the convex hull of a set of points in two-dimensional space. A convex hull is the smallest convex polygon that encloses all the points in a set. It is often used to approximate the shape of a point set.
Here's a detailed breakdown of the code:
-
Point and Points Types:
image.Point
from theimage
package is used to represent a point in two-dimensional space.points
is a custom type defined as a slice ofimage.Point
. It provides methods for sorting and comparing points.
-
ConvexHull
Function:- The
ConvexHull
method takes apoints
slice as input and returns a newpoints
slice representing the convex hull of the input points. - It essentially uses the "Monotone Chain" algorithm to construct the convex hull as follows:
- The points are sorted in ascending order of x-coordinates, and then in ascending order of y-coordinates for points with the same x-coordinate.
- The algorithm creates two "hulls": one for the lower boundary and one for the upper boundary of the convex hull.
- It iterates through the sorted points and checks if the current point creates a clockwise turn (i.e., a concave corner) with the last two points in the lower hull. If it does, it removes those points from the hull.
- It then adds the current point to the lower hull.
- After processing all points, the algorithm reverses the direction and starts from the last point in the lower hull. It performs the same checks and updates the upper hull.
- Finally, it joins the lower and upper hulls to form the convex hull.
- The
-
ccw
Function:- The
ccw
function takes threeimage.Point
as input and returns a boolean indicating whether the points make a counter-clockwise (CCW) turn. - It calculates the cross product of the two vectors formed by the three points to determine the direction of the turn.
- The
-
Main Function:
- The
main
function initializes apoints
slice with some example points. - It calls the
ConvexHull
method on thepoints
slice to compute the convex hull. - Finally, it prints the resulting convex hull points.
- The
In summary, this code demonstrates an efficient algorithm for constructing the convex hull of a set of points, which can be useful in various applications such as computer graphics, computational geometry, and data visualization.
Source code in the go programming language
package main
import (
"fmt"
"image"
"sort"
)
// ConvexHull returns the set of points that define the
// convex hull of p in CCW order starting from the left most.
func (p points) ConvexHull() points {
// From https://en.wikibooks.org/wiki/Algorithm_Implementation/Geometry/Convex_hull/Monotone_chain
// with only minor deviations.
sort.Sort(p)
var h points
// Lower hull
for _, pt := range p {
for len(h) >= 2 && !ccw(h[len(h)-2], h[len(h)-1], pt) {
h = h[:len(h)-1]
}
h = append(h, pt)
}
// Upper hull
for i, t := len(p)-2, len(h)+1; i >= 0; i-- {
pt := p[i]
for len(h) >= t && !ccw(h[len(h)-2], h[len(h)-1], pt) {
h = h[:len(h)-1]
}
h = append(h, pt)
}
return h[:len(h)-1]
}
// ccw returns true if the three points make a counter-clockwise turn
func ccw(a, b, c image.Point) bool {
return ((b.X - a.X) * (c.Y - a.Y)) > ((b.Y - a.Y) * (c.X - a.X))
}
type points []image.Point
func (p points) Len() int { return len(p) }
func (p points) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p points) Less(i, j int) bool {
if p[i].X == p[j].X {
return p[i].Y < p[i].Y
}
return p[i].X < p[j].X
}
func main() {
pts := points{
{16, 3}, {12, 17}, {0, 6}, {-4, -6}, {16, 6},
{16, -7}, {16, -3}, {17, -4}, {5, 19}, {19, -8},
{3, 16}, {12, 13}, {3, -4}, {17, 5}, {-3, 15},
{-3, -9}, {0, 11}, {-9, -3}, {-4, -2}, {12, 10},
}
hull := pts.ConvexHull()
fmt.Println("Convex Hull:", hull)
}
You may also check:How to resolve the algorithm Linear congruential generator step by step in the Logo programming language
You may also check:How to resolve the algorithm Iterated digits squaring step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Integer sequence step by step in the Pike programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the Batch File programming language
You may also check:How to resolve the algorithm Sorting algorithms/Sleep sort step by step in the SNUSP programming language