func test(t *testing.T, draw sample) { // Initialize the graphic context on an RGBA image dest := image.NewRGBA(image.Rect(0, 0, 297, 210.0)) gc := draw2dimg.NewGraphicContext(dest) // Draw Android logo output, err := draw(gc, "png") if err != nil { t.Errorf("Drawing %q failed: %v", output, err) return } // Save to png err = draw2dimg.SaveToPngFile(output, dest) if err != nil { t.Errorf("Saving %q failed: %v", output, err) } }
func (jc JointCutter) cutInternal(joint PieceJoint, img image.Image) (image.Image, error) { var radius, x, y int //todo dry it up //todo when there is more than one middle row it is not cutting pieces correctly if joint.Side == TOP_SIDE { radius = Percentage(jc.Piece, PERCENTAGE) x, y = img.Bounds().Max.X/2, img.Bounds().Min.Y } else if joint.Side == RIGHT_SIDE { radius = Percentage(jc.Piece, PERCENTAGE) percentInc := 0.0 if hasExternalJoint(jc.Piece, 0) { percentInc += PERCENTAGE x, y = img.Bounds().Max.X, (img.Bounds().Max.Y+Percentage(jc.Piece, percentInc))/2 } else if hasExternalJoint(jc.Piece, 2) { percentInc += PERCENTAGE x, y = img.Bounds().Max.X, (img.Bounds().Max.Y-Percentage(jc.Piece, percentInc))/2 } else { x, y = img.Bounds().Max.X, img.Bounds().Max.Y/2 } } else if joint.Side == BOTTOM_SIDE { radius = Percentage(jc.Piece, PERCENTAGE) x, y = img.Bounds().Max.X/2, img.Bounds().Max.Y } else if joint.Side == LEFT_SIDE { radius = Percentage(jc.Piece, PERCENTAGE) percentInc := 0.0 if hasExternalJoint(jc.Piece, 0) { percentInc += PERCENTAGE x, y = img.Bounds().Min.X, (img.Bounds().Max.Y+Percentage(jc.Piece, percentInc))/2 } else if hasExternalJoint(jc.Piece, 2) { percentInc += PERCENTAGE x, y = img.Bounds().Min.X, (img.Bounds().Max.Y-Percentage(jc.Piece, percentInc))/2 } else { x, y = img.Bounds().Min.X, img.Bounds().Max.Y/2 } } imageContext := image.NewRGBA(img.Bounds()) circleImg := &circle{image.Pt(x, y), radius, img, false, image.Rectangle{}} draw.Draw(imageContext, img.Bounds(), circleImg, img.Bounds().Min, draw.Src) draw2dimg.SaveToPngFile("./out/"+jc.Piece.Name+".png", imageContext) return imageContext, nil }
func TestCircle(t *testing.T) { width := 200 height := 200 img := image.NewRGBA(image.Rect(0, 0, width, height)) gc := draw2dimg.NewGraphicContext(img) gc.SetStrokeColor(color.NRGBA{255, 255, 255, 255}) gc.SetFillColor(color.NRGBA{255, 255, 255, 255}) gc.Clear() gc.SetStrokeColor(color.NRGBA{255, 0, 0, 255}) gc.SetLineWidth(1) // Draw a circle Circle(gc, 100, 100, 50) gc.Stroke() draw2dimg.SaveToPngFile("../output/draw2dkit/TestCircle.png", img) }
//shapes the rectangular piece removing and add joint pieces func (JigsawPieceCutter) ShapePiece(piece *Piece) (*Piece, error) { //fill a semi circle with transparency //the piece is 20% larger on each side add any cuts then crop down sides without external piece jointCutter := JointCutter{Piece: piece} fmt.Println("cutting piece ", piece.Name, piece.Joints) var img = piece.Image var err error for _, joint := range piece.Joints { if !joint.External { img, err = jointCutter.cutInternal(joint, img) if err != nil { return nil, err } } else { img, err = jointCutter.cutExternal(joint, img) } } draw2dimg.SaveToPngFile("./out/"+piece.Name+".png", img) return piece, nil }