comparison client/digitallibrary/jquery/dlGeometry.js @ 723:a653d96166e9 jquery

better birdMoveArea, now called setupBirdDrag.
author robcast
date Tue, 01 Feb 2011 16:03:57 +0100
parents 9f8056d6c289
children f5a0ac47bddd
comparison
equal deleted inserted replaced
721:aaaf0a5ada66 723:a653d96166e9
43 var that = { 43 var that = {
44 x : parseFloat(x), 44 x : parseFloat(x),
45 y : parseFloat(y) 45 y : parseFloat(y)
46 }; 46 };
47 } 47 }
48 that.equals = function(other) { 48 that.equals = function (other) {
49 return (this.x === other.x && this.y === other.y); 49 return (this.x === other.x && this.y === other.y);
50 };
51 // returns new position that is the difference between this and other
52 that.delta = function (other) {
53 return position(other.x - this.x, other.y - this.y);
54 };
55 // adjusts position $elem to this position
56 that.adjustDiv = function ($elem) {
57 $elem.offset({left : this.x, top : this.y});
50 }; 58 };
51 that.toString = function() { 59 that.toString = function() {
52 return (this.x + "," + this.y); 60 return (this.x + "," + this.y);
53 }; 61 };
54 return that; 62 return that;
103 that.setPt1 = function(pos) { 111 that.setPt1 = function(pos) {
104 this.x = pos.x; 112 this.x = pos.x;
105 this.y = pos.y; 113 this.y = pos.y;
106 return this; 114 return this;
107 }; 115 };
116 // adds pos to the upper left corner
117 that.addPt1 = function(pos) {
118 this.x += pos.x;
119 this.y += pos.y;
120 return this;
121 };
122 // sets the lower right corner to position pos
108 that.setPt2 = function(pos) { 123 that.setPt2 = function(pos) {
109 // sets the lower right corner to position pos
110 this.width = pos.x - this.x; 124 this.width = pos.x - this.x;
111 this.height = pos.y - this.y; 125 this.height = pos.y - this.y;
112 return this; 126 return this;
113 }; 127 };
128 // returns the center position of this Rectangle
114 that.getCenter = function() { 129 that.getCenter = function() {
115 // returns the center position of this Rectangle
116 return position(this.x + this.width / 2, this.y + this.height / 2); 130 return position(this.x + this.width / 2, this.y + this.height / 2);
117 }; 131 };
132 // moves this Rectangle's center to position pos
118 that.setCenter = function(pos) { 133 that.setCenter = function(pos) {
119 // moves this Rectangle's center to position pos
120 this.x = pos.x - this.width / 2; 134 this.x = pos.x - this.width / 2;
121 this.y = pos.y - this.height / 2; 135 this.y = pos.y - this.height / 2;
122 return this; 136 return this;
123 }; 137 };
138 // returns the size of this Rectangle
124 that.getSize = function() { 139 that.getSize = function() {
125 // returns the size of this Rectangle
126 return size(this.width, this.height); 140 return size(this.width, this.height);
127 }; 141 };
128 that.equals = function(other) { 142 that.equals = function(other) {
129 // equal props 143 // equal props
130 var eq = (this.x === other.x && this.y === other.y && 144 var eq = (this.x === other.x && this.y === other.y &&
131 this.width === other.width); 145 this.width === other.width);
132 return eq; 146 return eq;
133 }; 147 };
148 // returns the area of this Rectangle
134 that.getArea = function() { 149 that.getArea = function() {
135 // returns the area of this Rectangle
136 return (this.width * this.height); 150 return (this.width * this.height);
137 }; 151 };
152 // eliminates negative width and height
138 that.normalize = function() { 153 that.normalize = function() {
139 // eliminates negative width and height
140 var p = this.getPt2(); 154 var p = this.getPt2();
141 this.x = Math.min(this.x, p.x); 155 this.x = Math.min(this.x, p.x);
142 this.y = Math.min(this.y, p.y); 156 this.y = Math.min(this.y, p.y);
143 this.width = Math.abs(this.width); 157 this.width = Math.abs(this.width);
144 this.height = Math.abs(this.height); 158 this.height = Math.abs(this.height);
145 return this; 159 return this;
146 }; 160 };
161 // returns if Position "pos" lies inside of this rectangle
147 that.containsPosition = function(pos) { 162 that.containsPosition = function(pos) {
148 // returns if Position "pos" lies inside of this rectangle
149 var ct = ((pos.x >= this.x) && (pos.y >= this.y) && 163 var ct = ((pos.x >= this.x) && (pos.y >= this.y) &&
150 (pos.x <= this.x + this.width) && (pos.y <= this.y + this.height)); 164 (pos.x <= this.x + this.width) && (pos.y <= this.y + this.height));
151 return ct; 165 return ct;
152 }; 166 };
167 // returns if rectangle "rect" is contained in this rectangle
153 that.containsRect = function(rect) { 168 that.containsRect = function(rect) {
154 // returns if rectangle "rect" is contained in this rectangle
155 return (this.containsPosition(rect.getPt1()) && this.containsPosition(rect.getPt2())); 169 return (this.containsPosition(rect.getPt1()) && this.containsPosition(rect.getPt2()));
156 }; 170 };
171 // changes this rectangle's x/y values so it stays inside of rectangle rect
172 // keeping the proportions
157 that.stayInside = function(rect) { 173 that.stayInside = function(rect) {
158 // changes this rectangle's x/y values so it stays inside of rectangle rect
159 // keeping the proportions
160 if (this.x < rect.x) { 174 if (this.x < rect.x) {
161 this.x = rect.x; 175 this.x = rect.x;
162 } 176 }
163 if (this.y < rect.y) { 177 if (this.y < rect.y) {
164 this.y = rect.y; 178 this.y = rect.y;
169 if (this.y + this.height > rect.y + rect.height) { 183 if (this.y + this.height > rect.y + rect.height) {
170 this.y = rect.y + rect.height - this.height; 184 this.y = rect.y + rect.height - this.height;
171 } 185 }
172 return this; 186 return this;
173 }; 187 };
188 // clips this rectangle so it stays inside of rectangle rect
174 that.clipTo = function(rect) { 189 that.clipTo = function(rect) {
175 // clips this rectangle so it stays inside of rectangle rect
176 var p1 = rect.getPt1(); 190 var p1 = rect.getPt1();
177 var p2 = rect.getPt2(); 191 var p2 = rect.getPt2();
178 var this2 = this.getPt2(); 192 var this2 = this.getPt2();
179 this.setPt1(position(Math.max(this.x, p1.x), Math.max(this.y, p1.y))); 193 this.setPt1(position(Math.max(this.x, p1.x), Math.max(this.y, p1.y)));
180 this.setPt2(position(Math.min(this2.x, p2.x), Math.min(this2.y, p2.y))); 194 this.setPt2(position(Math.min(this2.x, p2.x), Math.min(this2.y, p2.y)));
181 return this; 195 return this;
182 }; 196 };
197 // returns the intersection of the given Rectangle and this one
183 that.intersect = function(rect) { 198 that.intersect = function(rect) {
184 // returns the intersection of the given Rectangle and this one
185 // FIX ME: not really, it should return null if there is no overlap 199 // FIX ME: not really, it should return null if there is no overlap
186 var sec = rect.copy(); 200 var sec = rect.copy();
187 if (sec.x < this.x) { 201 if (sec.x < this.x) {
188 sec.width = sec.width - (this.x - sec.x); 202 sec.width = sec.width - (this.x - sec.x);
189 sec.x = this.x; 203 sec.x = this.x;
198 if (sec.y + sec.height > this.y + this.height) { 212 if (sec.y + sec.height > this.y + this.height) {
199 sec.height = (this.y + this.height) - sec.y; 213 sec.height = (this.y + this.height) - sec.y;
200 } 214 }
201 return sec; 215 return sec;
202 }; 216 };
217 // returns a Rectangle that fits into this one (by moving first)
203 that.fit = function(rect) { 218 that.fit = function(rect) {
204 // returns a Rectangle that fits into this one (by moving first)
205 var sec = rect.copy(); 219 var sec = rect.copy();
206 sec.x = Math.max(sec.x, this.x); 220 sec.x = Math.max(sec.x, this.x);
207 sec.y = Math.max(sec.y, this.x); 221 sec.y = Math.max(sec.y, this.x);
208 if (sec.x + sec.width > this.x + this.width) { 222 if (sec.x + sec.width > this.x + this.width) {
209 sec.x = this.x + this.width - sec.width; 223 sec.x = this.x + this.width - sec.width;