From cc37e5c6581f710aad197a9c5042ba8e3757f803 Mon Sep 17 00:00:00 2001 From: Laurent Le Goff Date: Thu, 19 May 2011 10:44:16 +0200 Subject: [PATCH] Draw points of segments to see subdivion --- draw2d/curve/curve_test.go | 27 +++++++++++++++++++++++++-- draw2d/raster/line.go | 3 +-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/draw2d/curve/curve_test.go b/draw2d/curve/curve_test.go index a490116..951f4e6 100644 --- a/draw2d/curve/curve_test.go +++ b/draw2d/curve/curve_test.go @@ -8,6 +8,7 @@ import ( "bufio" "image" "image/png" + "exp/draw" "draw2d.googlecode.com/hg/draw2d/raster" ) @@ -56,6 +57,22 @@ func savepng(filePath string, m image.Image) { } } +func drawPoints(img draw.Image, c image.Color, s ...float64) image.Image { + for i := 0; i < len(s); i += 2 { + x, y := int(s[i]+0.5), int(s[i+1]+0.5) + img.Set(x, y, c) + img.Set(x, y+1, c) + img.Set(x, y-1, c) + img.Set(x+1, y, c) + img.Set(x+1, y+1, c) + img.Set(x+1, y-1, c) + img.Set(x-1, y, c) + img.Set(x-1, y+1, c) + img.Set(x-1, y-1, c) + + } + return img +} func TestCubicCurveRec(t *testing.T) { for i, curve := range testsFloat64 { @@ -67,7 +84,10 @@ func TestCubicCurveRec(t *testing.T) { s = curve.SegmentRec(s) img := image.NewNRGBA(300, 300) 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), raster.PolylineBresenham(img, image.Black, s...)) + raster.PolylineBresenham(img, image.Black, s...) + drawPoints(img, image.NRGBAColor{0, 0, 0, 0xff}, curve.X1, curve.Y1, curve.X2, curve.Y2, curve.X3, curve.Y3, curve.X4, curve.Y4) + drawPoints(img, image.NRGBAColor{0, 0, 0, 0xff}, s...) + savepng(fmt.Sprintf("_testRec%d.png", i), img) log.Printf("Num of points: %d\n", len(s)) } } @@ -82,7 +102,10 @@ func TestCubicCurve(t *testing.T) { s = curve.Segment(s) img := image.NewNRGBA(300, 300) 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), raster.PolylineBresenham(img, image.Black, s...)) + raster.PolylineBresenham(img, image.Black, s...) + drawPoints(img, image.NRGBAColor{0, 0, 0, 0xff}, curve.X1, curve.Y1, curve.X2, curve.Y2, curve.X3, curve.Y3, curve.X4, curve.Y4) + drawPoints(img, image.NRGBAColor{0, 0, 0, 0xff}, s...) + savepng(fmt.Sprintf("_test%d.png", i), img) log.Printf("Num of points: %d\n", len(s)) } } diff --git a/draw2d/raster/line.go b/draw2d/raster/line.go index fa8ee13..2a8c0a8 100644 --- a/draw2d/raster/line.go +++ b/draw2d/raster/line.go @@ -12,11 +12,10 @@ func abs(i int) int { return i } -func PolylineBresenham(img draw.Image, c image.Color, s ...float64) image.Image { +func PolylineBresenham(img draw.Image, c image.Color, s ...float64) { 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) {