You threw the baseball 40 feet.
This can be represented as two right triangles. The legs of your friend's triangle, the forward distance and distance right, are 15 and 20. Using the Pythagorean theorem, we find the diagonal distance from your friend's starting point:
20²+15²=c²
400+225=c²
625=c²
Take the square root of both sides:
√625=√(c²)
25=c
The legs of your right triangle are 12, your distance forward, and 9, your distance right:
12²+9²=c²
144+81=c²
225=c²
Take the square root of both sides:
√225=√(c²)
15=c
The total diagonal distance is 25+15=40 feet.