Class: Pxlsrt::Lines

Inherits:
Object
  • Object
show all
Defined in:
lib/pxlsrt/lines.rb

Overview

“Line” operations used on arrays f colors.

Class Method Summary collapse

Class Method Details

.fromDiagonals(obj, width) ⇒ Object

Uses math to turn an array of diagonals into a linear array.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/pxlsrt/lines.rb', line 135

def self.fromDiagonals(obj, width)
	ell=[]
	for k in obj.keys
		r=k.to_i
		n=r < 0
		if n
			x=0
			y=r.abs
		else
			x=r
			y=0
		end
		ell[x+y*width]=obj[k].first
		for v in 1..(obj[k].length-1)
			x+=1
			y+=1
			ell[x+y*width]=obj[k][v]
		end
	end
	return ell
end

.getDiagonals(array, width, height) ⇒ Object

Uses math to turn an array into an array of diagonals.



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/pxlsrt/lines.rb', line 120

def self.getDiagonals(array, width, height)
	dias={}
	for x in (1-height)..(width-1)
		z=[]
		for y in 0..(height-1)
			if (x+(width+1)*y).between?(width*y, (width*(y+1)-1))
				z.push(array[(x+(width+1)*y)])
			end
		end
		dias[x.to_s]=z
	end
	return dias
end

.handleMiddlate(arr, d) ⇒ Object

Handle middlate requests



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pxlsrt/lines.rb', line 61

def self.handleMiddlate(arr, d)
	n = Pxlsrt::Helpers.isNumeric?(d)
	if n and d.to_i > 0
		k = arr
		for l in 0...(d.to_i)
			k = Pxlsrt::Lines.middlate(k)
		end
		return k
	elsif n and d.to_i < 0
		k = arr
		for l in 0...(d.to_i.abs)
			k = Pxlsrt::Lines.reverseMiddlate(k)
		end
		return k
	elsif d == "" or d == "middle"
		return Pxlsrt::Lines.middlate(arr)
	else
		return arr
	end
end

.imageRGBLines(image, width) ⇒ Object

Gets “rows” of an array based on a width



83
84
85
# File 'lib/pxlsrt/lines.rb', line 83

def self.imageRGBLines(image, width)
	return image.each_slice(width).to_a
end

.middlate(arr) ⇒ Object

Some fancy rearranging.

a, b, c, d, e

-> [d, b, a, c, e]

a, b, c, d

-> [c, a, b, d]



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pxlsrt/lines.rb', line 31

def self.middlate(arr)
	a=[]
	for e in 0...arr.length
		if (arr.length+e) % 2 == 1
			a[0.5*(arr.length+e-1)]=arr[e]
		elsif (arr.length+e) % 2 == 0
			a[0.5*(arr.length-e)-1]=arr[e]
		end
	end
	return a
end

.randomSlices(mainLength, minLength, maxLength) ⇒ Object

Outputs random slices of an array. Because of the requirements of pxlsrt, it doesn’t actually slice the array, but returns a range-like array. Example:

[0, 5], [6, 7], [8, 10]


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
# File 'lib/pxlsrt/lines.rb', line 90

def self.randomSlices(mainLength, minLength, maxLength)
	if mainLength <= 1
		return [[0, 0]]
	else
		min = [minLength, maxLength].min
		max = [minLength, maxLength].max
		min = mainLength if min > mainLength
		max = mainLength if max > mainLength
		min = 1 if min < 1
		max = 1 if max < 1
		nu = [[0, rand(min..max) - 1]]
		last = nu.last.last
		sorting = true
		i = 0
		while sorting do
			if (mainLength - last) <= max
				if last + 1 <= mainLength - 1
					nu.push([last + 1, mainLength - 1])
				end
				sorting = false
			else
				nu.push([last+1, last + rand(min..max)])
			end
			last = nu.last.last
		end
		return nu
	end
end

.reverseMiddlate(arr) ⇒ Object

Some fancy unrearranging.

d, b, a, c, e

-> [a, b, c, d, e]

c, a, b, d

-> [a, b, c, d]



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pxlsrt/lines.rb', line 46

def self.reverseMiddlate(arr)
	a=[]
	for e in 0...arr.length
		if e == ((arr.length/2.0).ceil - 1)
			a[0] = arr[e]
		elsif e < ((arr.length/2.0).ceil - 1)
			a[arr.length - 2*e - 2] = arr[e]
		elsif e > ((arr.length/2.0).ceil - 1)
			a[2*e - arr.length + 1] = arr[e]
		end
	end
	return a
end

.rotateImage(what, width, height, a) ⇒ Object

ChunkyPNG’s rotation was a little slow and doubled runtime. This “rotates” an array, based on the width and height. It uses math and it’s really cool, trust me.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pxlsrt/lines.rb', line 9

def self.rotateImage(what, width, height, a)
	nu=[]
	case a
		when 0, 360, 4
			nu=what
		when 1, 90
			for xy in 0..(what.length-1)
				nu[((height-1)-(xy/width).floor)+(xy % width)*height]=what[xy]
			end
		when 2, 180
			nu=what.reverse
		when 3, 270
			for xy in 0..(what.length-1)
				nu[(xy/width).floor+((width-1)-(xy % width))*height]=what[xy]
			end
	end
	return nu
end