Getting a bunch more of the drawing API implemented
including drawing to a very plain X window, albeit an image of nothingness.
This commit is contained in:
parent
2b688fd385
commit
d009f8b70d
@ -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())
|
||||
}
|
||||
|
31
algs4/src/go/algs4/canvas.go
Normal file
31
algs4/src/go/algs4/canvas.go
Normal file
@ -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
|
||||
}
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user