-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcoord_transform.py
More file actions
32 lines (28 loc) · 1016 Bytes
/
Copy pathcoord_transform.py
File metadata and controls
32 lines (28 loc) · 1016 Bytes
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
import numpy as np
def world_to_grid(wx, wy, center_x, center_y, scale, grid_size):
"""
Transforms world coordinates (wx, wy) to grid coordinates (gx, gy).
Args:
wx: World X coordinate
wy: World Y coordinate
center_x: World X coordinate corresponding to the center of the grid
center_y: World Y coordinate corresponding to the center of the grid
scale: Scaling factor (pixels per world unit)
grid_size: Size of the grid (width/height)
Returns:
(gx, gy) tuple of grid coordinates (int)
"""
dx = wx - center_x
dy = wy - center_y
gx = int((dx * scale) + (grid_size / 2))
gy = int((dy * scale) + (grid_size / 2))
return gx, gy
def grid_to_world(gx, gy, center_x, center_y, scale, grid_size):
"""
Transforms grid coordinates (gx, gy) to world coordinates (wx, wy).
"""
dx = (gx - grid_size / 2) / scale
dy = (gy - grid_size / 2) / scale
wx = center_x + dx
wy = center_y + dy
return wx, wy