package myGraphics.Perspective; import myGraphics.VectorGrafix.Vector; import myGraphics.VectorGrafix.Vector2d; public class Viewport { public int hPix; /* Width of viewport in pixels */ public int vPix; /* Heigth of viewport in pixels */ public double pixPerUnit = 20.0; /* Window to viewport scale factor */ public Viewport() { hPix = 250; vPix = 250; pixPerUnit = 20.0; } public Viewport(int width, int height) { hPix = width; vPix = height; pixPerUnit = 20.0; } public Viewport(int width, int height, double thePixPerUnit) { hPix = width; vPix = height; pixPerUnit = thePixPerUnit; } public Vector2d window2viewport(Vector2d thePoint) { /* * Given a point projected on the image plane, this method translates * that point to a pixel on the viewport. */ return new Vector2d((double) hPix / 2.0 + pixPerUnit * thePoint.x(), (double) vPix / 2.0 - pixPerUnit * thePoint.y()); } public double fitImagePlane(double imageWidth, double imageHeight) { /* * This method will typically be called once, when the initial size of * the viewport is set. It probably ought not to be called when the * user resizes the viewport, unless the user intends the image size * to grow and shrink with the window. */ if (imageHeight / (double) vPix > imageWidth / (double) hPix) { /* taller image: make fit vertically */ pixPerUnit = (double) vPix / imageHeight; } else { /* wider (or equivalently proportioned) image: make fit horizontally */ pixPerUnit = (double) hPix / imageWidth; } return pixPerUnit; } }