Q:

Calculate angle of line with negative slope

I'm creating a map, and calculating the angle of the lines to determine the orientation of a road (NSEW).
Below is the formula I'm using to calculate the angle of a line:
Math.tan(slope) / (Math::PI/180)
This works until the slope of my line is negative. How can I properly calculate the angle of a line with negative slope?
Update To calculate the angle of the line, I am now using
Math.atan2(delta_y, delta_x) / (Math::PI/180)

The issue seems to stem from which points I select the line starts at/ends at. Depending which one I take, some are correct, some aren't, and vise versa. Here's an image of the map I create:

I'm only trying to calculate the angle of lines that intersect with the bounding box. One of the points is always where the line intersects with the bounding box, the other point is always just after that.
My origin (0,0) is at the top left.

A:

I was able to solve this by doing the following. Keep in mind this is an inverted axis where the origin (0, 0) is in the top left.
def angle(point1, point2)
  return 90 if vertical?(point1, point2)
  return 0 if horizontal?(point1, point2)
  point1, point2 = [point1, point2].sort_by(&:x)
  delta_x = point1.x - point2.x
  delta_y = point1.y - point2.y

  if point1.y > point2.y # inverted axis/origin
    Geometry.to_degrees(Math.atan(delta_y / delta_x)).abs
  else
    # Add 90 degrees when angle is in 3rd quadrant
    Geometry.to_degrees(Math.atan(delta_x / delta_y)).abs + 90
  end
end

