Distance to rect edge from point based on direction

I have a rect that is at position (0,0) at the top left and is 2200x2200 in size.

I am trying to specify the the distance to get to the edge of the rect in specific direction (degrees) from a point within the rect.

So say I am at point 50,50, and want to move 270 degrees until I hit the coordinate of the edge of the rect. What would be the best way to go about this?

Right now I have another function I use to get distance based on direction

distance = 2200
direction = 315
toEdge = distance*math.cos(math.radians(direction)), distance*math.sin(math.radians(direction))


This will just get a point 2200 units away from direction. I think this is half the step there, but can't get the math right on on how I translate this into getting the distance to the rect edge depending on coordinate.

If anyone could help me out I would be appreciative. Thanks.

1 Answer

If I understood what you're asking ,after you get your toEdge point , you need Intersection of a Line and a Rectangle to get intersection point. Then calculte distance from start point to intersection point.