Skip to content

Commit

Permalink
canvas: Replace TransformPoint/DrawPixel with AddPixel (#1022)
Browse files Browse the repository at this point in the history
Instead of having each widget (like `Plot`) have to call
`TransformPoint` and then separately draw a pixel, add a new `AddPixel`
that works like all the other canvas path commands.
  • Loading branch information
rohansingh authored Mar 28, 2024
1 parent a9b8a70 commit a3e42c0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 32 deletions.
9 changes: 3 additions & 6 deletions render/canvas/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ type Canvas interface {
// AddLineTo adds a line to the current path, from the current point to (x, y).
AddLineTo(x, y float64)

// AddPixel adds a pixel to the current path at (x, y).
AddPixel(x, y int)

// AddRectangle adds a rectangle to the current path.
AddRectangle(x, y, w, h float64)

Expand All @@ -47,9 +50,6 @@ type Canvas interface {
// DrawImageFromBuffer draws the given image at (x, y).
DrawImageFromBuffer(x, y, w, h float64, img []byte)

// DrawPixel draws a pixel at (x, y).
DrawPixel(x, y int)

// DrawString draws the given text at (x, y).
DrawString(x, y float64, text string)

Expand Down Expand Up @@ -85,9 +85,6 @@ type Canvas interface {
// SetFont sets the current font.
SetFont(font *fonts.Font)

// TransformPoint does a thing.
TransformPoint(x, y float64) (ax, ay float64)

// Translate translates the canvas by (dx, dy) for future drawing operations.
Translate(dx, dy float64)
}
Expand Down
12 changes: 4 additions & 8 deletions render/canvas/gg.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func (c *GGCanvas) AddLineTo(x, y float64) {
c.dc.LineTo(x, y)
}

func (c *GGCanvas) AddPixel(x, y int) {
c.dc.DrawRectangle(float64(x), float64(y), 1, 1)
}

func (c *GGCanvas) AddRectangle(x, y, w, h float64) {
c.dc.DrawRectangle(x, y, w, h)
}
Expand All @@ -54,10 +58,6 @@ func (c *GGCanvas) DrawImageFromBuffer(x, y, w, h float64, img []byte) {
c.dc.DrawImage(im, int(x), int(y))
}

func (c *GGCanvas) DrawPixel(x, y int) {
c.dc.SetPixel(x, y)
}

func (c *GGCanvas) DrawString(x, y float64, text string) {
c.dc.DrawString(text, x, y)
}
Expand Down Expand Up @@ -117,10 +117,6 @@ func (c *GGCanvas) SetFont(font *fonts.Font) {
c.dc.SetFontFace(c.currentFontFace)
}

func (c *GGCanvas) TransformPoint(x, y float64) (ax, ay float64) {
return c.dc.TransformPoint(x, y)
}

func (c *GGCanvas) Translate(dx, dy float64) {
c.dc.Translate(dx, dy)
}
15 changes: 8 additions & 7 deletions render/plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,14 @@ func (p Plot) Paint(dc canvas.Canvas, bounds image.Rectangle, frameIdx int) {
if y > p.invThreshold {
dc.SetColor(fillColInv)
for ; y != p.invThreshold && y >= 0; y-- {
tx, ty := dc.TransformPoint(float64(x), float64(y))
dc.DrawPixel(int(tx), int(ty))
dc.AddPixel(x, y)
dc.FillPath()
}
} else {
dc.SetColor(fillCol)
for ; y <= p.invThreshold && y <= p.Height; y++ {
tx, ty := dc.TransformPoint(float64(x), float64(y))
dc.DrawPixel(int(tx), int(ty))
dc.AddPixel(x, y)
dc.FillPath()
}
}
}
Expand All @@ -252,7 +252,8 @@ func (p Plot) Paint(dc canvas.Canvas, bounds image.Rectangle, frameIdx int) {
} else {
dc.SetColor(col)
}
dc.DrawPixel(int(point.X), int(point.Y))
dc.AddPixel(point.X, point.Y)
dc.FillPath()
}
} else {
// the line itself
Expand All @@ -263,8 +264,8 @@ func (p Plot) Paint(dc canvas.Canvas, bounds image.Rectangle, frameIdx int) {
} else {
dc.SetColor(col)
}
tx, ty := dc.TransformPoint(float64(x), float64(y))
dc.DrawPixel(int(tx), int(ty))
dc.AddPixel(x, y)
dc.FillPath()
}
}
}
Expand Down
16 changes: 9 additions & 7 deletions render/starfield.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,23 @@ func DrawLine(dc canvas.Canvas, x0, y0, x1, y1 int) {

if dx == 0 {
for ; y0 != y1; y0 += sy {
dc.DrawPixel(x0, y0)
dc.AddPixel(x0, y0)
}
dc.DrawPixel(x0, y0)
dc.AddPixel(x0, y0)
return
}

if dy == 0 {
for ; x0 != x1; x0 += sx {
dc.DrawPixel(x0, y0)
dc.AddPixel(x0, y0)
}
dc.DrawPixel(x0, y0)
dc.AddPixel(x0, y0)
return
}

err := dx + dy

dc.DrawPixel(x0, y0)
dc.AddPixel(x0, y0)
for x0 != x1 && y0 != y1 {
e2 := 2 * err
if e2 >= dy {
Expand All @@ -88,7 +88,7 @@ func DrawLine(dc canvas.Canvas, x0, y0, x1, y1 int) {
err += dx
y0 += sy
}
dc.DrawPixel(x0, y0)
dc.AddPixel(x0, y0)
}
}

Expand Down Expand Up @@ -141,9 +141,11 @@ func (s *Starfield) Paint(dc canvas.Canvas, bounds image.Rectangle, frameIdx int
if pX != 0 && pY != 0 && (pX != X || pY != Y) {
dc.SetColor(color.RGBA{0x22, 0x22, 0x22, 0xff})
DrawLine(dc, pX, pY, X, Y)
dc.FillPath()
}
dc.SetColor(color.RGBA{0xff, 0xff, 0xff, 0xff})
dc.DrawPixel(X, Y)
dc.AddPixel(X, Y)
dc.FillPath()
}

dc.SetColor(color.RGBA{0xff, 0xff, 0xff, 0xff})
Expand Down
8 changes: 4 additions & 4 deletions render/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ func (t Tracer) Paint(dc canvas.Canvas, bounds image.Rectangle, frameIdx int) {
x, y := t.Path.Point(frameIdx)

dc.SetColor(color.RGBA{0xff, 0xff, 0xff, 0xff})
tx, ty := dc.TransformPoint(float64(x), float64(y))
dc.DrawPixel(int(tx), int(ty))
dc.AddPixel(x, y)
dc.FillPath()

for i := 0; i < t.TraceLength; i++ {
col := uint8(0xdd - i*(0xff/t.TraceLength))
dc.SetColor(color.RGBA{col, col, col, 0xff})
x, y := t.Path.Point(frameIdx - (i + 1))
tx, ty := dc.TransformPoint(float64(x), float64(y))
dc.DrawPixel(int(tx), int(ty))
dc.AddPixel(x, y)
dc.FillPath()
}
}

0 comments on commit a3e42c0

Please sign in to comment.