diff --git a/draw2dkit/draw2dkit.go b/draw2dkit/draw2dkit.go new file mode 100644 index 0000000..1cb3820 --- /dev/null +++ b/draw2dkit/draw2dkit.go @@ -0,0 +1,46 @@ +// Copyright 2010 The draw2d Authors. All rights reserved. +// created: 13/12/2010 by Laurent Le Goff + +// Package draw2dkit procides helpers to draw common figure and draw using a Path or a GraphicContext +package draw2dkit + +import ( + "github.com/llgcode/draw2d" + "math" +) + +// Rectangle draws a rectangle using a PathBuilder +func Rectangle(path draw2d.PathBuilder, x1, y1, x2, y2 float64) { + path.MoveTo(x1, y1) + path.LineTo(x2, y1) + path.LineTo(x2, y2) + path.LineTo(x1, y2) + path.Close() +} + +// RoundedRectangle draws a rounded rectangle using a PathBuilder +func RoundedRectangle(path draw2d.PathBuilder, x1, y1, x2, y2, arcWidth, arcHeight float64) { + arcWidth = arcWidth / 2 + arcHeight = arcHeight / 2 + path.MoveTo(x1, y1+arcHeight) + path.QuadCurveTo(x1, y1, x1+arcWidth, y1) + path.LineTo(x2-arcWidth, y1) + path.QuadCurveTo(x2, y1, x2, y1+arcHeight) + path.LineTo(x2, y2-arcHeight) + path.QuadCurveTo(x2, y2, x2-arcWidth, y2) + path.LineTo(x1+arcWidth, y2) + path.QuadCurveTo(x1, y2, x1, y2-arcHeight) + path.Close() +} + +// Ellipse draws an ellipse using a PathBuilder +func Ellipse(path draw2d.PathBuilder, cx, cy, rx, ry float64) { + path.ArcTo(cx, cy, rx, ry, 0, -math.Pi*2) + path.Close() +} + +// Circle draws a circle using a PathBuilder +func Circle(path draw2d.PathBuilder, cx, cy, radius float64) { + path.ArcTo(cx, cy, radius, radius, 0, -math.Pi*2) + path.Close() +} diff --git a/draw2dkit/droid.go b/draw2dkit/droid.go new file mode 100644 index 0000000..5bd62dd --- /dev/null +++ b/draw2dkit/droid.go @@ -0,0 +1,50 @@ +package draw2dkit + +import ( + "github.com/llgcode/draw2d" + "math" +) + +func Droid(gc draw2d.GraphicContext, x, y float64) { + gc.SetLineCap(draw2d.RoundCap) + gc.SetLineWidth(5) + + // head + gc.ArcTo(x+80, y+70, 50, 50, 180*(math.Pi/180), 360*(math.Pi/180)) + gc.FillStroke() + gc.MoveTo(x+60, y+25) + gc.LineTo(x+50, y+10) + gc.MoveTo(x+100, y+25) + gc.LineTo(x+110, y+10) + gc.Stroke() + + // left eye + draw2d.Circle(gc, x+60, y+45, 5) + gc.FillStroke() + + // right eye + draw2d.Circle(gc, x+100, y+45, 5) + gc.FillStroke() + + // body + draw2d.RoundedRectangle(gc, x+30, y+75, x+30+100, y+75+90, 10, 10) + gc.FillStroke() + draw2d.Rectangle(gc, x+30, y+75, x+30+100, y+75+80) + gc.FillStroke() + + // left arm + draw2d.RoundedRectangle(gc, x+5, y+80, x+5+20, y+80+70, 10, 10) + gc.FillStroke() + + // right arm + draw2d.RoundedRectangle(gc, x+135, y+80, x+135+20, y+80+70, 10, 10) + gc.FillStroke() + + // left leg + draw2d.RoundedRectangle(gc, x+50, y+150, x+50+20, y+150+50, 10, 10) + gc.FillStroke() + + // right leg + draw2d.RoundedRectangle(gc, x+90, y+150, x+90+20, y+150+50, 10, 10) + gc.FillStroke() +} diff --git a/draw2dkit/gopher.go b/draw2dkit/gopher.go new file mode 100644 index 0000000..f2b743a --- /dev/null +++ b/draw2dkit/gopher.go @@ -0,0 +1,56 @@ +// Copyright 2010 The draw2d Authors. All rights reserved. +// created: 21/11/2010 by Laurent Le Goff + +package draw2dkit + +import ( + "image/color" + + "github.com/llgcode/draw2d" +) + +// Gopher draw a gopher using a gc thanks to https://github.com/golang-samples/gopher-vector/ +func Gopher(gc draw2d.GraphicContext, x, y, w, h float64) { + // Initialize Stroke Attribute + gc.SetLineWidth(3) + gc.SetLineCap(draw2d.RoundCap) + gc.SetStrokeColor(color.Black) + + // Left hand + // + gc.SetFillColor(color.RGBA{0xF6, 0xD2, 0xA2, 0xff}) + gc.MoveTo(10.634, 300.493) + rCubicCurveTo(gc, 0.764, 15.751, 16.499, 8.463, 23.626, 3.539) + rCubicCurveTo(gc, 6.765, -4.675, 8.743, -0.789, 9.337, -10.015) + rCubicCurveTo(gc, 0.389, -6.064, 1.088, -12.128, 0.744, -18.216) + rCubicCurveTo(gc, -10.23, -0.927, -21.357, 1.509, -29.744, 7.602) + gc.CubicCurveTo(10.277, 286.542, 2.177, 296.561, 10.634, 300.493) + gc.FillStroke() + + // + gc.MoveTo(10.634, 300.493) + rCubicCurveTo(gc, 2.29, -0.852, 4.717, -1.457, 6.271, -3.528) + gc.Stroke() + + // Left Ear + // + gc.MoveTo(46.997, 112.853) + gc.CubicCurveTo(-13.3, 95.897, 31.536, 19.189, 79.956, 50.74) + gc.LineTo(46.997, 112.853) + gc.Close() + gc.Stroke() +} + +func rQuadCurveTo(path draw2d.PathBuilder, dcx, dcy, dx, dy float64) { + x, y := path.LastPoint() + path.QuadCurveTo(x+dcx, y+dcy, x+dx, y+dy) +} + +func rCubicCurveTo(path draw2d.PathBuilder, dcx1, dcy1, dcx2, dcy2, dx, dy float64) { + x, y := path.LastPoint() + path.CubicCurveTo(x+dcx1, y+dcy1, x+dcx2, y+dcy2, x+dx, y+dy) +}