ebiten/vector/path.go

132 lines
3.3 KiB
Go
Raw Normal View History

2019-04-13 17:16:18 +02:00
// Copyright 2019 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package vector provides functions for vector graphics rendering.
//
// This package is under experiments and the API might be changed with breaking backward compatibility.
package vector
import (
"image/color"
"github.com/hajimehoshi/ebiten"
2019-04-14 08:48:17 +02:00
"github.com/hajimehoshi/ebiten/vector/internal/math"
2019-04-13 17:16:18 +02:00
)
var emptyImage *ebiten.Image
func init() {
emptyImage, _ = ebiten.NewImage(1, 1, ebiten.FilterDefault)
2019-07-30 18:58:54 +02:00
emptyImage.Fill(color.White)
2019-04-13 17:16:18 +02:00
}
2019-12-26 08:35:07 +01:00
// Path represents a collection of path segments.
2019-04-13 17:16:18 +02:00
type Path struct {
segs [][]math.Point
2019-04-14 08:48:17 +02:00
cur math.Point
2019-04-13 17:16:18 +02:00
}
// MoveTo skips the current position of the path to the given position (x, y) without adding any strokes.
func (p *Path) MoveTo(x, y float32) {
p.cur = math.Point{X: x, Y: y}
p.segs = append(p.segs, []math.Point{p.cur})
2019-04-13 17:16:18 +02:00
}
// LineTo adds a line segument to the path, which starts from the current position and ends to the given position (x, y).
2019-04-13 17:16:18 +02:00
//
// LineTo updates the current position to (x, y).
func (p *Path) LineTo(x, y float32) {
2019-04-13 19:34:42 +02:00
if len(p.segs) == 0 {
p.segs = append(p.segs, []math.Point{p.cur})
2019-04-13 19:34:42 +02:00
}
p.segs[len(p.segs)-1] = append(p.segs[len(p.segs)-1], math.Point{X: x, Y: y})
p.cur = math.Point{X: x, Y: y}
2019-04-13 17:16:18 +02:00
}
2019-12-28 17:47:18 +01:00
func nseg(x0, y0, x1, y1 float32) int {
distx := x1 - x0
if distx < 0 {
distx = -distx
}
disty := y1 - y0
if disty < 0 {
disty = -disty
}
dist := distx
if dist < disty {
dist = disty
}
num := 1
for float32(num) < dist {
num *= 2
}
2019-12-28 17:47:18 +01:00
return num
}
func (p *Path) QuadraticCurveTo(cpx, cpy, x, y float32) {
c := p.cur
num := nseg(c.X, c.Y, x, y)
2019-12-28 16:05:03 +01:00
for t := float32(0.0); t <= 1; t += 1.0 / float32(num) {
xf := (1-t)*(1-t)*c.X + 2*t*(1-t)*cpx + t*t*x
yf := (1-t)*(1-t)*c.Y + 2*t*(1-t)*cpy + t*t*y
p.LineTo(xf, yf)
}
}
func (p *Path) BezierCurveTo(cp0x, cp0y, cp1x, cp1y, x, y float32) {
c := p.cur
2019-12-28 17:47:18 +01:00
num := nseg(c.X, c.Y, x, y)
2019-12-28 16:05:03 +01:00
for t := float32(0.0); t <= 1; t += 1.0 / float32(num) {
xf := (1-t)*(1-t)*(1-t)*c.X + 3*(1-t)*(1-t)*t*cp0x + 3*(1-t)*t*t*cp1x + t*t*t*x
yf := (1-t)*(1-t)*(1-t)*c.Y + 3*(1-t)*(1-t)*t*cp0y + 3*(1-t)*t*t*cp1y + t*t*t*y
p.LineTo(xf, yf)
}
}
func (p *Path) Fill(dst *ebiten.Image, clr color.Color) {
var vertices []ebiten.Vertex
var indices []uint16
2019-04-13 17:16:18 +02:00
r, g, b, a := clr.RGBA()
var rf, gf, bf, af float32
if a > 0 {
rf = float32(r) / float32(a)
gf = float32(g) / float32(a)
bf = float32(b) / float32(a)
af = float32(a) / 0xffff
2019-04-13 17:16:18 +02:00
}
var base uint16
for _, seg := range p.segs {
for _, pt := range seg {
vertices = append(vertices, ebiten.Vertex{
DstX: pt.X,
DstY: pt.Y,
SrcX: 0,
SrcY: 0,
ColorR: rf,
ColorG: gf,
ColorB: bf,
ColorA: af,
})
}
for _, idx := range math.Triangulate(seg) {
indices = append(indices, idx+base)
}
base += uint16(len(seg))
2019-04-13 17:16:18 +02:00
}
dst.DrawTriangles(vertices, indices, emptyImage, nil)
2019-04-13 17:16:18 +02:00
}