Class: Pxlsrt::Spiral

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

Overview

Spiral iteration.

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ Spiral

Returns a new instance of Spiral.



5
6
7
8
9
10
11
12
13
# File 'lib/pxlsrt/spiral.rb', line 5

def initialize(x, y)
	@x = x
	@y = y
	@direction = "up"
	@step = 1
	@at = 0
	@count = 0
	@cycles = -1
end

Instance Method Details

#countObject

Return amount iterated.



31
32
33
# File 'lib/pxlsrt/spiral.rb', line 31

def count
	return @count
end

#cyclesObject

Return cycles gone through completely.



36
37
38
# File 'lib/pxlsrt/spiral.rb', line 36

def cycles
	return @cycles
end

#directionObject

Return current direction.



26
27
28
# File 'lib/pxlsrt/spiral.rb', line 26

def direction
	return @direction
end

#nextObject

Goes to next position. Returns position.



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

def next
	case @direction
	when "left"
		@x -= 1
		@at += 1
		if @at == @step
			@direction = "down"
			@at = 0
			@step += 1
		end
	when "down"
		@y += 1
		@at += 1
		if @at == @step
			@direction = "right"
			@at = 0
		end
	when "right"
		@x += 1
		@at += 1
		if @at == @step
			@direction = "up"
			@at = 0
			@step += 1
		end
	when "up"
		@cycles += 1 if @at == 0
		@y -= 1
		@at += 1
		if @at == @step
			@direction = "left"
			@at = 0
		end
	end
	@count += 1
	return pos
end

#posObject

Return current position.



41
42
43
# File 'lib/pxlsrt/spiral.rb', line 41

def pos
	return {:x => @x, :y => @y}
end

#xObject

Return current x value.



16
17
18
# File 'lib/pxlsrt/spiral.rb', line 16

def x
	return @x
end

#yObject

Return current y value.



21
22
23
# File 'lib/pxlsrt/spiral.rb', line 21

def y
	return @y
end