This commit is contained in:
Laurent Le Goff 2011-05-18 23:23:31 +02:00
parent 713b1fa1f6
commit b7d000715e
6 changed files with 56 additions and 72 deletions

View file

@ -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
} }

View file

@ -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
@ -145,13 +145,12 @@ func (curve *CubicCurveFloat64) Segment(segments []float64) ([]float64) {
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
} }

View file

@ -8,7 +8,6 @@ import (
"bufio" "bufio"
"image" "image"
"image/png" "image/png"
"exp/draw"
"draw2d.googlecode.com/hg/draw2d/raster" "draw2d.googlecode.com/hg/draw2d/raster"
) )
@ -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) {
} }
} }
} }

View file

@ -71,5 +71,3 @@ func (pathAdder *PathAdder) Convert(paths ...*PathStorage) {
} }
} }
} }

View file

@ -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)