mirror of
https://github.com/unidoc/unipdf.git
synced 2025-04-29 13:48:54 +08:00
parent
6e7b575f0f
commit
d0db62dada
@ -21,6 +21,7 @@ type CubicBezierCurve struct {
|
||||
P3 Point // Final point.
|
||||
}
|
||||
|
||||
// NewCubicBezierCurve returns a new cubic Bezier curve.
|
||||
func NewCubicBezierCurve(x0, y0, x1, y1, x2, y2, x3, y3 float64) CubicBezierCurve {
|
||||
curve := CubicBezierCurve{}
|
||||
curve.P0 = NewPoint(x0, y0)
|
||||
@ -45,6 +46,7 @@ func (curve CubicBezierCurve) AddOffsetXY(offX, offY float64) CubicBezierCurve {
|
||||
return curve
|
||||
}
|
||||
|
||||
// GetBounds returns the bounding box of the Bezier curve.
|
||||
func (curve CubicBezierCurve) GetBounds() model.PdfRectangle {
|
||||
minX := curve.P0.X
|
||||
maxX := curve.P0.X
|
||||
@ -84,21 +86,25 @@ func (curve CubicBezierCurve) GetBounds() model.PdfRectangle {
|
||||
return bounds
|
||||
}
|
||||
|
||||
// CubicBezierPath represents a collection of cubic Bezier curves.
|
||||
type CubicBezierPath struct {
|
||||
Curves []CubicBezierCurve
|
||||
}
|
||||
|
||||
// NewCubicBezierPath returns a new empty cubic Bezier path.
|
||||
func NewCubicBezierPath() CubicBezierPath {
|
||||
bpath := CubicBezierPath{}
|
||||
bpath.Curves = []CubicBezierCurve{}
|
||||
return bpath
|
||||
}
|
||||
|
||||
// AppendCurve appends the specified Bezier curve to the path.
|
||||
func (p CubicBezierPath) AppendCurve(curve CubicBezierCurve) CubicBezierPath {
|
||||
p.Curves = append(p.Curves, curve)
|
||||
return p
|
||||
}
|
||||
|
||||
// Copy returns a clone of the Bezier path.
|
||||
func (p CubicBezierPath) Copy() CubicBezierPath {
|
||||
bpathcopy := CubicBezierPath{}
|
||||
bpathcopy.Curves = []CubicBezierCurve{}
|
||||
@ -108,6 +114,7 @@ func (p CubicBezierPath) Copy() CubicBezierPath {
|
||||
return bpathcopy
|
||||
}
|
||||
|
||||
// Offset shifts the Bezier path with the specified offsets.
|
||||
func (p CubicBezierPath) Offset(offX, offY float64) CubicBezierPath {
|
||||
for i, c := range p.Curves {
|
||||
p.Curves[i] = c.AddOffsetXY(offX, offY)
|
||||
@ -115,6 +122,7 @@ func (p CubicBezierPath) Offset(offX, offY float64) CubicBezierPath {
|
||||
return p
|
||||
}
|
||||
|
||||
// GetBoundingBox returns the bounding box of the Bezier path.
|
||||
func (p CubicBezierPath) GetBoundingBox() Rectangle {
|
||||
bbox := Rectangle{}
|
||||
|
||||
|
@ -10,15 +10,19 @@ type Path struct {
|
||||
Points []Point
|
||||
}
|
||||
|
||||
// NewPath returns a new empty path.
|
||||
func NewPath() Path {
|
||||
return Path{}
|
||||
}
|
||||
|
||||
// AppendPoint adds the specified point to the path.
|
||||
func (p Path) AppendPoint(point Point) Path {
|
||||
p.Points = append(p.Points, point)
|
||||
return p
|
||||
}
|
||||
|
||||
// RemovePoint removes the point at the index specified by number from the
|
||||
// path. The index is 1-based.
|
||||
func (p Path) RemovePoint(number int) Path {
|
||||
if number < 1 || number > len(p.Points) {
|
||||
return p
|
||||
@ -29,10 +33,13 @@ func (p Path) RemovePoint(number int) Path {
|
||||
return p
|
||||
}
|
||||
|
||||
// Length returns the number of points in the path.
|
||||
func (p Path) Length() int {
|
||||
return len(p.Points)
|
||||
}
|
||||
|
||||
// GetPointNumber returns the path point at the index specified by number.
|
||||
// The index is 1-based.
|
||||
func (p Path) GetPointNumber(number int) Point {
|
||||
if number < 1 || number > len(p.Points) {
|
||||
return Point{}
|
||||
@ -40,6 +47,7 @@ func (p Path) GetPointNumber(number int) Point {
|
||||
return p.Points[number-1]
|
||||
}
|
||||
|
||||
// Copy returns a clone of the path.
|
||||
func (p Path) Copy() Path {
|
||||
pathcopy := Path{}
|
||||
pathcopy.Points = []Point{}
|
||||
@ -49,6 +57,7 @@ func (p Path) Copy() Path {
|
||||
return pathcopy
|
||||
}
|
||||
|
||||
// Offset shifts the path with the specified offsets.
|
||||
func (p Path) Offset(offX, offY float64) Path {
|
||||
for i, pt := range p.Points {
|
||||
p.Points[i] = pt.Add(offX, offY)
|
||||
@ -56,6 +65,7 @@ func (p Path) Offset(offX, offY float64) Path {
|
||||
return p
|
||||
}
|
||||
|
||||
// GetBoundingBox returns the bounding box of the path.
|
||||
func (p Path) GetBoundingBox() BoundingBox {
|
||||
bbox := BoundingBox{}
|
||||
|
||||
@ -93,6 +103,7 @@ func (p Path) GetBoundingBox() BoundingBox {
|
||||
return bbox
|
||||
}
|
||||
|
||||
// BoundingBox represents the smallest rectangular area that encapsulates an object.
|
||||
type BoundingBox struct {
|
||||
X float64
|
||||
Y float64
|
||||
|
@ -11,15 +11,18 @@ import (
|
||||
"github.com/unidoc/unidoc/pdf/internal/transform"
|
||||
)
|
||||
|
||||
// Point represents a two-dimensional point.
|
||||
type Point struct {
|
||||
X float64
|
||||
Y float64
|
||||
}
|
||||
|
||||
// NewPoint returns a new point with the coordinates x, y.
|
||||
func NewPoint(x, y float64) Point {
|
||||
return Point{X: x, Y: y}
|
||||
}
|
||||
|
||||
// Add shifts the coordinates of the point with dx, dy and returns the result.
|
||||
func (p Point) Add(dx, dy float64) Point {
|
||||
p.X += dx
|
||||
p.Y += dy
|
||||
|
@ -171,6 +171,7 @@ func (rect Rectangle) Draw(gsName string) ([]byte, *pdf.PdfRectangle, error) {
|
||||
// The currently supported line ending styles are None, Arrow (ClosedArrow) and Butt.
|
||||
type LineEndingStyle int
|
||||
|
||||
// Line ending styles.
|
||||
const (
|
||||
LineEndingStyleNone LineEndingStyle = 0
|
||||
LineEndingStyleArrow LineEndingStyle = 1
|
||||
@ -180,6 +181,7 @@ const (
|
||||
// LineStyle refers to how the line will be created.
|
||||
type LineStyle int
|
||||
|
||||
// Line styles.
|
||||
const (
|
||||
LineStyleSolid LineStyle = 0
|
||||
LineStyleDashed LineStyle = 1
|
||||
|
@ -7,11 +7,13 @@ package draw
|
||||
|
||||
import "math"
|
||||
|
||||
// Vector represents a two-dimensional vector.
|
||||
type Vector struct {
|
||||
Dx float64
|
||||
Dy float64
|
||||
}
|
||||
|
||||
// NewVector returns a new vector with the direction specified by dx and dy.
|
||||
func NewVector(dx, dy float64) Vector {
|
||||
v := Vector{}
|
||||
v.Dx = dx
|
||||
@ -19,6 +21,8 @@ func NewVector(dx, dy float64) Vector {
|
||||
return v
|
||||
}
|
||||
|
||||
// NewVectorBetween returns a new vector with the direction specified by
|
||||
// the subtraction of point a from point b (b-a).
|
||||
func NewVectorBetween(a Point, b Point) Vector {
|
||||
v := Vector{}
|
||||
v.Dx = b.X - a.X
|
||||
@ -26,6 +30,8 @@ func NewVectorBetween(a Point, b Point) Vector {
|
||||
return v
|
||||
}
|
||||
|
||||
// NewVectorPolar returns a new vector calculated from the specified
|
||||
// magnitude and angle.
|
||||
func NewVectorPolar(length float64, theta float64) Vector {
|
||||
v := Vector{}
|
||||
|
||||
@ -34,12 +40,14 @@ func NewVectorPolar(length float64, theta float64) Vector {
|
||||
return v
|
||||
}
|
||||
|
||||
// Add adds the specified vector to the current one and returns the result.
|
||||
func (v Vector) Add(other Vector) Vector {
|
||||
v.Dx += other.Dx
|
||||
v.Dy += other.Dy
|
||||
return v
|
||||
}
|
||||
|
||||
// Rotate rotates the vector by the specified angle.
|
||||
func (v Vector) Rotate(phi float64) Vector {
|
||||
mag := v.Magnitude()
|
||||
angle := v.GetPolarAngle()
|
||||
@ -57,16 +65,19 @@ func (v Vector) Flip() Vector {
|
||||
return v
|
||||
}
|
||||
|
||||
// FlipY flips the sign of the Dy component of the vector.
|
||||
func (v Vector) FlipY() Vector {
|
||||
v.Dy = -v.Dy
|
||||
return v
|
||||
}
|
||||
|
||||
// FlipX flips the sign of the Dx component of the vector.
|
||||
func (v Vector) FlipX() Vector {
|
||||
v.Dx = -v.Dx
|
||||
return v
|
||||
}
|
||||
|
||||
// Scale scales the vector by the specified factor.
|
||||
func (v Vector) Scale(factor float64) Vector {
|
||||
mag := v.Magnitude()
|
||||
theta := v.GetPolarAngle()
|
||||
@ -76,10 +87,13 @@ func (v Vector) Scale(factor float64) Vector {
|
||||
return v
|
||||
}
|
||||
|
||||
// Magnitude returns the magnitude of the vector.
|
||||
func (v Vector) Magnitude() float64 {
|
||||
return math.Sqrt(math.Pow(v.Dx, 2.0) + math.Pow(v.Dy, 2.0))
|
||||
}
|
||||
|
||||
// GetPolarAngle returns the angle the magnitude of the vector forms with the
|
||||
// positive X-axis going counterclockwise.
|
||||
func (v Vector) GetPolarAngle() float64 {
|
||||
return math.Atan2(v.Dy, v.Dx)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user