1 19 package jcckit.renderer; 20 21 import jcckit.graphic.Anchor; 22 import jcckit.graphic.GraphPoint; 23 import jcckit.graphic.ClippingRectangle; 24 25 36 public class Transformation { 37 private final double _scale, _x0, _y0; 38 39 49 public Transformation(int width, int height, ClippingRectangle paper, 50 Anchor horizontalAnchor, Anchor verticalAnchor) { 51 double pWidth = paper.getMaxX() - paper.getMinX(); 52 double pHeight = paper.getMaxY() - paper.getMinY(); 53 _scale = Math.min(width / pWidth, height / pHeight); 54 _x0 = 0.5 * horizontalAnchor.getFactor() * (width - _scale * pWidth) 55 - _scale * paper.getMinX(); 56 _y0 = 0.5 * verticalAnchor.getFactor() * (_scale * pHeight - height) 57 + height + _scale * + paper.getMinY(); 58 } 59 60 61 public int transformX(double x) { 62 return trim(_scale * x + _x0); 63 } 64 65 66 public int transformY(double y) { 67 return trim(_y0 - _scale * y); 68 } 69 70 71 public int transformWidth(double width) { 72 return trim(_scale * width + 0.5); 73 } 74 75 76 public int transformHeight(double height) { 77 return trim(_scale * height + 0.5); 78 } 79 80 private static int trim(double number) 81 { 82 return number > Short.MAX_VALUE 83 ? Short.MAX_VALUE 84 : (number < Short.MIN_VALUE ? Short.MIN_VALUE : (int) number); 85 } 86 87 91 public GraphPoint transformBack(int x, int y) { 92 return new GraphPoint((x - _x0) / _scale, (_y0 - y) / _scale); 93 } 94 } 95 96 | Popular Tags |