Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Approximate directions solution in Clear category for Find Enemy by V.Shkaberda
from math import atan2, pi
ROT = {'N':0, 'NW':1, 'SW':2, 'S':3, 'SE':4, 'NE':5}
def find_dist(x, y):
''' Metrics
'''
(x1, y1), (x2, y2) = x, y
return abs(x2 - x1) + abs(y2 - y1) - min(abs(y2 - y1), (abs(x2 - x1) + (x1 % 2 == y1 > y2))//2)
def find_dir(x, y, dir):
''' Calculate angle in radians and rotate it basing on relative direction.
(0 + corr) of the rot_angle points to the right edge of the forward direction
'''
(x1, y1), (x2, y2) = x, y
dx, dy = (x2 - x1, y1 - y2)
angle = atan2(dy, dx) # returns [+pi; -pi]
rot_angle = (angle - pi / 6 * (13 + 2 * ROT[dir])) % (2 * pi)
# correction is needed cause the center of the "direction axis" isn't the center of coordinate
corr = pi / (2 * (find_dist(x, y) + 4))
return ('R' if rot_angle > 5 * pi / 3 - corr else
'B' if rot_angle > pi + corr else
'L' if rot_angle > 2 * pi / 3 - corr else
'F' if rot_angle > corr else 'R')
def find_enemy(you, dir, enemy):
x, y = ((ord(coo[0]), int(coo[1:])) for coo in (you, enemy))
return [find_dir(x, y, dir), find_dist(x, y)]
May 9, 2019
Comments: