Documentation
¶
Overview ¶
Package polygon is a library for constructing [simple polygons]. It provides methods to measure polygon properties, such as perimeter, area, and sum of internal angles. A simple polygon is a sequence of points that are connecting to each other without self-intersection.
Euclidean geometry is assumed throughout this library.
Example: (constructing right triangle with 3 points)
p, _ := polygon.NewPolygon(
polygon.Point{0, 0},
polygon.Point{0, 3},
polygon.Point{4, 0},
)
fmt.Println(p.Area()) // 6.0
Index ¶
- Variables
- func BetweenSegment(a Point, p Point, b Point) bool
- func EuclideanDistance(a Point, b Point) float64
- func LineSegmentIntersect(p1 Point, p2 Point, q1 Point, q2 Point) bool
- func ManhattanDistance(a Point, b Point) float64
- func OrientationTriplet(a Point, b Point, c Point) int
- type Point
- type Polygon
- func NewPolygon(pts ...Point) (*Polygon, error)
- func NewRectangle(length float64, height float64) Polygon
- func NewRhombus(diagonalA float64, diagonalB float64) Polygon
- func NewSquare(side float64) Polygon
- func NewTrapezoid(sideA float64, sideB float64, height float64) Polygon
- func NewTriangle(base float64, height float64) Polygon
Constants ¶
This section is empty.
Variables ¶
var ( ErrInsufficientPoints = errors.New("points must be more than 2") // ErrInsufficientPoints is for lack of Polygon points. ErrCollinearPoints = errors.New("points must not collinear") // ErrCollinearPoints is for Polygon with collinear points ErrSelfIntersecting = errors.New("sides must not intersect each other") // ErrSelfIntersecting is for self-intersecting Polygon (not supported) )
Functions ¶
func BetweenSegment ¶
BetweenSegment returns true if Point p is located in a line segment from Point a to Point b. If a->p->b is not collinear, it always returns false.
func EuclideanDistance ¶
EuclideanDistance returns the euclidean distance between two points.
func LineSegmentIntersect ¶
LineSegmentIntersect returns true if a line segment from p1 to p2 intersects with a line segment from q1 to q2.
func ManhattanDistance ¶
ManhattanDistance returns the manhattan distance between two points. To measure the length of a line segment starts in a and ends in b, use EuclideanDistance instead.
func OrientationTriplet ¶
OrientationTriplet returns an integer denoting the orientation made of triplet a->b->c.
There are three possible outputs. If a->b->c is forming a clockwise triangle, it returns 1. If a->b->c is forming a counter-lockwise triangle, it returns -1. If a->b->c is collinear (forming a line), it returns 0.
Types ¶
type Polygon ¶
type Polygon struct {
// contains filtered or unexported fields
}
Polygon is a type that stores a sequence of connected Point(s) forming valid simple polygon. It is immutable and all the points must be defined in the creation.
ALWAYS construct Polygon(s) from NewPolygon() constructor or any utility functions in examples.go. Directly constructing Polygon{} may resulted in unexpected behavior.
func NewPolygon ¶
NewPolygon returns a pointer to Polygon (which is nil in case of error) and an error Error may happen if points provided to this constructor are forming an invalid simple polygon.
There are three checks in this constructor. First, it checks for insufficient points. If less than 3 points are provided, they are not enough to form a polygon. Second, it checks for any collinearity by taking three neighboring points. It is considered invalid in this library. Third, if the polygon has more than 3 points (quadrilateral or more), it checks for self intersection. This library does not support self-intersecting polygons, since it is not a simple polygon anymore.
func NewRectangle ¶
func NewRhombus ¶
func NewTriangle ¶
func (Polygon) NumberOfSides ¶
NumberOfSides returs the number of sides of a Polygon.
func (Polygon) NumberOfVertices ¶
NumberOfSides returs the number of vertices of a Polygon.
func (Polygon) SumOfInteriorAngles ¶
NumberOfSides returs the sum of interior angles of a Polygon in Radian.