]> git.mjollnir.org Git - moodle.git/blob
aec9bf6fea5a50ddb85e4a3495d99e945232ad61
[moodle.git] /
1 package flare.vis.operator.distortion
2 {
3         import flash.geom.Rectangle;
4         
5         /**
6          * Computes a graphical fisheye distortion of a graph view. This distortion 
7          * allocates more space to items near the layout anchor and less space to 
8          * items further away, magnifying space near the anchor and demagnifying 
9          * distant space in a continuous fashion.
10          * 
11          * <p>
12          * For more details on this form of transformation, see Manojit Sarkar and 
13          * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of 
14          * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
15          * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
16          * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>. 
17          * </p>
18          */
19         public class FisheyeDistortion extends Distortion
20         {
21                 private var _dx:Number; // x distortion factor
22                 private var _dy:Number; // y distortion factor
23         private var _ds:Number; // size distortion factor
24                 
25                 // --------------------------------------------------------------------
26                 
27                 /**
28              * Create a new FisheyeDistortion with the given distortion factors
29              * along the x and y directions.
30              * @param dx the distortion factor along the x axis (0 for none)
31              * @param dy the distortion factor along the y axis (0 for none)
32              * @param ds the distortion factor to use for sizes (0 for none)
33              */
34                 public function FisheyeDistortion(dx:Number=4, dy:Number=4, ds:Number=3) {
35                         _dx = dx;
36                         _dy = dy;
37                         _ds = ds;
38                         super(_dx>0, _dy>0, _ds>0);
39                 }
40                 
41                 /** @inheritDoc */
42                 protected override function xDistort(x:Number):Number
43                 {
44                         return fisheye(x, layoutAnchor.x, _dx, _b.left, _b.right);
45                 }
46                 
47                 /** @inheritDoc */
48                 protected override function yDistort(y:Number):Number
49                 {
50                         return fisheye(y, layoutAnchor.y, _dy, _b.top, _b.bottom);
51                 }
52                 
53                 /** @inheritDoc */
54                 protected override function sizeDistort(bb:Rectangle, x:Number, y:Number):Number
55                 {
56                         if (!_distortX && !_distortY) return 1;
57                         var fx:Number=1, fy:Number=1;
58                         var a:Number, min:Number, max:Number, v:Number;
59                         
60                 if (_distortX) {
61                     a = layoutAnchor.x;
62                     min = bb.left;
63                     max = bb.right;
64                     v = Math.abs(min-a) > Math.abs(max-a) ? min : max;
65                     if (v < _b.left || v > _b.right) v = (v==min ? max : min);
66                     fx = fisheye(v, a, _dx, _b.left, _b.right);
67                     fx = Math.abs(x-fx) / (max - min);
68                 }
69         
70                 if (_distortY) {
71                         a = layoutAnchor.y;
72                         min = bb.top;
73                         max = bb.bottom;
74                         v = Math.abs(min-a) > Math.abs(max-a) ? min : max;
75                         if (v < _b.top || v > _b.bottom) v = (v==min ? max : min);
76                     fy = fisheye(v, a, _dy, _b.top, _b.bottom);
77                     fy = Math.abs(y-fy) / (max - min);
78                 }
79                 
80                 var sf:Number = (!_distortY ? fx : (!_distortX ? fy : Math.min(fx,fy)));
81                 return (!isFinite(sf) || isNaN(sf)) ? 1 : _ds * sf;
82                 }
83                 
84                 private function fisheye(x:Number, a:Number, d:Number, 
85                         min:Number, max:Number) : Number
86                 {
87                 if (d == 0) return x;
88                 
89                 var left:Boolean = x<a;
90                 var v:Number, m:Number = (left ? a-min : max-a);
91                 if ( m == 0 ) m = max-min;
92                 v = Math.abs(x - a) / m;
93                 v = (d+1)/(d+(1/v));
94                 return (left?-1:1)*m*v + a;
95             }
96                 
97         } // end of class FisheyeDistortion
98 }