Alright, I'm not 100% sure I'm understanding this correctly. You say that p can be located on the right or top line and that p2 can be located on the bottom or left line. Do you mean the rectangle can be rotated? If that's the case, the question should say that p can be on the right or bottom line of the rectangle. Also, are you looking for two separate answers or one that works both when p2 is on the bottom and on the left?
If you do mean that the rectangle can be rotated, and want two different answers, it's pretty simple. First I'll deal with when p2 is on the bottom and p is on the right.
Since p2 is on the bottom line we know the y-coordinate is h, according to the diagram. We also know that p is (0,y). Because of the 45 degree angle, we know that the distance between p's y-coordinate and the lower right corner is the same as the distance between the lower right corner and p2's x-coordinate, which in this case is p2's x-coordinate. Therefore, the coordinates of p2 are (h-y, h).
If p2 is on the left and p is on the bottom, it's very similar. Since p2 is on the left, it's x-coordinate is h. Because p is on the x-axis, it's (x,0). Because of the 45 degree angle, the distance between the lower left corner and p is the same as the distance between the lower left corner and p2, which this time gives us p2's y-coordinate. Therefore the coordinates of p2 are (h,h-x).
Hopefully I understood your intentions correctly. If not, I hope you can use my misunderstandings to further improve your question.