From d009f8b70d9775498e668a56994b44e0b4c8fcf8 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Thu, 20 Dec 2012 23:40:31 -0500 Subject: [PATCH] Getting a bunch more of the drawing API implemented including drawing to a very plain X window, albeit an image of nothingness. --- algs4/src/go/algs4-interval2d/main.go | 6 ++++- algs4/src/go/algs4/canvas.go | 31 ++++++++++++++++++++++++ algs4/src/go/algs4/draw.go | 35 ++++++++++++++++++++++----- algs4/src/go/algs4/interval1d.go | 18 ++++++++++---- algs4/src/go/algs4/interval2d.go | 8 +++--- 5 files changed, 83 insertions(+), 15 deletions(-) create mode 100644 algs4/src/go/algs4/canvas.go diff --git a/algs4/src/go/algs4-interval2d/main.go b/algs4/src/go/algs4-interval2d/main.go index 5148506..0acdec2 100644 --- a/algs4/src/go/algs4-interval2d/main.go +++ b/algs4/src/go/algs4-interval2d/main.go @@ -67,7 +67,11 @@ func main() { } } - algs4.Draw() + err = algs4.Draw() + if err != nil { + die(err) + } + fmt.Println(c) fmt.Printf("area = %.2f\n", box.Area()) } diff --git a/algs4/src/go/algs4/canvas.go b/algs4/src/go/algs4/canvas.go new file mode 100644 index 0000000..32f6a22 --- /dev/null +++ b/algs4/src/go/algs4/canvas.go @@ -0,0 +1,31 @@ +package algs4 + +import ( + "image" +) + +var ( + DefaultCanvas = NewCanvas(512, 512) +) + +type Canvas struct { + Height int + Width int + img *image.RGBA +} + +func NewCanvas(height, width int) *Canvas { + return &Canvas{ + Height: height, + Width: width, + img: image.NewRGBA(image.Rect(0, 0, height, width)), + } +} + +func (me *Canvas) Image() *image.RGBA { + return me.img +} + +func (me *Canvas) Rectangle(x, y, halfWidth, halfHeight float64) { + return +} diff --git a/algs4/src/go/algs4/draw.go b/algs4/src/go/algs4/draw.go index 0c44d02..d2dd307 100644 --- a/algs4/src/go/algs4/draw.go +++ b/algs4/src/go/algs4/draw.go @@ -1,12 +1,35 @@ package algs4 -var ( - DefaultCanvas = &Canvas{} +import ( + "errors" + "image" + "image/draw" + + "code.google.com/p/x-go-binding/ui" + "code.google.com/p/x-go-binding/ui/x11" ) -type Canvas struct { -} +func Draw() error { + img := DefaultCanvas.Image() + if img == nil { + return errors.New("DefaultCanvas's image is nil!") + } -func Draw() { - return + w, err := x11.NewWindow() + if err != nil { + return err + } + + draw.Draw(w.Screen(), w.Screen().Bounds(), img, image.ZP, draw.Src) + w.FlushImage() + + for evt := range w.EventChan() { + switch evt := evt.(type) { + case ui.KeyEvent: + if evt.Key == ' ' { + return nil + } + } + } + return nil } diff --git a/algs4/src/go/algs4/interval1d.go b/algs4/src/go/algs4/interval1d.go index f7950d7..200773f 100644 --- a/algs4/src/go/algs4/interval1d.go +++ b/algs4/src/go/algs4/interval1d.go @@ -1,13 +1,21 @@ package algs4 type Interval1D struct { - Lo float64 - Hi float64 + Left float64 + Right float64 } -func NewInterval1D(lo, hi float64) *Interval1D { +func NewInterval1D(left, right float64) *Interval1D { return &Interval1D{ - Lo: lo, - Hi: hi, + Left: left, + Right: right, } } + +func (me *Interval1D) Contains(x float64) bool { + return me.Left <= x && x <= me.Right +} + +func (me *Interval1D) Length() float64 { + return me.Right - me.Left +} diff --git a/algs4/src/go/algs4/interval2d.go b/algs4/src/go/algs4/interval2d.go index 0d16770..25e2a36 100644 --- a/algs4/src/go/algs4/interval2d.go +++ b/algs4/src/go/algs4/interval2d.go @@ -19,13 +19,15 @@ func NewInterval2D(xint, yint *Interval1D, canvas *Canvas) *Interval2D { } func (me *Interval2D) Contains(p *Point2D) bool { - return false + return me.Xint.Contains(p.X) && me.Yint.Contains(p.Y) } func (me *Interval2D) Draw() { - return + xc := (me.Xint.Left + me.Xint.Right) / 2.0 + yc := (me.Yint.Left + me.Yint.Right) / 2.0 + me.Canvas.Rectangle(xc, yc, me.Xint.Length()/2.0, me.Yint.Length()/2.0) } func (me *Interval2D) Area() float64 { - return float64(0) + return me.Xint.Length() * me.Yint.Length() }