aboutsummaryrefslogtreecommitdiff
path: root/lines/img.go
blob: ec6ab096fd624d4166aef896c505d1f15b9d5cbc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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
}

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