Browse Source

Initial commit

master
commit
a9901ed590
  1. 2
      .gitignore
  2. 51
      README.md
  3. 211
      d2d.go
  4. 75
      examples/example1.go
  5. 37
      examples/example2.go
  6. BIN
      examples/example_image1.png
  7. BIN
      examples/example_image2.png
  8. 3
      go.mod
  9. 85
      shapes/circle.go
  10. 96
      shapes/line.go
  11. 52
      shapes/rectangle.go
  12. 100
      shapes/triangle.go

2
.gitignore vendored

@ -0,0 +1,2 @@
example1
example2

51
README.md

@ -0,0 +1,51 @@
# d2d - draw 2D
## the most minimal, probably most lacking, but simple 2d drawing Go package
![Example image](https://unbewohnte.su:3000/Unbewohnte/d2d/examples/example_image2.png)
## What can it do ?
Draw
- Points
- Lines
- Rectangles (Filled and empty)
- Circles (Filled and empty)
of any color that satisfies basic stdlib's color.Color interface
## Installation
`go get unbewohnte.su:3000/Unbewohnte/d2d` - if it doesn't work, your best choice is to just download it manually or copy-paste the code into your project
## Usage
The main part of the package - `Canvas` struct that is essentially just a wrapper to `draw.Image`. Though the drawing can be done via direct work with shapes, canvas comes with additional quality of life drawing functions such as `Border`, `FillWhole`, `Grid`, `FloodFill`.
### Example
```
canvas := d2d.NewCanvas(1920, 1080)
canvas.FillWhole(color.RGBA{60, 180, 30, 255})
canvas.Border(5, color.Black)
canvas.DrawFilledCircle(shapes.NewCircle(image.Pt(500, 500), 80), color.Black)
canvas.DrawLine(shapes.NewLine(image.Pt(0, 0), image.Pt(1920, 1080)), color.Black)
canvas.DrawFilledTriangle(
shapes.NewTriangle(
image.Pt(900, 20),
image.Pt(1400, 400),
image.Pt(500, 300),
),
color.Black,
)
err := canvas.SaveAsPNG("image.png")
if err != nil {
panic(err)
}
```
## License
MIT

211
d2d.go

@ -0,0 +1,211 @@
/*
The MIT License (MIT)
Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package d2d
import (
"image"
"image/color"
"image/draw"
"image/png"
"os"
"unbewohnte/d2d/shapes"
)
// Image wrapper
type Canvas struct {
innerImage draw.Image
}
// Creates new canvas with provided resolution
func NewCanvas(width uint16, height uint16) *Canvas {
return &Canvas{
innerImage: image.NewNRGBA(image.Rect(0, 0, int(width), int(height))),
}
}
// Returns a pointer to the wrapped draw.Image
func (c *Canvas) InnerImage() *draw.Image {
return &c.innerImage
}
// Fills the whole canvas with color
func (c *Canvas) FillWhole(color color.Color) {
for y := 0; y < c.innerImage.Bounds().Dy(); y++ {
for x := 0; x < c.innerImage.Bounds().Dx(); x++ {
c.innerImage.Set(x, y, color)
}
}
}
// Swaps old neighboring colors with a new color
func (c *Canvas) FloodFill(pt image.Point, oldColor color.Color, newColor color.Color) {
if c.innerImage.At(pt.X, pt.Y) == c.innerImage.ColorModel().Convert(oldColor) {
c.innerImage.Set(pt.X, pt.Y, newColor)
c.FloodFill(image.Pt(pt.X-1, pt.Y), oldColor, newColor)
c.FloodFill(image.Pt(pt.X+1, pt.Y), oldColor, newColor)
c.FloodFill(image.Pt(pt.X, pt.Y-1), oldColor, newColor)
c.FloodFill(image.Pt(pt.X, pt.Y+1), oldColor, newColor)
}
}
// Save image data as PNG file
func (c *Canvas) SaveAsPNG(filepath string) error {
file, err := os.Create(filepath)
if err != nil {
return err
}
err = png.Encode(file, c.innerImage)
if err != nil {
return err
}
return nil
}
// Returns the bottom-right point of canvas
func (c *Canvas) Bounds() image.Point {
return image.Pt(c.innerImage.Bounds().Dx(), c.innerImage.Bounds().Dy())
}
// Scales up image by magnitude
func (c *Canvas) ScaleUp(magnitude uint8) {
var scaledCanvasImage *image.NRGBA = image.NewNRGBA(
image.Rect(
0,
0,
c.innerImage.Bounds().Dx()*int(magnitude),
c.innerImage.Bounds().Dy()*int(magnitude),
),
)
var basePixelColor color.Color
for y0 := 0; y0 < c.innerImage.Bounds().Dy(); y0++ {
for x0 := 0; x0 < c.innerImage.Bounds().Dx(); x0++ {
basePixelColor = c.innerImage.At(x0, y0)
for y := int(magnitude) * y0; y < int(magnitude)*(y0+1); y++ {
for x := int(magnitude) * x0; x < int(magnitude)*(x0+1); x++ {
scaledCanvasImage.Set(x, y, basePixelColor)
}
}
}
}
c.innerImage = scaledCanvasImage
}
// Draws a point on canvas
func (c *Canvas) DrawPoint(pt image.Point, color color.Color) {
c.innerImage.Set(pt.X, pt.Y, color)
}
// Draws a circle on canvas
func (c *Canvas) DrawCircle(circle shapes.Circle, color color.Color) {
circle.Draw(c.innerImage, color)
}
// Draws a filled circle on canvas
func (c *Canvas) DrawFilledCircle(circle shapes.Circle, color color.Color) {
circle.DrawFilled(c.innerImage, color)
}
// Draws a rectangle on canvas
func (c *Canvas) DrawRectangle(rectangle shapes.Rectangle, color color.Color) {
rectangle.Draw(c.innerImage, color)
}
// Draws a filled rectangle on canvas
func (c *Canvas) DrawFilledRectangle(rectangle shapes.Rectangle, color color.Color) {
rectangle.DrawFilled(c.innerImage, color)
}
// Draws a line on canvas
func (c *Canvas) DrawLine(line shapes.Line, color color.Color) {
line.Draw(c.innerImage, color)
}
// Draws a triangle on canvas
func (c *Canvas) DrawTriangle(triangle shapes.Triangle, color color.Color) {
triangle.Draw(c.innerImage, color)
}
// Draws a filled triangle on canvas
func (c *Canvas) DrawFilledTriangle(triangle shapes.Triangle, color color.Color) {
triangle.DrawFilled(c.innerImage, color)
}
// Draws a grid on canvas
func (c *Canvas) Grid(border shapes.Rectangle, spacing uint16, lineWidth uint16, color color.Color) {
if lineWidth == 0 {
return
}
for x := border.UpperLeft.X; x < border.BottomRight.X; x += int(lineWidth + spacing) {
c.DrawFilledRectangle(
shapes.NewRectangle(
image.Pt(x, border.UpperLeft.Y),
image.Pt(x+int(lineWidth), border.BottomRight.Y),
),
color,
)
}
for y := border.UpperLeft.Y; y < border.BottomRight.Y; y += int(lineWidth + spacing) {
c.DrawFilledRectangle(
shapes.NewRectangle(
image.Pt(border.UpperLeft.X, y),
image.Pt(border.BottomRight.X, y+int(lineWidth)),
),
color,
)
}
}
// Draws a canvas border
func (c *Canvas) Border(width uint16, color color.Color) {
// upper part
c.DrawFilledRectangle(
shapes.NewRectangle(
image.Pt(0, 0),
image.Pt(c.innerImage.Bounds().Dx(), int(width)),
),
color,
)
// left part
c.DrawFilledRectangle(
shapes.NewRectangle(
image.Pt(0, 0),
image.Pt(int(width), c.innerImage.Bounds().Dy()),
),
color,
)
// right part
c.DrawFilledRectangle(
shapes.NewRectangle(
image.Pt(c.innerImage.Bounds().Dx()-int(width), 0),
image.Pt(c.innerImage.Bounds().Dx(), c.innerImage.Bounds().Dy()),
),
color,
)
// bottom part
c.DrawFilledRectangle(
shapes.NewRectangle(
image.Pt(0, c.innerImage.Bounds().Dy()-int(width)),
image.Pt(c.innerImage.Bounds().Dx(), c.innerImage.Bounds().Dy()),
),
color,
)
}

75
examples/example1.go

@ -0,0 +1,75 @@
package main
import (
"image"
"image/color"
"unbewohnte/d2d"
"unbewohnte/d2d/shapes"
)
const (
width int = 600
height int = 600
)
func main() {
var canvas = d2d.NewCanvas(uint16(width), uint16(height))
canvas.FillWhole(color.RGBA{237, 223, 123, 255})
canvas.DrawFilledRectangle(
shapes.NewRectangle(
image.Pt(0+width/10, 0),
image.Pt(width/10+10, height),
),
color.RGBA{255, 190, 11, 255},
)
canvas.DrawFilledRectangle(
shapes.NewRectangle(
image.Pt(width-width/10, 0),
image.Pt(width-width/10+10, height),
),
color.RGBA{255, 190, 11, 255},
)
var j uint8 = 0
j = 0
for i := width / 2; i > 1; i-- {
canvas.DrawFilledCircle(
shapes.NewCircle(
image.Pt(width/2, height/2),
i,
),
color.RGBA{131, 56 + j, 236, 255},
)
j++
}
for i := width / 3; i > 1; i-- {
canvas.DrawFilledCircle(
shapes.NewCircle(
image.Pt(width/2, height/2),
i,
),
color.RGBA{255, 190, 11 + j, 255},
)
j++
}
j = 0
for i := width / 5; i > 1; i-- {
canvas.DrawFilledCircle(
shapes.NewCircle(
image.Pt(width/2, height/2),
i,
),
color.RGBA{251, 86 + j, 7, 255},
)
j++
}
err := canvas.SaveAsPNG("example_image1.png")
if err != nil {
panic(err)
}
}

37
examples/example2.go

@ -0,0 +1,37 @@
package main
import (
"image"
"image/color"
"unbewohnte/d2d"
"unbewohnte/d2d/shapes"
)
const (
width int = 1400
height int = 800
)
func main() {
var canvas = d2d.NewCanvas(uint16(width), uint16(height))
canvas.FillWhole(color.RGBA{237, 223, 123, 255})
canvas.Border(5, color.Black)
canvas.DrawPoint(image.Pt(500, 700), color.Black)
canvas.DrawFilledCircle(shapes.NewCircle(image.Pt(500, 500), 80), color.Black)
canvas.DrawFilledRectangle(shapes.NewRectangle(image.Pt(20, 20), image.Pt(300, 300)), color.Black)
canvas.DrawLine(shapes.NewLine(image.Pt(0, 0), image.Pt(1920, 1080)), color.Black)
canvas.DrawFilledTriangle(
shapes.NewTriangle(
image.Pt(900, 20),
image.Pt(1400, 400),
image.Pt(500, 300),
),
color.Black,
)
err := canvas.SaveAsPNG("example_image2.png")
if err != nil {
panic(err)
}
}

BIN
examples/example_image1.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

BIN
examples/example_image2.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

3
go.mod

@ -0,0 +1,3 @@
module unbewohnte/d2d
go 1.18

85
shapes/circle.go

@ -0,0 +1,85 @@
/*
The MIT License (MIT)
Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package shapes
import (
"image"
"image/color"
"image/draw"
)
type Circle struct {
Center image.Point
Radius int
}
func NewCircle(center image.Point, radius int) Circle {
if radius < 0 {
radius = -radius
}
return Circle{
Center: center,
Radius: radius,
}
}
func (c *Circle) Draw(canvas draw.Image, color color.Color) {
var (
x int = c.Radius - 1
y int = 0
dx int = 1
dy int = 1
err int = dx - (c.Radius * 2)
)
for x > y {
canvas.Set(c.Center.X+x, c.Center.Y+y, color)
canvas.Set(c.Center.X+y, c.Center.Y+x, color)
canvas.Set(c.Center.X-y, c.Center.Y+x, color)
canvas.Set(c.Center.X-x, c.Center.Y+y, color)
canvas.Set(c.Center.X-x, c.Center.Y-y, color)
canvas.Set(c.Center.X-y, c.Center.Y-x, color)
canvas.Set(c.Center.X+y, c.Center.Y-x, color)
canvas.Set(c.Center.X+x, c.Center.Y-y, color)
if err <= 0 {
y++
err += dy
dy += 2
}
if err > 0 {
x--
dx += 2
err += dx - (c.Radius * 2)
}
}
}
func (c *Circle) DrawFilled(canvas draw.Image, color color.Color) {
c.Draw(canvas, color)
var (
dx int
dy int
)
for y := c.Center.Y - c.Radius; y < c.Center.Y+c.Radius; y++ {
dy = y - c.Center.Y
for x := c.Center.X - c.Radius; x < c.Center.X+c.Radius; x++ {
dx = x - c.Center.X
if (dx*dx + dy*dy) < (c.Radius * c.Radius) {
canvas.Set(x, y, color)
}
}
}
}

96
shapes/line.go

@ -0,0 +1,96 @@
/*
The MIT License (MIT)
Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package shapes
import (
"image"
"image/color"
"image/draw"
)
type Line struct {
Start image.Point
End image.Point
}
func NewLine(start image.Point, end image.Point) Line {
return Line{
Start: start,
End: end,
}
}
func (l *Line) Draw(canvas draw.Image, color color.Color) {
var (
x0 = l.Start.X
x1 = l.End.X
y0 = l.Start.Y
y1 = l.End.Y
dx int = x1 - x0
sx int
dy int = y1 - y0
sy int
err int
e2 int
)
// abs dx
if dx < 0 {
dx = -dx
}
// -abs dy
if dy < 0 {
dy = -dy
}
dy = -dy
// error
err = dx + dy
// sx
if x0 < x1 {
sx = 1
} else {
sx = -1
}
//sy
if y0 < y1 {
sy = 1
} else {
sy = -1
}
for {
canvas.Set(x0, y0, color)
if x0 == x1 && y0 == y1 {
break
}
e2 = 2 * err
if e2 >= dy {
if x0 == x1 {
break
}
err = err + dy
x0 = x0 + sx
}
if e2 <= dx {
if y0 == y1 {
break
}
err = err + dx
y0 = y0 + sy
}
}
}

52
shapes/rectangle.go

@ -0,0 +1,52 @@
/*
The MIT License (MIT)
Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package shapes
import (
"image"
"image/color"
"image/draw"
)
type Rectangle struct {
UpperLeft image.Point
BottomRight image.Point
}
func NewRectangle(upperLeft image.Point, bottomRight image.Point) Rectangle {
return Rectangle{
UpperLeft: upperLeft,
BottomRight: bottomRight,
}
}
func (r *Rectangle) Draw(canvas draw.Image, color color.Color) {
for y := r.UpperLeft.Y; y <= r.BottomRight.Y; y++ {
canvas.Set(r.UpperLeft.X, y, color)
canvas.Set(r.BottomRight.X, y, color)
}
for x := r.UpperLeft.X; x <= r.BottomRight.X; x++ {
canvas.Set(x, r.UpperLeft.Y, color)
canvas.Set(x, r.BottomRight.Y, color)
}
}
func (r *Rectangle) DrawFilled(canvas draw.Image, color color.Color) {
for y := 0; y < r.BottomRight.Y-r.UpperLeft.Y; y++ {
for x := 0; x < r.BottomRight.X-r.UpperLeft.X; x++ {
canvas.Set(x+r.UpperLeft.X, y+r.UpperLeft.Y, color)
}
}
}

100
shapes/triangle.go

@ -0,0 +1,100 @@
/*
The MIT License (MIT)
Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package shapes
import (
"image"
"image/color"
"image/draw"
"math"
)
type Triangle struct {
p0 image.Point
p1 image.Point
p2 image.Point
}
func NewTriangle(p0 image.Point, p1 image.Point, p2 image.Point) Triangle {
return Triangle{
p0: p0,
p1: p1,
p2: p2,
}
}
func (t *Triangle) Draw(canvas draw.Image, color color.Color) {
line0 := NewLine(t.p0, t.p1)
line0.Draw(canvas, color)
line1 := NewLine(t.p1, t.p2)
line1.Draw(canvas, color)
line11 := NewLine(t.p2, t.p1)
line11.Draw(canvas, color)
line2 := NewLine(t.p2, t.p0)
line2.Draw(canvas, color)
}
func max(a int, b int) int {
if a > b {
return a
}
return b
}
func min(a int, b int) int {
if a < b {
return a
}
return b
}
func area(p0 image.Point, p1 image.Point, p2 image.Point) float32 {
return float32(
math.Abs(
float64((p0.X*(p1.Y-p2.Y) + p1.X*(p2.Y-p0.Y) + p2.X*(p0.Y-p1.Y))) / 2.0,
),
)
}
func (t *Triangle) isPointInside(pt image.Point) bool {
var (
a = area(t.p0, t.p1, t.p2)
a1 = area(pt, t.p1, t.p2)
a2 = area(t.p0, pt, t.p2)
a3 = area(t.p0, t.p1, pt)
)
return (a == a1+a2+a3)
}
func (t *Triangle) DrawFilled(canvas draw.Image, color color.Color) {
t.Draw(canvas, color)
maxX := max(t.p0.X, max(t.p1.X, t.p2.X))
maxY := max(t.p0.Y, max(t.p1.Y, t.p2.Y))
minX := min(t.p0.X, min(t.p1.X, t.p2.X))
minY := min(t.p0.Y, min(t.p1.Y, t.p2.Y))
for y := minY; y < maxY; y++ {
for x := minX; x < maxX; x++ {
if t.isPointInside(image.Pt(x, y)) {
canvas.Set(x, y, color)
}
}
}
}
Loading…
Cancel
Save