Skip to content

Instantly share code, notes, and snippets.

@internetsadboy
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save internetsadboy/7febc5c6aa968cb05f62 to your computer and use it in GitHub Desktop.

Select an option

Save internetsadboy/7febc5c6aa968cb05f62 to your computer and use it in GitHub Desktop.
690 Midterm: derive display parameters for centering an image in a display area.
/*
* CenterImage
*
* Derive display parameters for centering an image within an arbitrary display area
*
* @build Processing sketch app
* @fileType .pde
* @description no physical image used, the image is defined by its dimensions (w, h) and position (x, y)
*/
int winW, winH, displayAreaW, displayAreaH, displayAreaX, displayAreaY, imgW, imgH, imgX, imgY;
void setup ()
{
// window dimensions
winW = 800;
winH = 600;
// draw window
background(255);
size(winW, winH);
// displayArea dimensions
displayAreaW = 600;
displayAreaH = 450;
// arbitrary displayArea position (x, y)
displayAreaX = 40; // center displayArea => x = winW / 2 - displayAreaW / 2
displayAreaY = 40; // center displayArea => y = winH / 2 - displayAreaH / 2;
// draw displayArea
fill(220);
stroke(220);
rect(displayAreaX, displayAreaY, displayAreaW, displayAreaH);
// image dimensions
imgW = 400;
imgH = 200;
// centered image position (x, y)
imgX = (displayAreaW / 2) - (imgW / 2) + displayAreaX;
imgY = (displayAreaH / 2) - (imgH / 2) + displayAreaY;
// draw image
fill(100, 200, 200);
stroke(100, 200, 200);
rect(imgX, imgY, imgW, imgH);
}
void draw ()
{
// stuff
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment