I have this code to generate a red rectangle inside a grey rectangle:
new Rectangle(grey_rectangle_position_x, Game.SCREEN_HEIGHT/2-Rectangle.height/2,0);
This code makes the following:

Now, I want to rotate the red rectangle and randomize his X coordinate but keeping it inside the grey rectangle.
This is my actual code of the rectangle:
public class Rectangle {
public static int width=30;
public static int height=60;
public static Color color=Color.RED;
public Rectangle(int x, int y, int angle) {
super(x, y,angle);
}
public void render(ShapeRenderer shapeRenderer) {
shapeRenderer.begin(ShapeType.Filled);
shapeRenderer.setColor(color);
shapeRenderer.identity();
shapeRenderer.translate(pos_x+width/2, pos_y, 0.f);
shapeRenderer.rotate(0.f, 0.f, 1.f, angle);
shapeRenderer.rect(-width/2, -height/2, width, height);
shapeRenderer.end();
}
}
And this is what I should do to randomize the position of the rectangle:
new Rectangle(randomized_x_position, Game.SCREEN_HEIGHT/2-Rectangle.height/2,0);
The problem is that I don't know how to calculate the randomized_x_position in order to keep the rectangle inside the grey rectangle.
Another problem is that the current Rectangle.render() method is making my rectangle outside the grey rectangle after rotation:

When what I really want is:

