Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

Student question: re-scaling coordinates for Magic Art Gallery

+5 votes
A student asked me about how to re-scale pixel coordinates so that the image always fits nicely in a 600x600 DrawingPanel window.
asked Jan 12, 2015 in (Winter 14) by Forrest Stonedahl (100 points)

1 Answer

+1 vote
There's no need to use complicated IF statements or anything like that.  

It's actually a fairly straightforward math/algebra problem involving ratios -- e.g., if you have a source X value of 200 in an image with width 1000, then how far over should you be in a window of width 600?  (Answer: 120.  Why?)

Or, again, if you have a source X value of 122 in an image that's 300 pixels wide, then what should the X value be in a window with width 600?  (Answer: 244.  Why?)

You just need to generalize these examples for any source image width.  You can get the width of an image by doing myImage.getWidth().
answered Jan 12, 2015 by Forrest Stonedahl (100 points)