mirror of
https://github.com/unidoc/unipdf.git
synced 2025-05-02 22:17:06 +08:00
129 lines
2.0 KiB
Go
129 lines
2.0 KiB
Go
![]() |
package draw
|
||
|
|
||
|
import "fmt"
|
||
|
|
||
|
type Point struct {
|
||
|
X float64
|
||
|
Y float64
|
||
|
}
|
||
|
|
||
|
func (p Point) Add(dx, dy float64) Point {
|
||
|
p.X += dx
|
||
|
p.Y += dy
|
||
|
return p
|
||
|
}
|
||
|
|
||
|
// Add vector to a point.
|
||
|
func (this Point) AddVector(v Vector) Point {
|
||
|
this.X += v.Dx
|
||
|
this.Y += v.Dy
|
||
|
return this
|
||
|
}
|
||
|
|
||
|
func (p Point) String() string {
|
||
|
return fmt.Sprintf("(%.1f,%.1f)", p.X, p.Y)
|
||
|
}
|
||
|
|
||
|
type Path struct {
|
||
|
Points []Point
|
||
|
}
|
||
|
|
||
|
func NewPath() Path {
|
||
|
path := Path{}
|
||
|
path.Points = []Point{}
|
||
|
return path
|
||
|
}
|
||
|
|
||
|
func NewPoint(x, y float64) Point {
|
||
|
point := Point{}
|
||
|
point.X = x
|
||
|
point.Y = y
|
||
|
return point
|
||
|
}
|
||
|
|
||
|
func (this Path) AppendPoint(point Point) Path {
|
||
|
this.Points = append(this.Points, point)
|
||
|
return this
|
||
|
}
|
||
|
|
||
|
func (this Path) RemovePoint(number int) Path {
|
||
|
if number < 1 || number > len(this.Points) {
|
||
|
return this
|
||
|
}
|
||
|
|
||
|
idx := number - 1
|
||
|
this.Points = append(this.Points[:idx], this.Points[idx+1:]...)
|
||
|
return this
|
||
|
}
|
||
|
|
||
|
func (this Path) Length() int {
|
||
|
return len(this.Points)
|
||
|
}
|
||
|
|
||
|
func (this Path) GetPointNumber(number int) Point {
|
||
|
if number < 1 || number > len(this.Points) {
|
||
|
return Point{}
|
||
|
}
|
||
|
return this.Points[number-1]
|
||
|
}
|
||
|
|
||
|
func (path Path) Copy() Path {
|
||
|
pathcopy := Path{}
|
||
|
pathcopy.Points = []Point{}
|
||
|
for _, p := range path.Points {
|
||
|
pathcopy.Points = append(pathcopy.Points, p)
|
||
|
}
|
||
|
return pathcopy
|
||
|
}
|
||
|
|
||
|
func (path Path) Offset(offX, offY float64) Path {
|
||
|
for i, p := range path.Points {
|
||
|
path.Points[i] = p.Add(offX, offY)
|
||
|
}
|
||
|
return path
|
||
|
}
|
||
|
|
||
|
func (path Path) GetBoundingBox() Rectangle {
|
||
|
bbox := Rectangle{}
|
||
|
|
||
|
minX := 0.0
|
||
|
maxX := 0.0
|
||
|
minY := 0.0
|
||
|
maxY := 0.0
|
||
|
for idx, p := range path.Points {
|
||
|
if idx == 0 {
|
||
|
minX = p.X
|
||
|
maxX = p.X
|
||
|
minY = p.Y
|
||
|
maxY = p.Y
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
if p.X < minX {
|
||
|
minX = p.X
|
||
|
}
|
||
|
if p.X > maxX {
|
||
|
maxX = p.X
|
||
|
}
|
||
|
if p.Y < minY {
|
||
|
minY = p.Y
|
||
|
}
|
||
|
if p.Y > maxY {
|
||
|
maxY = p.Y
|
||
|
}
|
||
|
}
|
||
|
|
||
|
bbox.X = minX
|
||
|
bbox.Y = minY
|
||
|
bbox.Width = maxX - minX
|
||
|
bbox.Height = maxY - minY
|
||
|
return bbox
|
||
|
}
|
||
|
|
||
|
type Rectangle struct {
|
||
|
X float64
|
||
|
Y float64
|
||
|
Width float64
|
||
|
Height float64
|
||
|
}
|