Beam direction optimization is an important problem in radiation therapy. In intensity modulated radiation therapy (IMRT), the difficulty for computer optimization of the beam directions arises from the fact that they are coupled with the intensity profiles of the incident beams. In order to obtain the optimal incident beam directions using iterative or stochastic methods, the beam profiles ought to be optimized after every change of beam configuration. In this paper we report an effective algorithm to optimize gantry angles for IMRT. In our calculation the gantry angles and the beam profiles (beamlet weights) were treated as two separate groups of variables. The gantry angles were sampled according to a simulated annealing algorithm. For each sampled beam configuration, beam profile calculation was done using a fast filtered backprojection (FBP) method. Simulated annealing was also used for beam profile optimization to examine the performance of the FBP for beam orientation optimization. Relative importance factors were incorporated into the objective function to control the relative importance of the target and the sensitive structures. Minimization of the objective function resulted in the best possible beam orientations and beam profiles judged by the given objective function. The algorithm was applied to several model problems and the results showed that the approach has potential for IMRT applications.