1 package prefuse.action.distortion; 2 3 import java.awt.geom.Point2D ; 4 import java.awt.geom.Rectangle2D ; 5 6 24 public class BifocalDistortion extends Distortion { 25 26 private double rx, ry; private double mx, my; 29 32 public BifocalDistortion() { 33 this(0.1,3); 34 } 35 36 51 public BifocalDistortion(double range, double mag) { 52 this(range,mag,range,mag); 53 } 55 76 public BifocalDistortion(double xrange, double xmag, 77 double yrange, double ymag) 78 { 79 rx = xrange; 80 mx = xmag; 81 ry = yrange; 82 my = ymag; 83 m_distortX = !(rx == 0 || mx == 1.0); 84 m_distortY = !(ry == 0 || my == 1.0); 85 } 86 87 90 protected double distortX(double x, Point2D a, Rectangle2D b) { 91 return bifocal(x, a.getX(), rx, mx, b.getMinX(), b.getMaxX()); 92 } 93 94 97 protected double distortY(double y, Point2D a, Rectangle2D b) { 98 return bifocal(y, a.getY(), ry, my, b.getMinY(), b.getMaxY()); 99 } 100 101 104 protected double distortSize(Rectangle2D bbox, double x, double y, 105 Point2D anchor, Rectangle2D bounds) 106 { 107 boolean xmag = false, ymag = false; 108 double m; 109 110 if ( m_distortX ) { 111 double cx = bbox.getCenterX(), ax = anchor.getX(); 112 double minX = bounds.getMinX(), maxX = bounds.getMaxX(); 113 m = (cx<ax ? ax-minX : maxX-ax); 114 if ( m == 0 ) m = maxX-minX; 115 if ( Math.abs(cx-ax) <= rx*m ) 116 xmag = true; 117 } 118 119 if ( m_distortY ) { 120 double cy = bbox.getCenterY(), ay = anchor.getY(); 121 double minY = bounds.getMinY(), maxY = bounds.getMaxY(); 122 m = (cy<ay ? ay-minY : maxY-ay); 123 if ( m == 0 ) m = maxY-minY; 124 if ( Math.abs(cy-ay) <= ry*m ) 125 ymag = true; 126 } 127 128 if ( xmag && !m_distortY ) { 129 return mx; 130 } else if ( ymag && !m_distortX ) { 131 return my; 132 } else if ( xmag && ymag ) { 133 return Math.min(mx,my); 134 } else { 135 return Math.min((1-rx*mx)/(1-rx), (1-ry*my)/(1-ry)); 136 } 137 } 138 139 private double bifocal(double x, double a, double r, 140 double mag, double min, double max) 141 { 142 double m = (x<a ? a-min : max-a); 143 if ( m == 0 ) m = max-min; 144 double v = x - a, s = m*r; 145 if ( Math.abs(v) <= s ) { return x = v*mag + a; 147 } else { double bx = r*mag; 149 x = ((Math.abs(v)-s) / m) * ((1-bx)/(1-r)); 150 return (v<0?-1:1)*m*(x + bx) + a; 151 } 152 } 153 154 158 public double getXMagnification() { 159 return mx; 160 } 161 162 166 public void setXMagnification(double mx) { 167 this.mx = mx; 168 } 169 170 174 public double getYMagnification() { 175 return my; 176 } 177 178 182 public void setYMagnification(double my) { 183 this.my = my; 184 } 185 186 190 public double getXRange() { 191 return rx; 192 } 193 194 198 public void setXRange(double rx) { 199 this.rx = rx; 200 } 201 202 206 public double getYRange() { 207 return ry; 208 } 209 210 214 public void setYRange(double ry) { 215 this.ry = ry; 216 } 217 218 } | Popular Tags |