geom

package
v1.3.4 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2021 License: BSD-3-Clause Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AbsInt

func AbsInt(x int) int

func DistanceOf

func DistanceOf(a, b Coord) float64

两点之间的距离

func IsOnSegment

func IsOnSegment(q, p1, p2 Coord) bool

点q是否在线段(p1,p2)上

func IsSegmentIntersect

func IsSegmentIntersect(p1, q1, p2, q2 Coord) bool

线段(p1,q1)与线段(p2,q2)是否相交

  1. General Case: – (p1, q1, p2) 和 (p1, q1, q2) have different orientations and – (p2, q2, p1) and (p2, q2, q1) have different orientations.
  2. Special Case – (p1, q1, p2), (p1, q1, q2), (p2, q2, p1), and (p2, q2, q1) are all collinear and – the x-projections of (p1, q1) and (p2, q2) intersect – the y-projections of (p1, q1) and (p2, q2) intersect

func MaxInt

func MaxInt(x, y int) int

func MinInt

func MinInt(x, y int) int

func OrientationOfCoord

func OrientationOfCoord(p1, p2, p3 Coord) int

3个坐标的朝向 0 --> p, q, r 共线 1 --> 顺时针 2 --> 逆时针 Slope of line segment (p1, p2): σ = (y2 - y1)/(x2 - x1) Slope of line segment (p2, p3): τ = (y3 - y2)/(x3 - x2) If σ > τ, the orientation is clockwise (right turn)

Types

type Circle

type Circle struct {
	Center Coord // 中心点
	Radius int   // 半径
}

圆形

func NewCircle

func NewCircle(center Coord, radius int) Circle

func (*Circle) CrossPoint

func (c *Circle) CrossPoint(point Coord) Coord

点到圆心的线段和圆的交点

func (*Circle) SurroundRect

func (c *Circle) SurroundRect() Rectangle

获取包围矩形

type ConvexPolygon

type ConvexPolygon struct {
	Vertexes []Coord
}

凸多边形

func (*ConvexPolygon) Contains

func (c *ConvexPolygon) Contains(x, y int) bool

type Coord

type Coord struct {
	X int
	Y int
}

坐标

func GetMiddlePointOf

func GetMiddlePointOf(a, b Coord) Coord

获取线段(a,b)的中间点

func LineSection

func LineSection(a, b Coord, m, n int) Coord

找出一个点p把线段(a,b)按比例m:n切分成2段

func NewCoord

func NewCoord(x, y int) Coord

func SegmentIntersectPoint

func SegmentIntersectPoint(p1, q1, p2, q2 Coord) Coord

线段(p1,q1)与线段(p2,q2)的交点,假设两条线段已经相交 当已经确定两个线段AB和CD是相交的时候,我们可以把AB和CD看成一个四边形的两条对角线,它们相交与点O, 我们可以通过三角形面积公式求出ABC和ABD的面积,它们的比值就是OC和OD的比值,然后再用定比分点公式求出O的坐标。 see https://leetcode-cn.com/problems/intersection-lcci/solution/

type Rectangle

type Rectangle struct {
	Coord             // 左下角原点
	Width, Height int // 宽度、高度
}

矩形

func NewRectangle

func NewRectangle(x, y, w, h int) Rectangle

func RectIntersect

func RectIntersect(a *Rectangle, b *Rectangle) Rectangle

矩形相交区域

func RectUnion

func RectUnion(a *Rectangle, b *Rectangle) Rectangle

矩形结合区域

func (*Rectangle) Contains

func (r *Rectangle) Contains(x, y int) bool

点是否在矩形内

func (*Rectangle) ContainsPoint

func (r *Rectangle) ContainsPoint(pt Coord) bool

点是否在矩形内

func (*Rectangle) ContainsRegion

func (r *Rectangle) ContainsRegion(rec *Rectangle) bool

是否包含

func (*Rectangle) GetVertexes

func (r *Rectangle) GetVertexes() [4]Coord

四个顶点

func (*Rectangle) Inflate

func (r *Rectangle) Inflate(width, height int)

展开或者收缩矩形

func (*Rectangle) IsIntersectsWith

func (r *Rectangle) IsIntersectsWith(rec *Rectangle) bool

是否相交

type Triangle

type Triangle struct {
	Vertexes [3]Coord
}

三角形

func NewTriangle

func NewTriangle(a, b, c Coord) Triangle

func (*Triangle) Area

func (t *Triangle) Area() int

计算面积

func (*Triangle) Contains

func (t *Triangle) Contains(p Coord) bool

如果p点在三角形(ABC)内,则PAB, PAC, PBC的面积应该与ABC相等

func (*Triangle) IsValid

func (t *Triangle) IsValid() bool

判断三角形是否合法,两边之和大于第三边

type Vector

type Vector struct {
	X, Y int
}

二维向量

func NewVectorFrom

func NewVectorFrom(a, b Coord) Vector

func (*Vector) Add

func (a *Vector) Add(b *Vector) Vector

加法

func (*Vector) Cross

func (a *Vector) Cross(b *Vector) int

叉积(cross product)

func (*Vector) Dot

func (a *Vector) Dot(b *Vector) int

点积(dot product)

func (*Vector) GetAngle

func (a *Vector) GetAngle(b *Vector) float64

获取2个相交向量的角度, cosθ = a x b / |a| |b|

func (*Vector) Length

func (a *Vector) Length() float64

向量长度

func (*Vector) Mul

func (a *Vector) Mul(m float64) Vector

乘法

func (*Vector) Normalize

func (a *Vector) Normalize() Vector

单位向量

func (*Vector) Rotate

func (a *Vector) Rotate(angle float64) Vector

向量按照角度逆时针旋转获得新的向量,如果需要顺时针旋转,angle传入负值 对于任意两个不同点A和B,A绕B旋转θ角度后的坐标为: (Δx*cosθ- Δy * sinθ+ xB, Δy*cosθ + Δx * sinθ+ yB )

func (*Vector) SquaredLength

func (a *Vector) SquaredLength() int

func (*Vector) Sub

func (a *Vector) Sub(b *Vector) Vector

减法

func (Vector) ToCoord

func (a Vector) ToCoord(start Coord) Coord

转换成坐标

func (*Vector) Trunc

func (a *Vector) Trunc(ratio float64) Vector

按比例截断

Jump to

Keyboard shortcuts

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