-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.cfc
More file actions
248 lines (208 loc) · 8.32 KB
/
Copy pathGrid.cfc
File metadata and controls
248 lines (208 loc) · 8.32 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<cfcomponent output="false">
<cffunction name="init" access="public" returntype="Grid" output="false">
<cfset variables.grid = {} />
<cfset variables.minX = '' />
<cfset variables.maxX = '' />
<cfset variables.minY = '' />
<cfset variables.maxY = '' />
<cfset variables.hasMinMax = false />
<cfreturn this />
</cffunction>
<cffunction name="setValue" access="public" returntype="void" output="false">
<cfargument name="position" type="struct" required="true" />
<cfargument name="value" required="true" />
<cfif !variables.hasMinMax>
<cfset variables.minX = arguments.position.x />
<cfset variables.maxX = arguments.position.x />
<cfset variables.minY = arguments.position.y />
<cfset variables.maxY = arguments.position.y />
<cfset variables.hasMinMax = true />
</cfif>
<cfset variables.minX = Min(variables.minX, arguments.position.x) />
<cfset variables.maxX = Max(variables.maxX, arguments.position.x) />
<cfset variables.minY = Min(variables.minY, arguments.position.y) />
<cfset variables.maxY = Max(variables.maxY, arguments.position.y) />
<cfif !StructKeyExists(variables.grid, arguments.position.y)>
<cfset variables.grid[arguments.position.y] = {} />
</cfif>
<cfset variables.grid[arguments.position.y][arguments.position.x] = arguments.value />
</cffunction>
<cffunction name="getValue" access="public" returntype="any" output="false">
<cfargument name="position" type="struct" required="true" />
<cfargument name="default" type="any" required="false" />
<cfif hasValue(arguments.position)>
<cfreturn variables.grid[arguments.position.y][arguments.position.x] />
</cfif>
<cfif StructKeyExists(arguments, 'default')>
<cfreturn arguments.default />
</cfif>
<cfthrow message="Found no value for position { x: #arguments.position.x#, y: #arguments.position.y# }" />
</cffunction>
<cffunction name="hasValue" access="public" returntype="boolean" output="false">
<cfargument name="position" type="struct" required="true" />
<cfreturn StructKeyExists(variables.grid, arguments.position.y) && StructKeyExists(variables.grid[arguments.position.y], arguments.position.x) />
</cffunction>
<cffunction name="countValues" access="public" returntype="numeric" output="false">
<cfargument name="value" type="any" required="true" />
<cfset var num = 0 />
<cfset var x = '' />
<cfset var y = '' />
<cfloop collection="#variables.grid#" item="y">
<cfloop collection="#variables.grid[y]#" item="x">
<cfif variables.grid[y][x] eq arguments.value>
<cfset num++ />
</cfif>
</cfloop>
</cfloop>
<cfreturn num />
</cffunction>
<cffunction name="getMinX" access="public" returntype="numeric" output="false">
<cfreturn variables.minX />
</cffunction>
<cffunction name="getMaxX" access="public" returntype="numeric" output="false">
<cfreturn variables.maxX />
</cffunction>
<cffunction name="getMinY" access="public" returntype="numeric" output="false">
<cfreturn variables.minY />
</cffunction>
<cffunction name="getMaxY" access="public" returntype="numeric" output="false">
<cfreturn variables.maxY />
</cffunction>
<cffunction name="getSlice" access="public" returntype="Grid" output="false">
<cfargument name="position1" type="struct" required="true" />
<cfargument name="position2" type="struct" required="true" />
<cfset var gridSlice = CreateObject('component', 'Grid').init() />
<cfset var x = '' />
<cfset var y = '' />
<cfset var position = '' />
<cfloop from="#Min(arguments.position1.x, arguments.position2.x)#" to="#Max(arguments.position1.x, arguments.position2.x)#" index="x">
<cfloop from="#Min(arguments.position1.y, arguments.position2.y)#" to="#Max(arguments.position1.y, arguments.position2.y)#" index="y">
<cfset position = { x = x, y = y } />
<cfset gridSlice.setValue(position, getValue(position)) />
</cfloop>
</cfloop>
<cfreturn gridSlice />
</cffunction>
<cffunction name="putSlice" access="public" returntype="void" output="false">
<cfargument name="topLeftPosition" type="struct" required="true" />
<cfargument name="gridSlice" type="Grid" required="true" />
<cfset var x = '' />
<cfset var y = '' />
<cfset var fromSlicePosition = '' />
<cfset var toGridPosition = '' />
<cfloop from="#arguments.gridSlice.getMinY()#" to="#arguments.gridSlice.getMaxY()#" index="y">
<cfloop from="#arguments.gridSlice.getMinX()#" to="#arguments.gridSlice.getMaxX()#" index="x">
<cfset fromSlicePosition = { x = x, y = y } />
<cfset toGridPosition = { x = arguments.topLeftPosition.x + x - arguments.gridSlice.getMinX() , y = arguments.topLeftPosition.y + y - arguments.gridSlice.getMinY() } />
<cfset setValue(toGridPosition, arguments.gridSlice.getValue(fromSlicePosition)) />
</cfloop>
</cfloop>
</cffunction>
<cffunction name="getTransposedGrid" access="public" returntype="Grid" output="false">
<cfset var transposedGrid = CreateObject('component', 'Grid').init() />
<cfset var x = '' />
<cfset var y = '' />
<cfloop from="#getMinY()#" to="#getMaxY()#" index="y">
<cfloop from="#getMinX()#" to="#getMaxX()#" index="x">
<cfset transposedGrid.setValue({ x = y, y = x }, getValue({ x = x, y = y })) />
</cfloop>
</cfloop>
<cfreturn transposedGrid />
</cffunction>
<cffunction name="getFlippedGrid" access="public" returntype="Grid" output="false">
<cfargument name="axisToFlipAround" type="string" required="true" />
<cfset var flippedGrid = CreateObject('component', 'Grid').init() />
<cfset var x = '' />
<cfset var y = '' />
<cfset var newX = '' />
<cfset var newY = '' />
<cfloop from="#getMinY()#" to="#getMaxY()#" index="y">
<cfloop from="#getMinX()#" to="#getMaxX()#" index="x">
<cfswitch expression="#arguments.axisToFlipAround#">
<cfcase value="y">
<cfset newX = getMaxX() - (x - getMinX()) />
<cfset newY = y />
</cfcase>
<cfcase value="x">
<cfset newX = x />
<cfset newY = getMaxY() - (y - getMinY()) />
</cfcase>
<cfdefaultcase>
<cfthrow message="Unexpected" />
</cfdefaultcase>
</cfswitch>
<cfset flippedGrid.setValue({ x = newX, y = newY }, getValue({ x = x, y = y })) />
</cfloop>
</cfloop>
<cfreturn flippedGrid />
</cffunction>
<cffunction name="getClockwiseRotatedGrid" access="public" returntype="Grid" output="false">
<cfreturn this.getTransposedGrid().getFlippedGrid('y') />
</cffunction>
<cffunction name="move" access="public" returntype="struct" output="false">
<cfargument name="position" type="struct" required="true" />
<cfargument name="direction" type="string" required="true" />
<cfset var newPosition = StructCopy(arguments.position) />
<cfswitch expression="#arguments.direction#">
<cfcase value="UP">
<cfset newPosition.y-- />
</cfcase>
<cfcase value="LEFT">
<cfset newPosition.x-- />
</cfcase>
<cfcase value="DOWN">
<cfset newPosition.y++ />
</cfcase>
<cfcase value="RIGHT">
<cfset newPosition.x++ />
</cfcase>
<cfdefaultcase>
<cfthrow message="Unexpected direction: #arguments.direction#" />
</cfdefaultcase>
</cfswitch>
<cfreturn newPosition />
</cffunction>
<cffunction name="turnLeft" access="public" returntype="string" output="false">
<cfargument name="direction" type="string" required="true" />
<cfswitch expression="#arguments.direction#">
<cfcase value="UP">
<cfreturn 'LEFT' />
</cfcase>
<cfcase value="LEFT">
<cfreturn 'DOWN' />
</cfcase>
<cfcase value="DOWN">
<cfreturn 'RIGHT' />
</cfcase>
<cfcase value="RIGHT">
<cfreturn 'UP' />
</cfcase>
<cfdefaultcase>
<cfthrow message="Unexpected direction: #arguments.direction#" />
</cfdefaultcase>
</cfswitch>
</cffunction>
<cffunction name="turnRight" access="public" returntype="string" output="false">
<cfargument name="direction" type="string" required="true" />
<cfswitch expression="#arguments.direction#">
<cfcase value="UP">
<cfreturn 'RIGHT' />
</cfcase>
<cfcase value="LEFT">
<cfreturn 'UP' />
</cfcase>
<cfcase value="DOWN">
<cfreturn 'LEFT' />
</cfcase>
<cfcase value="RIGHT">
<cfreturn 'DOWN' />
</cfcase>
<cfdefaultcase>
<cfthrow message="Unexpected direction: #arguments.direction#" />
</cfdefaultcase>
</cfswitch>
</cffunction>
<cffunction name="getStruct" access="public" returntype="struct" output="false">
<cfreturn variables.grid />
</cffunction>
</cfcomponent>