Know the limits of an image after rotation I have an image which I know the number of rows and the number of columns. I would like to calculate the limits of the new image after a rotation.

   BufferedImage myImage = ImageIO.read( new File( "D:\\Users...jpg" ) );
    xmaxOrigine = myImage.getWidth() - 1;
    ymaxOrigine = myImage.getHeight() - 1;
    angle = 12;

    angleRadian = Math.toRadians( angle );
    cos = Math.cos( angleRadian );
    sin = Math.sin( angleRadian );

    p1X = (int) ( ( xmaxOrigine * cos ) - ( ymaxOrigine * sin ) );
    p1Y = (int) ( ( xmaxOrigine * sin ) + ( ymaxOrigine * cos ) );

    xmin_f = xmin_f &lt; p1X ? xmin_f : p1X;
    xmax_f = xmax_f &lt; p1X ? p1X : xmax_f;
    ymin_f = ymin_f &lt; p1Y ? ymin_f : p1Y;
    ymax_f = ymax_f &lt; p1Y ? p1Y : ymax_f;

    p2X = (int) ( ( 0  * cos ) - ( ymaxOrigine * sin ) );
    p2Y = (int) ( ( 0 * sin ) + ( ymaxOrigine * cos ) );

    xmin_f = xmin_f &lt; p2X ? xmin_f : p2X;
    xmax_f = xmax_f &lt; p2X ? p2X : xmax_f;
    ymin_f = ymin_f &lt; p2Y ? ymin_f : p2Y;
    ymax_f = ymax_f &lt; p2Y ? p2Y : ymax_f;

    p3X = (int) ( ( xmaxOrigine * cos ) - ( 0 * sin ) );
    p3Y = (int) ( ( xmaxOrigine * sin ) + ( 0 * cos ) );

    xmin_f = xmin_f &lt; p3X ? xmin_f : p3X;
    xmax_f = xmax_f &lt; p3X ? p3X : xmax_f;
    ymin_f = ymin_f &lt; p3Y ? ymin_f : p3Y;
    ymax_f = ymax_f &lt; p3Y ? p3Y : ymax_f;

    p4X = 0;
    p4Y = 0;

    xmin_f = xmin_f &lt; p4X ? xmin_f : p4X;
    xmax_f = xmax_f &lt; p4X ? p4X : xmax_f;
    ymin_f = ymin_f &lt; p4Y ? ymin_f : p4Y;
    ymax_f = ymax_f &lt; p4Y ? p4Y : ymax_f;

    widthFinal = xmax_f - xmin_f;
    heightFinal = ymax_f - ymin_f;


As you can see I look for the xmin, xmax, ymin, ymax for each points. I would like to make this operation once if it is possible. Also, how can I condense this code?
xmaxOrigine



So this looks like you are mixing a French word with English.  Be careful of that.  A native English reader will be mildly confused by this, as it looks similar to original which would make sense in that context.  But the real problem is with someone for whom origine does not look like a word.  For example, take native Mandarin speakers.  Presumably they know how to look up English words that they don't understand in the English code.  They won't however know that this is not an English word.  So they won't find it in an English directory and won't know that they should look in a French dictionary.  

Perhaps your context will have you overwhelmingly sharing this code with people who speak enough French to know what you mean.  But if you do need to share with someone who doesn't speak French, that person may be confused by things like this.  


    xmin_f = xmin_f &lt; p1X ? xmin_f : p1X;
    xmax_f = xmax_f &lt; p1X ? p1X : xmax_f;
    ymin_f = ymin_f &lt; p1Y ? ymin_f : p1Y;
    ymax_f = ymax_f &lt; p1Y ? p1Y : ymax_f;



This would be easier to read if you used the natural language constructs for this.  

    xmin_f = Math.min(xmin_f, p1X);
    xmax_f = Math.max(p1X, xmax_f);
    ymin_f = Math.min(ymin_f, p1Y);
    ymax_f = Math.max(p1Y, ymax_f);


You also do this repeatedly.  If these are object fields, you can make a method to do this for you.  

private void expandBoundaries(int x, int y) {
    xmin_f = Math.min(xmin_f, x);
    xmax_f = Math.min(xmax_f, x);
    ymin_f = Math.min(ymin_f, y);
    ymax_f = Math.min(ymax_f, y);
}


If they are not object fields, you can make objects to track them.  Then you'd use them like 

    xRange.include(p1X);
    yRange.include(p1Y);


and at the end 

    widthFinal = xRange.getMax() - xRange.getMin();
    heightFinal = yRange.getMax() - yRange.getMin();


or even 

    widthFinal = xRange.getDifference();
    heightFinal = yRange.getDifference();


with include defined as 

public void include(T value) {
    min = Math.min(min, value);
    max = Math.max(max, value);
}


You can also save some multiplications by storing the values for later use:

    float rotatedX2X = xmaxOrigine * cos;
    float rotatedY2X = ymaxOrigine * sin;
    float rotatedX2Y = xmaxOrigine * sin;
    float rotatedY2Y = ymaxOrigine * cos;


Then you could rewrite this code as 

    BufferedImage myImage = ImageIO.read( new File( "D:\\Users...jpg" ) );
    xmaxOrigine = myImage.getWidth() - 1;
    ymaxOrigine = myImage.getHeight() - 1;

    angle = 12;
    angleRadian = Math.toRadians( angle );
    cos = Math.cos( angleRadian );
    sin = Math.sin( angleRadian );

    float rotatedX2X = xmaxOrigine * cos;
    float rotatedY2X = ymaxOrigine * sin;
    float rotatedX2Y = xmaxOrigine * sin;
    float rotatedY2Y = ymaxOrigine * cos;

    xRange.include((int) (rotatedX2X - rotatedY2X));
    yRange.include((int) (rotatedX2Y + rotatedY2Y));
    xRange.include((int)-rotatedY2X);
    yRange.include((int)rotatedY2Y);
    xRange.include((int)rotatedX2X);
    yRange.include((int)rotatedX2Y);
    xRange.include(0);
    yRange.include(0);

    widthFinal = xRange.getDifference();
    heightFinal = yRange.getDifference();


Note that this gets rid of p1X, p1Y, etc.  They weren't used in the original code snippet.  It's possible that they were used elsewhere.  If so, you'd have to put them back.  

Then your Range class:

public class Range&lt;T extends Number&gt; {
    private T max;
    private T min;

    public Range(T min, T max) {
        this.min = min;
        this.max = max;
    }

    public void include(T value) {
        min = Math.min(min, value);
        max = Math.max(max, value);
    }

    public T getMin() {
        return min;  
    }

    public T getMax() {
        return max;  
    }

    public T getDifference() {
        return max - min;
    }
}


Note that you have to initialize the Range before using it.  You don't show the initialization in the original code snippet, so I didn't show it here.  It would look something like 

    Range xRange = new Range(small, large);