gofmt
This commit is contained in:
parent
713b1fa1f6
commit
b7d000715e
6 changed files with 56 additions and 72 deletions
|
@ -53,7 +53,7 @@ func reshape(w, h int) {
|
||||||
gl.Translatef(0, float32(-h), 0) /* Shift origin up to upper-left corner. */
|
gl.Translatef(0, float32(-h), 0) /* Shift origin up to upper-left corner. */
|
||||||
gl.Enable(gl.BLEND)
|
gl.Enable(gl.BLEND)
|
||||||
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
|
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
|
||||||
gl.Disable(gl.DEPTH_TEST);
|
gl.Disable(gl.DEPTH_TEST)
|
||||||
width, height = w, h
|
width, height = w, h
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,29 +27,29 @@ func (c *CubicCurveFloat64) ArbitraryPoint(mu float64) (x, y float64) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CubicCurveFloat64) SubdivideAt(c1, c2 *CubicCurveFloat64, t float64) {
|
func (c *CubicCurveFloat64) SubdivideAt(c1, c2 *CubicCurveFloat64, t float64) {
|
||||||
inv_t := (1 - t)
|
inv_t := (1 - t)
|
||||||
c1.X1, c1.Y1 = c.X1, c.Y1
|
c1.X1, c1.Y1 = c.X1, c.Y1
|
||||||
c2.X4, c2.Y4 = c.X4, c.Y4
|
c2.X4, c2.Y4 = c.X4, c.Y4
|
||||||
|
|
||||||
c1.X2 = inv_t * c.X1 + t * c.X2
|
c1.X2 = inv_t*c.X1 + t*c.X2
|
||||||
c1.Y2 = inv_t * c.Y1 + t * c.Y2
|
c1.Y2 = inv_t*c.Y1 + t*c.Y2
|
||||||
|
|
||||||
x23 := inv_t * c.X2 + t * c.X3
|
x23 := inv_t*c.X2 + t*c.X3
|
||||||
y23 := inv_t * c.Y2 + t * c.Y3
|
y23 := inv_t*c.Y2 + t*c.Y3
|
||||||
|
|
||||||
c2.X3 = inv_t * c.X3 + t * c.X4
|
c2.X3 = inv_t*c.X3 + t*c.X4
|
||||||
c2.Y3 = inv_t * c.Y3 + t * c.Y4
|
c2.Y3 = inv_t*c.Y3 + t*c.Y4
|
||||||
|
|
||||||
c1.X3 = inv_t * c1.X2 + t * x23
|
c1.X3 = inv_t*c1.X2 + t*x23
|
||||||
c1.Y3 = inv_t * c1.Y2 + t * y23
|
c1.Y3 = inv_t*c1.Y2 + t*y23
|
||||||
|
|
||||||
c2.X2 = inv_t * x23 + t * c2.X3
|
c2.X2 = inv_t*x23 + t*c2.X3
|
||||||
c2.Y2 = inv_t * y23 + t * c2.Y3
|
c2.Y2 = inv_t*y23 + t*c2.Y3
|
||||||
|
|
||||||
c1.X4 = inv_t * c1.X3 + t * c2.X2
|
c1.X4 = inv_t*c1.X3 + t*c2.X2
|
||||||
c1.Y4 = inv_t * c1.Y3 + t * c2.Y2
|
c1.Y4 = inv_t*c1.Y3 + t*c2.Y2
|
||||||
|
|
||||||
c2.X1, c2.Y1 = c1.X4, c1.Y4
|
c2.X1, c2.Y1 = c1.X4, c1.Y4
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CubicCurveFloat64) Subdivide(c1, c2 *CubicCurveFloat64) {
|
func (c *CubicCurveFloat64) Subdivide(c1, c2 *CubicCurveFloat64) {
|
||||||
|
@ -121,7 +121,7 @@ func (c *CubicCurveFloat64) segmentRec(segments []float64) []float64 {
|
||||||
return segments
|
return segments
|
||||||
}
|
}
|
||||||
|
|
||||||
func (curve *CubicCurveFloat64) Segment(segments []float64) ([]float64) {
|
func (curve *CubicCurveFloat64) Segment(segments []float64) []float64 {
|
||||||
// Add the first point
|
// Add the first point
|
||||||
segments = segments[0 : len(segments)+2]
|
segments = segments[0 : len(segments)+2]
|
||||||
segments[len(segments)-2] = curve.X1
|
segments[len(segments)-2] = curve.X1
|
||||||
|
@ -141,17 +141,16 @@ func (curve *CubicCurveFloat64) Segment(segments []float64) ([]float64) {
|
||||||
d2 = math.Fabs(((c.X2-c.X4)*dy - (c.Y2-c.Y4)*dx))
|
d2 = math.Fabs(((c.X2-c.X4)*dy - (c.Y2-c.Y4)*dx))
|
||||||
d3 = math.Fabs(((c.X3-c.X4)*dy - (c.Y3-c.Y4)*dx))
|
d3 = math.Fabs(((c.X3-c.X4)*dy - (c.Y3-c.Y4)*dx))
|
||||||
|
|
||||||
if (d2+d3)*(d2+d3) < flattening_threshold*(dx*dx+dy*dy) || i == len(curves) - 1 {
|
if (d2+d3)*(d2+d3) < flattening_threshold*(dx*dx+dy*dy) || i == len(curves)-1 {
|
||||||
segments = segments[0 : len(segments)+2]
|
segments = segments[0 : len(segments)+2]
|
||||||
segments[len(segments)-2] = c.X4
|
segments[len(segments)-2] = c.X4
|
||||||
segments[len(segments)-1] = c.Y4
|
segments[len(segments)-1] = c.Y4
|
||||||
i--;
|
i--
|
||||||
} else {
|
} else {
|
||||||
// second half of bezier go lower onto the stack
|
// second half of bezier go lower onto the stack
|
||||||
c.Subdivide(&curves[i+1], &curves[i])
|
c.Subdivide(&curves[i+1], &curves[i])
|
||||||
i++;
|
i++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return segments
|
return segments
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,16 +8,15 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"image"
|
"image"
|
||||||
"image/png"
|
"image/png"
|
||||||
"exp/draw"
|
|
||||||
"draw2d.googlecode.com/hg/draw2d/raster"
|
"draw2d.googlecode.com/hg/draw2d/raster"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
testsFloat64 = []CubicCurveFloat64 {
|
testsFloat64 = []CubicCurveFloat64{
|
||||||
CubicCurveFloat64{100, 100, 200, 100, 100, 200, 200, 200},
|
CubicCurveFloat64{100, 100, 200, 100, 100, 200, 200, 200},
|
||||||
CubicCurveFloat64{100, 100, 300, 200, 200, 200, 200, 100},
|
CubicCurveFloat64{100, 100, 300, 200, 200, 200, 200, 100},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -34,15 +33,6 @@ func init() {
|
||||||
}
|
}
|
||||||
f.Write([]byte("</body></html>"))
|
f.Write([]byte("</body></html>"))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func rasterPolyline(img draw.Image, c image.Color, s ...float64) image.Image {
|
|
||||||
for i := 2; i < len(s); i+=2 {
|
|
||||||
raster.Bresenham(img, c, int(s[i-2]+0.5), int(s[i-1]+0.5), int(s[i]+0.5), int(s[i+1]+0.5))
|
|
||||||
}
|
|
||||||
return img
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func savepng(filePath string, m image.Image) {
|
func savepng(filePath string, m image.Image) {
|
||||||
|
@ -75,8 +65,8 @@ func TestCubicCurveCasteljauRec(t *testing.T) {
|
||||||
s := make([]float64, 0, numSegments)
|
s := make([]float64, 0, numSegments)
|
||||||
s = curve.SegmentRec(s)
|
s = curve.SegmentRec(s)
|
||||||
img := image.NewNRGBA(300, 300)
|
img := image.NewNRGBA(300, 300)
|
||||||
rasterPolyline(img, image.NRGBAColor{0xff, 0, 0, 0xff}, curve.X1, curve.Y1, curve.X2, curve.Y2, curve.X3, curve.Y3, curve.X4, curve.Y4)
|
raster.PolylineBresenham(img, image.NRGBAColor{0xff, 0, 0, 0xff}, curve.X1, curve.Y1, curve.X2, curve.Y2, curve.X3, curve.Y3, curve.X4, curve.Y4)
|
||||||
savepng(fmt.Sprintf("_testRec%d.png", i), rasterPolyline(img, image.Black, s...))
|
savepng(fmt.Sprintf("_testRec%d.png", i), raster.PolylineBresenham(img, image.Black, s...))
|
||||||
log.Printf("Num of points: %d\n", len(s))
|
log.Printf("Num of points: %d\n", len(s))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -90,8 +80,8 @@ func TestCubicCurveCasteljau(t *testing.T) {
|
||||||
s := make([]float64, 0, numSegments)
|
s := make([]float64, 0, numSegments)
|
||||||
s = curve.Segment(s)
|
s = curve.Segment(s)
|
||||||
img := image.NewNRGBA(300, 300)
|
img := image.NewNRGBA(300, 300)
|
||||||
rasterPolyline(img, image.NRGBAColor{0xff, 0, 0, 0xff}, curve.X1, curve.Y1, curve.X2, curve.Y2, curve.X3, curve.Y3, curve.X4, curve.Y4)
|
raster.PolylineBresenham(img, image.NRGBAColor{0xff, 0, 0, 0xff}, curve.X1, curve.Y1, curve.X2, curve.Y2, curve.X3, curve.Y3, curve.X4, curve.Y4)
|
||||||
savepng(fmt.Sprintf("_test%d.png", i), rasterPolyline(img, image.Black, s...))
|
savepng(fmt.Sprintf("_test%d.png", i), raster.PolylineBresenham(img, image.Black, s...))
|
||||||
log.Printf("Num of points: %d\n", len(s))
|
log.Printf("Num of points: %d\n", len(s))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -118,13 +108,3 @@ func BenchmarkCubicCurveCasteljau(b *testing.B) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -9,8 +9,8 @@ import (
|
||||||
|
|
||||||
|
|
||||||
type VertexAdder struct {
|
type VertexAdder struct {
|
||||||
command VertexCommand
|
command VertexCommand
|
||||||
adder raster.Adder
|
adder raster.Adder
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewVertexAdder(adder raster.Adder) *VertexAdder {
|
func NewVertexAdder(adder raster.Adder) *VertexAdder {
|
||||||
|
@ -34,7 +34,7 @@ func (vertexAdder *VertexAdder) Vertex(x, y float64) {
|
||||||
|
|
||||||
type PathAdder struct {
|
type PathAdder struct {
|
||||||
adder raster.Adder
|
adder raster.Adder
|
||||||
firstPoint raster.Point
|
firstPoint raster.Point
|
||||||
ApproximationScale float64
|
ApproximationScale float64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,5 +71,3 @@ func (pathAdder *PathAdder) Convert(paths ...*PathStorage) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,13 @@ func abs(i int) int {
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func PolylineBresenham(img draw.Image, c image.Color, s ...float64) image.Image {
|
||||||
|
for i := 2; i < len(s); i += 2 {
|
||||||
|
Bresenham(img, c, int(s[i-2]+0.5), int(s[i-1]+0.5), int(s[i]+0.5), int(s[i+1]+0.5))
|
||||||
|
}
|
||||||
|
return img
|
||||||
|
}
|
||||||
|
|
||||||
func Bresenham(img draw.Image, color image.Color, x0, y0, x1, y1 int) {
|
func Bresenham(img draw.Image, color image.Color, x0, y0, x1, y1 int) {
|
||||||
dx := abs(x1 - x0)
|
dx := abs(x1 - x0)
|
||||||
dy := abs(y1 - y0)
|
dy := abs(y1 - y0)
|
||||||
|
|
|
@ -40,8 +40,8 @@ func (p *GLPainter) Paint(ss []raster.Span, done bool) {
|
||||||
p.vertices = p.vertices[0 : vi+vlenrequired]
|
p.vertices = p.vertices[0 : vi+vlenrequired]
|
||||||
p.colors = p.colors[0 : ci+clenrequired]
|
p.colors = p.colors[0 : ci+clenrequired]
|
||||||
var (
|
var (
|
||||||
colors []uint8
|
colors []uint8
|
||||||
vertices []int32
|
vertices []int32
|
||||||
)
|
)
|
||||||
for _, s := range ss {
|
for _, s := range ss {
|
||||||
ma := s.A >> 16
|
ma := s.A >> 16
|
||||||
|
|
Loading…
Reference in a new issue