polygon

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2024 License: MIT Imports: 2 Imported by: 0

README

polygon

Go package to construct simple polygonal shapes.

Example

p, _ := polygon.NewPolygon(
    polygon.Point{0, 0},
    polygon.Point{0, 3},
    polygon.Point{4, 0},
)
fmt.Println(p.Area()) // 6.0
_, err := polygon.NewPolygon(
    polygon.Point{0, 0},
    polygon.Point{3, 3},
)
fmt.Println(err) // ErrInsufficientPoints

Installation

go get github.com/hamonangann/polygon

Contribution

Fork this repository and open a pull request to improve this project. Your contribution matters a lot!

If you have any inquiries, please raise an issue.

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

Constants

This section is empty.

Variables

View Source
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

func BetweenSegment(a Point, p Point, b Point) bool

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

func EuclideanDistance(a Point, b Point) float64

EuclideanDistance returns the euclidean distance between two points.

func LineSegmentIntersect

func LineSegmentIntersect(p1 Point, p2 Point, q1 Point, q2 Point) bool

LineSegmentIntersect returns true if a line segment from p1 to p2 intersects with a line segment from q1 to q2.

func ManhattanDistance

func ManhattanDistance(a Point, b Point) float64

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

func OrientationTriplet(a Point, b Point, c Point) int

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 Point

type Point struct {
	X float64
	Y float64
}

Point is a type that stores a location of a point in a Cartesian coordinate system.

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

func NewPolygon(pts ...Point) (*Polygon, error)

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 NewRectangle(length float64, height float64) Polygon

func NewRhombus

func NewRhombus(diagonalA float64, diagonalB float64) Polygon

func NewSquare

func NewSquare(side float64) Polygon

func NewTrapezoid

func NewTrapezoid(sideA float64, sideB float64, height float64) Polygon

func NewTriangle

func NewTriangle(base float64, height float64) Polygon

func (Polygon) Area

func (p Polygon) Area() float64

Area returns the area inside a Polygon's sides.

func (Polygon) NumberOfSides

func (p Polygon) NumberOfSides() int

NumberOfSides returs the number of sides of a Polygon.

func (Polygon) NumberOfVertices

func (p Polygon) NumberOfVertices() int

NumberOfSides returs the number of vertices of a Polygon.

func (Polygon) Perimeter

func (p Polygon) Perimeter() float64

Perimeter returns the sum of length of each sides of a Polygon

func (Polygon) Points

func (p Polygon) Points() []Point

Points returns a sequence of Point(s) forming the polygon.

func (Polygon) Sides

func (p Polygon) Sides() []float64

Sides returns the length of each sides of a Polygon

func (Polygon) SumOfInteriorAngles

func (p Polygon) SumOfInteriorAngles() float64

NumberOfSides returs the sum of interior angles of a Polygon in Radian.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL