Start by calculating the length of the line from the origin to the edge:

For theta between -45 and +45 degrees h= n/(2 cos(theta)),

For theta between 45 and 135 degrees h = n/(2 sin(theta))

For theta between 135 and 225 degrees h = -n/(2 cos(theta)),

For theta between 225 and 315 degrees h = -n/(2 sin(theta))

The distance from the origin to (x,y) is sqrt(x^2+y^2). Hence the length from (x,y) to the edge is h-sqrt(x^2+y^2).