Skip to content

Commit 81aecdb

Browse files
committed
Updated the grid label text to be on the outside of the grid. Makes the scene inside less cluttered around where objects will be
1 parent 6f36d52 commit 81aecdb

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

graphics/graphics_grid.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def __init_grid(self):
5858

5959
# Update the labels instead of recreating them
6060
update_grid_numbers(self.__focal_point, self.grid_object[self.__labels_idx],
61-
self.__num_squares, self.__scale, self.__scene)
61+
self.__num_squares, self.__scale, self.__is_3d, self.__scene)
6262

6363
def __create_grid_objects(self):
6464
"""
@@ -276,6 +276,7 @@ def update_grid(self):
276276
self.grid_object[self.__labels_idx],
277277
self.__num_squares,
278278
self.__scale,
279+
self.__is_3d,
279280
self.__scene)
280281

281282
def toggle_2d_3d(self):

graphics/graphics_text.py

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def draw_text(label_text, label_position, scene):
102102
return the_label
103103

104104

105-
def update_grid_numbers(focal_point, numbers_list, num_squares, scale, scene):
105+
def update_grid_numbers(focal_point, numbers_list, num_squares, scale, is_3d, scene):
106106
"""
107107
Draw the grid numbers along the xyz axes.
108108
@@ -114,6 +114,8 @@ def update_grid_numbers(focal_point, numbers_list, num_squares, scale, scene):
114114
:type num_squares: `int`
115115
:param scale: The scaled length of 1 square unit
116116
:type scale: `float`
117+
:param is_3d: Whether the grid is 3D or not
118+
:type is_3d: `bool`
117119
:param scene: The scene in which to draw the object
118120
:type scene: class:`vpython.canvas`
119121
"""
@@ -160,7 +162,13 @@ def update_grid_numbers(focal_point, numbers_list, num_squares, scale, scene):
160162
for x_pos in x_coords:
161163
# Draw the corresponding unit number at each x coordinate
162164
txt = "{:.2f}".format(x_pos)
163-
pos = vector(x_pos + padding, y_origin + padding, z_origin)
165+
if is_3d:
166+
if (sign(camera_axes.y) * -1) > 0:
167+
pos = vector(x_pos, max_y_coord + padding, z_origin)
168+
else:
169+
pos = vector(x_pos, min_y_coord - padding, z_origin)
170+
else:
171+
pos = vector(x_pos, y_origin - padding, z_origin)
164172
if append:
165173
numbers_list.append(draw_text(txt, pos, scene))
166174
numbers_list[len(numbers_list)-1].height = get_text_size(scene)
@@ -169,13 +177,16 @@ def update_grid_numbers(focal_point, numbers_list, num_squares, scale, scene):
169177
numbers_list[index].pos = pos
170178
numbers_list[index].height = get_text_size(scene)
171179
index += 1
172-
# Draw the axis label at either the positive or negative side away from center
173-
# If sign = -1, draw off max side, if sign = 0 or 1, draw off negative side
180+
# Draw the axis label at the centre of the axes numbers
174181
txt = "X"
175-
if (sign(camera_axes.x) * -1) > 0:
176-
pos = vector(max_x_coord + 1, y_origin, z_origin)
182+
if (sign(camera_axes.y) * -1) > 0:
183+
x = (max_x_coord + min_x_coord) / 2
184+
y = max_y_coord + scale * 2
185+
pos = vector(x, y, z_origin)
177186
else:
178-
pos = vector(min_x_coord - 1, y_origin, z_origin)
187+
x = (max_x_coord + min_x_coord) / 2
188+
y = min_y_coord - scale * 2
189+
pos = vector(x, y, z_origin)
179190
if append:
180191
numbers_list.append(draw_text(txt, pos, scene))
181192
numbers_list[len(numbers_list) - 1].height = get_text_size(scene)
@@ -189,7 +200,13 @@ def update_grid_numbers(focal_point, numbers_list, num_squares, scale, scene):
189200
for y_pos in y_coords:
190201
# Draw the corresponding unit number at each x coordinate
191202
txt = "{:.2f}".format(y_pos)
192-
pos = vector(x_origin, y_pos + padding, z_origin + padding)
203+
if is_3d:
204+
if (sign(camera_axes.x) * -1) > 0:
205+
pos = vector(max_x_coord + padding, y_pos, z_origin)
206+
else:
207+
pos = vector(min_x_coord - padding, y_pos, z_origin)
208+
else:
209+
pos = vector(x_origin - padding, y_pos, z_origin)
193210
if append:
194211
numbers_list.append(draw_text(txt, pos, scene))
195212
numbers_list[len(numbers_list) - 1].height = get_text_size(scene)
@@ -198,13 +215,17 @@ def update_grid_numbers(focal_point, numbers_list, num_squares, scale, scene):
198215
numbers_list[index].pos = pos
199216
numbers_list[index].height = get_text_size(scene)
200217
index += 1
201-
# Draw the axis label at either the positive or negative side away from center
202-
# If sign = -1, draw off max side, if sign = 0 or 1, draw off negative side
218+
# Draw the axis label at the centre of the axes numbers
203219
txt = "Y"
204-
if (sign(camera_axes.y) * -1) > 0:
205-
pos = vector(x_origin, max_y_coord + 1, z_origin)
220+
if (sign(camera_axes.x) * -1) > 0:
221+
x = max_x_coord + scale * 2
222+
y = (max_y_coord + min_y_coord) / 2
223+
pos = vector(x, y, z_origin)
206224
else:
207-
pos = vector(x_origin, min_y_coord - 1, z_origin)
225+
x = min_x_coord - scale * 2
226+
y = (max_y_coord + min_y_coord) / 2
227+
pos = vector(x, y, z_origin)
228+
208229
if append:
209230
numbers_list.append(draw_text(txt, pos, scene))
210231
numbers_list[len(numbers_list) - 1].height = get_text_size(scene)
@@ -218,7 +239,10 @@ def update_grid_numbers(focal_point, numbers_list, num_squares, scale, scene):
218239
for z_pos in z_coords:
219240
# Draw the corresponding unit number at each x coordinate
220241
txt = "{:.2f}".format(z_pos)
221-
pos = vector(x_origin, y_origin - padding, z_pos + padding)
242+
if (sign(camera_axes.x) * -1) > 0:
243+
pos = vector(max_x_coord + padding, y_origin, z_pos)
244+
else:
245+
pos = vector(min_x_coord - padding, y_origin, z_pos)
222246
if append:
223247
numbers_list.append(draw_text(txt, pos, scene))
224248
numbers_list[len(numbers_list) - 1].height = get_text_size(scene)
@@ -230,10 +254,10 @@ def update_grid_numbers(focal_point, numbers_list, num_squares, scale, scene):
230254
# Draw the axis label at either the positive or negative side away from center
231255
# If sign = -1, draw off max side, if sign = 0 or 1, draw off negative side
232256
txt = "Z"
233-
if (sign(camera_axes.z) * -1) > 0:
234-
pos = vector(x_origin, y_origin, max_z_coord + 1)
257+
if (sign(camera_axes.x) * -1) > 0:
258+
pos = vector(max_x_coord + scale * 2, y_origin, (max_z_coord + min_z_coord) / 2)
235259
else:
236-
pos = vector(x_origin, y_origin, min_z_coord - 1)
260+
pos = vector(min_x_coord - scale * 2, y_origin, (max_z_coord + min_z_coord) / 2)
237261
if append:
238262
numbers_list.append(draw_text(txt, pos, scene))
239263
numbers_list[len(numbers_list) - 1].height = get_text_size(scene)

0 commit comments

Comments
 (0)