aboutsummaryrefslogtreecommitdiff
path: root/lines
diff options
context:
space:
mode:
authorsina <sina@snix.ir>2022-10-28 01:46:02 +0330
committersina <sina@snix.ir>2022-10-28 01:46:02 +0330
commit708ed84f9b9643eabf5991f6e9e27fe451ecc3cf (patch)
tree4cbb9ea5c7fbd4a7757eae229c79ed9435e62639 /lines
adding sources
Diffstat (limited to 'lines')
-rw-r--r--lines/img.go218
-rw-r--r--lines/num.go69
2 files changed, 287 insertions, 0 deletions
diff --git a/lines/img.go b/lines/img.go
new file mode 100644
index 0000000..ec6ab09
--- /dev/null
+++ b/lines/img.go
@@ -0,0 +1,218 @@
+package lines
+
+import (
+ "image"
+ "image/color"
+ "image/draw"
+ "image/png"
+ "os"
+)
+
+const sizesimpix = 20
+
+const (
+ white = iota
+ darks
+ bered
+)
+
+type BreLines struct {
+ img *image.RGBA
+ col map[byte]color.Color
+ pngfile *os.File
+ imgsize int
+ pixsize int
+}
+
+func (p *BreLines) GetIMG() *image.RGBA { return p.img }
+
+func NewBreLine(size int, path string) (*BreLines, error) {
+ imageRGBA := image.NewRGBA(image.Rect(0, 0, size, size))
+ cols := make(map[byte]color.Color)
+ cols[darks] = color.Gray16{0xdada}
+ cols[white] = color.White
+ cols[bered] = color.RGBA{255, 0, 0, 255}
+
+ file, err := os.Create(path)
+ if err != nil {
+ return nil, err
+ }
+
+ return &BreLines{
+ img: imageRGBA, col: cols, pngfile: file,
+ pixsize: int(size / sizesimpix), imgsize: size}, err
+}
+
+func (p *BreLines) DrawMesh() {
+ draw.Draw(
+ p.img, p.img.Bounds(),
+ &image.Uniform{p.col[white]}, image.Point{}, draw.Src,
+ )
+
+ var locx int
+ var icor byte
+ for currx := 0; currx < p.img.Bounds().Max.X; currx++ {
+ var locy int
+ for curr_y := 0; curr_y < sizesimpix; curr_y++ {
+ fg := image.Rect(locx, locy, locx+p.pixsize, locy+p.pixsize)
+ un := &image.Uniform{p.col[icor]}
+ draw.Draw(p.img, fg, un, image.Point{}, draw.Src)
+ locy += p.pixsize
+ icor = 1 - icor
+ }
+ locx += p.pixsize
+ icor = 1 - icor
+ }
+}
+
+func (p *BreLines) setPixel(x, y int) {
+ x = x * p.pixsize
+ y = (sizesimpix - 1 - y) * p.pixsize
+ draw.Draw(
+ p.img, image.Rect(x, y, x+p.pixsize, y+p.pixsize),
+ &image.Uniform{p.col[bered]}, image.Point{}, draw.Src,
+ )
+
+}
+
+func (p *BreLines) WriteToFile() error {
+ defer p.pngfile.Close()
+ return png.Encode(p.pngfile, newXAndYAxis(p.imgsize).drawXandY(p.img))
+}
+
+func (p *BreLines) lessThanOrEqOne(x1, y1 int, x2, y2 int, dx, dy int) {
+ dx = abs(dx)
+ dy = abs(dy)
+
+ Po := (2 * dy) - dx
+ p.setPixel(x1, y1)
+ xk, yk := x1, y1
+ for k := x1; k < x2; k++ {
+ if Po < 0 {
+ xk++
+ p.setPixel(xk, yk)
+ Po = Po + (2 * dy)
+ } else {
+ xk++
+ yk++
+ p.setPixel(xk, yk)
+ Po = Po + (2 * dy) - (2 * dx)
+ }
+ }
+}
+
+func (p *BreLines) greaterThanOne(x1, y1 int, x2, y2 int, dx, dy int) {
+ dx = abs(dx)
+ dy = abs(dy)
+ Po := (2 * dx) - dy
+ p.setPixel(x1, y1)
+ xk, yk := x1, y1
+
+ for k := y1; k < y2; k++ {
+ if Po < 0 {
+ yk++
+ p.setPixel(xk, yk)
+ Po = Po + (2 * dx)
+ } else {
+ xk++
+ yk++
+ p.setPixel(xk, yk)
+ Po = Po + (2 * dx) - (2 * dy)
+ }
+ }
+}
+
+func (p *BreLines) lessThanOrEqNegOne(x1, y1 int, x2, y2 int, dx, dy int) {
+ dx = abs(dx)
+ dy = abs(dy)
+ Po := (2 * dy) - dx
+ p.setPixel(x1, y1)
+ xk, yk := x1, y1
+
+ for k := x1; k < x2; k++ {
+ if Po < 0 {
+ xk++
+ p.setPixel(xk, yk)
+ Po = Po + (2 * dy)
+ } else {
+ xk++
+ yk--
+ p.setPixel(xk, yk)
+ Po = Po + (2 * dy) - (2 * dx)
+ }
+ }
+}
+
+func (p *BreLines) greaterThanNegOne(x1, y1 int, x2, y2 int, dx, dy int) {
+ dx = abs(dx)
+ dy = abs(dy)
+ Po := (2 * dy) - dx
+ p.setPixel(x1, y1)
+
+ xk, yk := x1, y1
+
+ for k := y1; k > y2; k-- {
+ if Po < 0 {
+ yk--
+ p.setPixel(xk, yk)
+ Po = Po + (2 * dx)
+ } else {
+ xk++
+ yk--
+ p.setPixel(xk, yk)
+ Po = Po + (2 * dx) - (2 * dy)
+ }
+ }
+
+}
+
+func (p *BreLines) BresenhamLine(x1, y1 int, x2, y2 int) {
+
+ // swap if point start is greater than point end
+ if x1 > x2 {
+ tempx := x2
+ tempy := y2
+ x2 = x1
+ y2 = y1
+ x1 = tempx
+ y1 = tempy
+ }
+
+ dx := x2 - x1 // delat X
+ dy := y2 - y1 // delta Y
+
+ // for 0 < M <= 1, constant increment on x axis
+ if dy <= dx && dy > 0 {
+ p.lessThanOrEqOne(x1, y1, x2, y2, dx, dy)
+ return
+
+ }
+
+ // for M > 1, constant increment on y axis
+ if dy > dx && dy > 0 {
+ p.greaterThanOne(x1, y1, x2, y2, dx, dy)
+ return
+
+ }
+
+ // for 0 > M > -1 constant increment on x axis
+ // decrement on y axis
+ if dy >= -dx {
+ p.lessThanOrEqNegOne(x1, y1, x2, y2, dx, dy)
+ return
+
+ }
+
+ // for M < -1 constant decrement on y axis
+ // increment on x axis
+ if dy < -dx {
+ p.greaterThanNegOne(x1, y1, x2, y2, dx, dy)
+ }
+}
+
+func abs(a int) int {
+ if a < 0 {
+ return -a
+ }
+ return a
+}
diff --git a/lines/num.go b/lines/num.go
new file mode 100644
index 0000000..40becd0
--- /dev/null
+++ b/lines/num.go
@@ -0,0 +1,69 @@
+package lines
+
+import (
+ "fmt"
+ "image"
+ "image/color"
+ "image/draw"
+
+ "golang.org/x/image/font"
+ "golang.org/x/image/font/basicfont"
+ "golang.org/x/image/math/fixed"
+)
+
+const padding = 30
+
+type xAndYAxis struct {
+ img *image.RGBA
+ size int
+}
+
+func newXAndYAxis(size int) *xAndYAxis {
+ imageRGBA := image.NewRGBA(image.Rect(0, 0, size+padding, size+padding))
+ return &xAndYAxis{img: imageRGBA, size: size}
+}
+
+func (p *xAndYAxis) drawXandY(s *image.RGBA) *image.RGBA {
+ draw.Draw(p.img, p.img.Bounds(), &image.Uniform{color.White}, image.Point{}, draw.Src)
+ r := image.Rectangle{image.Point{padding, 0}, p.img.Rect.Max}
+ draw.Draw(p.img, r, s, image.Point{0, 0}, draw.Src)
+
+ p.vLine(padding-1, 0, s.Bounds().Max.Y)
+ p.hLine(padding-1, s.Bounds().Max.Y+1, p.img.Bounds().Max.X)
+
+ px := int(p.size / sizesimpix)
+ ds := sizesimpix - 1
+ for cx := 0; cx < sizesimpix; cx++ {
+ p.addTextTo(int(padding/3), int(((cx+1)*px)-(px/3)), fmt.Sprintf("%02d", ds))
+ ds--
+ }
+
+ for cx := 0; cx < sizesimpix; cx++ {
+ p.addTextTo(int(cx*px+padding+(px/4)), int((p.img.Bounds().Max.Y)-padding/2), fmt.Sprintf("%02d", cx))
+ }
+
+ return p.img
+
+}
+
+func (p *xAndYAxis) hLine(x1, y, x2 int) {
+ for ; x1 <= x2; x1++ {
+ p.img.Set(x1, y, color.Gray16{0x0})
+ }
+}
+
+func (p *xAndYAxis) vLine(x, y1, y2 int) {
+ for ; y1 <= y2; y1++ {
+ p.img.Set(x, y1, color.Gray16{0x0})
+ }
+}
+
+func (p *xAndYAxis) addTextTo(x, y int, label string) {
+ d := &font.Drawer{Dst: p.img,
+ Src: image.NewUniform(color.Gray16{0x0}),
+ Face: basicfont.Face7x13,
+ Dot: fixed.Point26_6{X: fixed.I(x), Y: fixed.I(y)},
+ }
+
+ d.DrawString(label)
+}

Snix LLC Git Repository Holder Copyright(C) 2022 All Rights Reserved Email To Snix.IR