This is not the solution to your problem, however it might be useful. I always use this class, because I am lazy to rewrite the code times and times again.
public static class SimpleRectFloat {
public float x1, y1, x2, y2;
public SimpleRectFloat(float x1, float y1, float x2, float y2){
this.set(x1, y1, x2, y2);
}
final static SimpleRectFloat fromCenter(float cx, float cy, float sx, float sy){
return new SimpleRectFloat(cx - sx * 0.5f, cy - sy * 0.5f, cx + sx * 0.5f, cy + sy * 0.5f);
}
final public SimpleRectFloat set(float x1, float y1, float x2, float y2){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
return this;
}
final public SimpleRectFloat set(SimpleRectFloat rect){
if(rect == null) return this;
this.x1 = rect.x1;
this.y1 = rect.y1;
this.x2 = rect.x2;
this.y2 = rect.y2;
return this;
}
final public SimpleRectFloat setCentered(float x, float y, float w, float h){
return this.set(x - w * 0.5f, y - h * 0.5f, x + w * 0.5f, y + h * 0.5f);
}
final public SimpleRectFloat setCenter(float x, float y){
return this.setCentered(x, y, this.getWidth(), this.getHeight());
}
final public SimpleRectFloat setSizeFromCenter(float w, float h){
return this.setCentered(this.getCenterX(), this.getCenterY(), w, h);
}
final public SimpleRectFloat copy(){
return new SimpleRectFloat(this.x1, this.y1, this.x2, this.y2);
}
final public float getLeft(){
return (float)Math.min(this.x1, this.x2);
}
final public float getRight(){
return (float)Math.max(this.x1, this.x2);
}
final public float getTop(){
return (float)Math.min(this.y1, this.y2);
}
final public float getBottom(){
return (float)Math.max(this.y1, this.y2);
}
final public float getWidth(){
return Math.abs(this.x1 - this.x2);
}
final public float getHeight(){
return Math.abs(this.y1 - this.y2);
}
final public PVector getSize(){
return new PVector(this.getWidth(), this.getHeight());
}
final public float getCenterX(){
return (this.x1 + this.x2) * 0.5f;
}
final public float getCenterY(){
return (this.y1 + this.y2) * 0.5f;
}
final public PVector getCenter(){
return new PVector(this.getCenterX(), this.getCenterY());
}
final public PVector map(float x, float y, SimpleRectFloat rect){
if(rect == null) throw new NullPointerException();
return new PVector(
this.getLeft() + (x - rect.getLeft()) / rect.getWidth() * this.getWidth(),
this.getTop() + (y - rect.getTop()) / rect.getHeight() * this.getHeight()
);
}
final public PVector map(PVector p, SimpleRectFloat rect){
if(p == null || rect == null) throw new NullPointerException();
return new PVector(
this.getLeft() + (p.x - rect.getLeft()) / rect.getWidth() * this.getWidth(),
this.getTop() + (p.y - rect.getTop()) / rect.getHeight() * this.getHeight()
);
}
final public boolean isPointInside(float x, float y){
return x >= this.getLeft() && x <= this.getRight() && y >= this.getTop() &&y <= this.getBottom();
}
final public boolean isPointInside(PVector p){
if(p == null) return false;
return this.isPointInside((float)p.x, (float)p.y);
}
final public SimpleRectFloat scaleCenter(float scale){
float cx = this.getCenterX();
float cy = this.getCenterY();
float sx = this.getWidth() * 0.5f * scale;
float sy = this.getHeight() * 0.5f * scale;
return this.set(cx - sx, cy - sy, cx + sx, cy + sy);
}
final public SimpleRectFloat createToFitAtCenter(float width, float height, float scale){
if(width < 0) width = - width;
if(height < 0) height = - height;
float upscale = (float)Math.min(this.getWidth() / width, this.getHeight() / height);
return SimpleRectFloat.fromCenter(this.getCenterX(), this.getCenterY(), width, height).scaleCenter(upscale * scale);
}
final public SimpleRectFloat fitInside(SimpleRectFloat rect, float scale){
if(rect == null) return this;
float upscale = (float)Math.min(rect.getWidth() / this.getWidth(), rect.getHeight() / this.getHeight());
return this.setCenter(rect.getCenterX(), rect.getCenterY()).scaleCenter(upscale * scale);
}
@Override final public String toString(){
return "[" + this.x1 + ", " + this.y1 + ", " + this.x2 + ", " + this.y2 + "]";
}
}
And here is an example code that maybe resembles yours (though I’m not confident it does).
SimpleRectFloat rect;
PImage image;
float zoomLevel = 1;
void setup(){
size(640, 480);
image = loadImage("path");
rect = SimpleRectFloat.createToFitAtCenter(image.width, image.height, 1);
}
void draw(){
background(23);
//Snap rect to center set size to image size * zoomLevel
rect.setCenter(width * 0.5, height * 0.5).setSizeFromCenter(image.width * zoomLevel, image.height * zoomLevel);
//Draw image
imageMode(CORNERS);
image(image, rect.x1, rect.y1, rect.x2, rect.y2);
imageMode(CENTER);
image(image, rect.getCenterX(), rect.getCenterY(), rect.getWidth(), rect.getHeight());
imageMode(CORNER);
image(image, rect.getLeft(), rect.getTop(), rect.getWidth(), rect.getHeight());
}
void mouseWheel(MouseEvent e){
if(e.getCount() > 0) zoomLevel *= 1.25;
else zoomLevel *= 0.8;
}