Trigonometric problem - help with angles

Hy, guys.

I´m writing some classes to help me with my trigonometric works in flash.
I´he stopped at a question wich is taken my sleep. What I want to do is to take a point “A”, wich is inside a rectangular area. And than, with an angle “n”, calculate an ending point “B”. This end point B is at the border of my rectangular area.

The question is that, deppending on the position of the point “A” and the dimensions of my area, sometimes I have to use the sin and sometimes the cosin of the angle.

But I do not know wich one to use. Because the “A” point is not at the center of a square area. And I do not want to do a “fake” hitTest in a “enter_frame” event handler.

The images bellow help to ilustrate my doubt.

  1. Here, the easy step: the distance based on an angle:

Now the problem A. Here the solution is with the adjacent side of the triangle:

And with this angle, I need to use the opposite side of the triangle:

Is there an Math solution to this, instead of use loopings to calculate and check the last possible position inside the area?

Thanks, guys!

pp