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 (
|
2019-12-29 15:35:12 +01:00
|
|
|
"math"
|
2019-04-13 17:16:18 +02:00
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
2019-04-13 17:16:18 +02:00
|
|
|
)
|
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
type point struct {
|
|
|
|
x float32
|
|
|
|
y float32
|
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 {
|
2021-07-02 12:26:09 +02:00
|
|
|
segs [][]point
|
|
|
|
cur 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) {
|
2021-07-02 12:26:09 +02:00
|
|
|
p.cur = point{x: x, y: y}
|
|
|
|
p.segs = append(p.segs, []point{p.cur})
|
2019-04-13 17:16:18 +02:00
|
|
|
}
|
|
|
|
|
2019-12-27 03:26:16 +01: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 {
|
2021-07-02 12:26:09 +02:00
|
|
|
p.segs = append(p.segs, []point{p.cur})
|
2019-04-13 19:34:42 +02:00
|
|
|
}
|
2021-07-02 12:26:09 +02:00
|
|
|
p.segs[len(p.segs)-1] = append(p.segs[len(p.segs)-1], point{x: x, y: y})
|
|
|
|
p.cur = point{x: x, y: y}
|
2019-04-13 17:16:18 +02:00
|
|
|
}
|
|
|
|
|
2020-03-16 15:49:55 +01:00
|
|
|
// nseg returns a number of segments based on the given two points (x0, y0) and (x1, y1).
|
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
|
|
|
|
}
|
|
|
|
|
2019-12-29 15:35:12 +01:00
|
|
|
return int(math.Ceil(float64(dist)))
|
2019-12-28 17:47:18 +01:00
|
|
|
}
|
|
|
|
|
2020-03-16 15:49:55 +01:00
|
|
|
// QuadTo adds a quadratic Bézier curve to the path.
|
2019-12-30 08:14:47 +01:00
|
|
|
func (p *Path) QuadTo(cpx, cpy, x, y float32) {
|
2021-07-02 12:26:09 +02:00
|
|
|
// TODO: Split more appropriate number of segments.
|
2019-12-28 17:47:18 +01:00
|
|
|
c := p.cur
|
2021-07-02 12:26:09 +02: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) {
|
2021-07-02 12:26:09 +02:00
|
|
|
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
|
2019-12-28 16:05:03 +01:00
|
|
|
p.LineTo(xf, yf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 15:49:55 +01:00
|
|
|
// CubicTo adds a cubic Bézier curve to the path.
|
2019-12-30 08:14:47 +01:00
|
|
|
func (p *Path) CubicTo(cp0x, cp0y, cp1x, cp1y, x, y float32) {
|
2021-07-02 12:26:09 +02:00
|
|
|
// TODO: Split more appropriate number of segments.
|
2019-12-28 16:05:03 +01:00
|
|
|
c := p.cur
|
2021-07-02 12:26:09 +02: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) {
|
2021-07-02 12:26:09 +02:00
|
|
|
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
|
2019-12-28 06:57:50 +01:00
|
|
|
p.LineTo(xf, yf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
// AppendVerticesAndIndices appends vertices and indices for this path and returns them.
|
|
|
|
// AppendVerticesAndIndices works in a similar way to the built-in append function.
|
|
|
|
// If the arguments are nils, AppendVerticesAndIndices returns new slices.
|
|
|
|
//
|
|
|
|
// The returned vertice's SrcX and SrcY are 0, and ColorR, ColorG, ColorB, and ColorA are 1.
|
|
|
|
//
|
|
|
|
// The returned values are intended to be passed to DrawTriangles or DrawTrianglesShader with EvenOdd option
|
|
|
|
// in order to render a complex polygon like a concave polygon, a polygon with holes, or a self-intersecting polygon.
|
|
|
|
func (p *Path) AppendVerticesAndIndices(vertices []ebiten.Vertex, indices []uint16) ([]ebiten.Vertex, []uint16) {
|
|
|
|
// TODO: Add tests.
|
2019-04-13 17:16:18 +02:00
|
|
|
|
2019-12-27 03:26:16 +01:00
|
|
|
var base uint16
|
|
|
|
for _, seg := range p.segs {
|
2021-07-02 12:26:09 +02:00
|
|
|
if len(seg) < 3 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for i, pt := range seg {
|
2019-12-27 03:26:16 +01:00
|
|
|
vertices = append(vertices, ebiten.Vertex{
|
2021-07-02 12:26:09 +02:00
|
|
|
DstX: pt.x,
|
|
|
|
DstY: pt.y,
|
2019-12-27 03:26:16 +01:00
|
|
|
SrcX: 0,
|
|
|
|
SrcY: 0,
|
2021-07-02 12:26:09 +02:00
|
|
|
ColorR: 1,
|
|
|
|
ColorG: 1,
|
|
|
|
ColorB: 1,
|
|
|
|
ColorA: 1,
|
2019-12-27 03:26:16 +01:00
|
|
|
})
|
2021-07-02 12:26:09 +02:00
|
|
|
if i < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
indices = append(indices, base, base+uint16(i-1), base+uint16(i))
|
2019-12-27 03:26:16 +01:00
|
|
|
}
|
|
|
|
base += uint16(len(seg))
|
2019-04-13 17:16:18 +02:00
|
|
|
}
|
2021-07-02 12:26:09 +02:00
|
|
|
return vertices, indices
|
2019-04-13 17:16:18 +02:00
|
|
|
}
|