Skip to content

Commit

Permalink
separated out a Path2D type
Browse files Browse the repository at this point in the history
  • Loading branch information
tfriedel6 committed Jan 23, 2019
1 parent 7565296 commit bb244c4
Show file tree
Hide file tree
Showing 9 changed files with 284 additions and 227 deletions.
4 changes: 2 additions & 2 deletions canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Canvas struct {
x, y, w, h int
fx, fy, fw, fh float64

path path
path Path2D
convex bool
rect bool

Expand Down Expand Up @@ -52,7 +52,7 @@ type drawState struct {
lineDashOffset float64

scissor scissor
clip path
clip Path2D

shadowColor glColor
shadowOffsetX float64
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ require (
golang.org/x/exp v0.0.0-20181106170214-d68db9428509
golang.org/x/image v0.0.0-20181109002202-aa35264064ba
golang.org/x/mobile v0.0.0-20181026062114-a27dd33d354d
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8 // indirect
golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ golang.org/x/image v0.0.0-20181109002202-aa35264064ba h1:tKfAeDKyjJZwxAJ8TPBZaf6
golang.org/x/image v0.0.0-20181109002202-aa35264064ba/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=
golang.org/x/mobile v0.0.0-20181026062114-a27dd33d354d h1:DuZZDdMFwDrzmycNhCaWSve7Vh+BIrjm7ttgb4fD3Os=
golang.org/x/mobile v0.0.0-20181026062114-a27dd33d354d/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8 h1:YoY1wS6JYVRpIfFngRf2HHo9R9dAne3xbkGOQ5rJXjU=
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35 h1:YAFjXN64LMvktoUZH9zgY4lGc/msGN7HQfoSuKCgaDU=
golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
254 changes: 254 additions & 0 deletions path2d.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
package canvas

import "math"

type Path2D struct {
p []pathPoint
move vec
cwSum float64
}

type pathPoint struct {
pos vec
tf vec
next vec
flags pathPointFlag
}

type pathPointFlag uint8

const (
pathMove pathPointFlag = 1 << iota
pathAttach
pathIsRect
pathIsConvex
pathIsClockwise
pathSelfIntersects
)

// NewPath2D creates a new Path2D and returns it
func NewPath2D() *Path2D {
return &Path2D{p: make([]pathPoint, 0, 20)}
}

// func (p *Path2D) AddPath(p2 *Path2D) {
// }

// MoveTo (see equivalent function on canvas type)
func (p *Path2D) MoveTo(x, y float64) {
if len(p.p) > 0 && isSamePoint(p.p[len(p.p)-1].pos, vec{x, y}, 0.1) {
return
}
p.p = append(p.p, pathPoint{pos: vec{x, y}, tf: vec{x, y}, flags: pathMove}) // todo more flags probably
p.cwSum = 0
p.move = vec{x, y}
}

// LineTo (see equivalent function on canvas type)
func (p *Path2D) LineTo(x, y float64) {
count := len(p.p)
if count > 0 && isSamePoint(p.p[len(p.p)-1].pos, vec{x, y}, 0.1) {
return
}
if count == 0 {
p.MoveTo(x, y)
return
}
prev := &p.p[count-1]
prev.next = vec{x, y}
prev.flags |= pathAttach
p.p = append(p.p, pathPoint{pos: vec{x, y}, tf: vec{x, y}})
newp := &p.p[count]

px, py := prev.pos[0], prev.pos[1]
p.cwSum += (x - px) * (y + py)
cwTotal := p.cwSum
cwTotal += (p.move[0] - x) * (p.move[1] + y)
if cwTotal <= 0 {
newp.flags |= pathIsClockwise
}

if prev.flags&pathSelfIntersects > 0 {
newp.flags |= pathSelfIntersects
}

if len(p.p) < 4 {
newp.flags |= pathIsConvex
} else if prev.flags&pathIsConvex > 0 {
cuts := false
b0, b1 := prev.pos, vec{x, y}
for i := 1; i < count; i++ {
a0, a1 := p.p[i-1].pos, p.p[i].pos
_, r1, r2 := lineIntersection(a0, a1, b0, b1)
if r1 > 0 && r1 < 1 && r2 > 0 && r2 < 1 {
cuts = true
break
}
}
if cuts {
newp.flags |= pathSelfIntersects
} else {
prev2 := &p.p[len(p.p)-3]
cw := (newp.flags & pathIsClockwise) > 0

ln := prev.pos.sub(prev2.pos)
lo := vec{ln[1], -ln[0]}
dot := newp.pos.sub(prev2.pos).dot(lo)

if (cw && dot <= 0) || (!cw && dot >= 0) {
newp.flags |= pathIsConvex
}
}
}
}

// Arc (see equivalent function on canvas type)
func (p *Path2D) Arc(x, y, radius, startAngle, endAngle float64, anticlockwise bool) {
lastWasMove := len(p.p) == 0 || p.p[len(p.p)-1].flags&pathMove != 0

startAngle = math.Mod(startAngle, math.Pi*2)
if startAngle < 0 {
startAngle += math.Pi * 2
}
endAngle = math.Mod(endAngle, math.Pi*2)
if endAngle < 0 {
endAngle += math.Pi * 2
}
if !anticlockwise && endAngle <= startAngle {
endAngle += math.Pi * 2
} else if anticlockwise && endAngle >= startAngle {
endAngle -= math.Pi * 2
}
const step = math.Pi * 2 / 360
if anticlockwise {
for a := startAngle; a > endAngle; a -= step {
s, c := math.Sincos(a)
p.LineTo(x+radius*c, y+radius*s)
}
} else {
for a := startAngle; a < endAngle; a += step {
s, c := math.Sincos(a)
p.LineTo(x+radius*c, y+radius*s)
}
}
s, c := math.Sincos(endAngle)
p.LineTo(x+radius*c, y+radius*s)

if lastWasMove {
p.p[len(p.p)-1].flags |= pathIsConvex
}
}

// ArcTo (see equivalent function on canvas type)
func (p *Path2D) ArcTo(x1, y1, x2, y2, radius float64) {
if len(p.p) == 0 {
return
}
p0, p1, p2 := p.p[len(p.p)-1].pos, vec{x1, y1}, vec{x2, y2}
v0, v1 := p0.sub(p1).norm(), p2.sub(p1).norm()
angle := math.Acos(v0.dot(v1))
// should be in the range [0-pi]. if parallel, use a straight line
if angle <= 0 || angle >= math.Pi {
p.LineTo(x2, y2)
return
}
// cv are the vectors orthogonal to the lines that point to the center of the circle
cv0 := vec{-v0[1], v0[0]}
cv1 := vec{v1[1], -v1[0]}
x := cv1.sub(cv0).div(v0.sub(v1))[0] * radius
if x < 0 {
cv0 = cv0.mulf(-1)
cv1 = cv1.mulf(-1)
}
center := p1.add(v0.mulf(math.Abs(x))).add(cv0.mulf(radius))
a0, a1 := cv0.mulf(-1).atan2(), cv1.mulf(-1).atan2()
p.Arc(center[0], center[1], radius, a0, a1, x > 0)
}

// QuadraticCurveTo (see equivalent function on canvas type)
func (p *Path2D) QuadraticCurveTo(x1, y1, x2, y2 float64) {
if len(p.p) == 0 {
return
}
p0 := p.p[len(p.p)-1].pos
p1 := vec{x1, y1}
p2 := vec{x2, y2}
v0 := p1.sub(p0)
v1 := p2.sub(p1)

const step = 0.01

for r := 0.0; r < 1; r += step {
i0 := v0.mulf(r).add(p0)
i1 := v1.mulf(r).add(p1)
pt := i1.sub(i0).mulf(r).add(i0)
p.LineTo(pt[0], pt[1])
}
p.LineTo(x2, y2)
}

// BezierCurveTo (see equivalent function on canvas type)
func (p *Path2D) BezierCurveTo(x1, y1, x2, y2, x3, y3 float64) {
if len(p.p) == 0 {
return
}
p0 := p.p[len(p.p)-1].pos
p1 := vec{x1, y1}
p2 := vec{x2, y2}
p3 := vec{x3, y3}
v0 := p1.sub(p0)
v1 := p2.sub(p1)
v2 := p3.sub(p2)

const step = 0.01

for r := 0.0; r < 1; r += step {
i0 := v0.mulf(r).add(p0)
i1 := v1.mulf(r).add(p1)
i2 := v2.mulf(r).add(p2)
iv0 := i1.sub(i0)
iv1 := i2.sub(i1)
j0 := iv0.mulf(r).add(i0)
j1 := iv1.mulf(r).add(i1)
pt := j1.sub(j0).mulf(r).add(j0)
p.LineTo(pt[0], pt[1])
}
p.LineTo(x3, y3)
}

// ClosePath (see equivalent function on canvas type)
func (p *Path2D) ClosePath() {
if len(p.p) < 2 {
return
}
if isSamePoint(p.p[len(p.p)-1].pos, p.p[0].pos, 0.1) {
return
}
closeIdx := 0
for i := len(p.p) - 1; i >= 0; i-- {
if p.p[i].flags&pathMove != 0 {
closeIdx = i
break
}
}
p.LineTo(p.p[closeIdx].pos[0], p.p[closeIdx].pos[1])
p.p[len(p.p)-1].next = p.p[closeIdx].next
p.p[len(p.p)-1].flags |= pathAttach
}

// Rect (see equivalent function on canvas type)
func (p *Path2D) Rect(x, y, w, h float64) {
lastWasMove := len(p.p) == 0 || p.p[len(p.p)-1].flags&pathMove != 0
p.MoveTo(x, y)
p.LineTo(x+w, y)
p.LineTo(x+w, y+h)
p.LineTo(x, y+h)
p.LineTo(x, y)
if lastWasMove {
p.p[len(p.p)-1].flags |= pathIsRect
p.p[len(p.p)-1].flags |= pathIsConvex
}
}

// func (p *Path2D) Ellipse(...) {
// }
Loading

0 comments on commit bb244c4

Please sign in to comment.