-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatilesmap.m
167 lines (141 loc) · 3.77 KB
/
catilesmap.m
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
classdef catilesmap < handle
%%
properties(SetAccess = private, GetAccess = private)
tiles_h; % Number of rows of tiles
tiles_w; % Number of column of tiles
current_h; % row position of current tile in the map
current_w; % row position of current tile in the map
end
%%
methods(Access = public)
%%
function obj = catilesmap(rows,cols)
%%
%
%
%%
assert(numel(rows) == 1 && numel(cols) == 1);
obj.tiles_h = rows;
obj.tiles_w = cols;
obj.current_h = 1;
obj.current_w = 1;
end
%%
function reinit(obj)
%%
%
%
%%
obj.current_h = 1;
obj.current_w = 1;
end
%%
function [row,col] = currenttile(obj)
row = obj.current_h;
col = obj.current_w;
end
%%
function [row,col] = centraltile(obj)
%%
%
%
%%
row = fix(obj.tiles_h /2);
col = fix(obj.tiles_w /2);
end
%%
function [row,col] = south(obj)
%%
%
%
%%
if obj.current_h == obj.tiles_h
row = 0;
col = 0;
else
row = obj.current_h + 1;
col = obj.current_w;
end
end
function [row,col] = north(obj)
%%
%
%
%%
if obj.current_h == obj.tiles_h
row = 0;
col = 0;
else
row = obj.current_h + 1;
col = obj.current_w;
end
end
%%
function [row,col] = east(obj)
%%
%
%
%%
if obj.current_w == obj.tiles_w
row = 0;
col = 0;
else
row = obj.current_h;
col = obj.current_w + 1;
end
end
%%
function [row,col] = west(obj)
%%
%
%
%%
row = obj.current_h;
col = obj.current_w - 1;
end
%%
function [row, col] = next(obj)
%%
%
%
%%
row = obj.current_h;
col = obj.current_w;
if row == 1 || col == obj.tiles_w
row = col + row;
col = 1;
if row > obj.tiles_h
e = row - obj.tiles_h;
row = row - e;
col = col + e;
end
else
row = row - 1;
col = col + 1;
end
if row == obj.tiles_h && col == obj.tiles_w
row = 1;
col = 1;
end
obj.current_h = row;
obj.current_w = col;
end
%%
function lindex = twosub2lindex(obj, row, col)
lindex = (col -1) * obj.tiles_h + row;
end
%%
function [row, col] = lindex2twosub(obj, lindex)
row = rem(lindex, obj.tiles_h);
col = floor(lindex/obj.tiles_h) + 1;
if row == 0
row = obj.tiles_h;
col = col - 1;
end
end
%%
function nbr = tilesnumber(obj)
nbr = obj.tiles_h * obj.tiles_w;
end
end
end