Mercurial > hg > digilib-old
annotate client/digitallibrary/jquery/dlGeometry.js @ 736:54928116a7b2 jquery
merge
author | hertzhaft |
---|---|
date | Wed, 02 Feb 2011 00:25:49 +0100 |
parents | 25f2898696a2 |
children | b4460f0a540d |
rev | line source |
---|---|
606 | 1 /* digilib geometry classes |
2 * should be integrated into jquery.digilib.js | |
3 */ | |
4 | |
5 var dlGeometry = function() { | |
6 /* | |
7 * Size class | |
8 */ | |
9 var size = function (w, h) { | |
10 var that = { | |
11 width : parseFloat(w), | |
12 height : parseFloat(h) | |
13 }; | |
14 that.equals = function(other) { | |
15 return (this.width === other.width && this.height === other.height); | |
16 }; | |
17 that.toString = function() { | |
18 return (this.width + "x" + this.height); | |
19 }; | |
20 return that; | |
21 }; | |
22 | |
23 /* | |
24 * Position class | |
25 */ | |
26 var position = function (x, y) { | |
654 | 27 if (typeof x === "object") { |
28 if (x instanceof jQuery) { | |
29 // jQuery object | |
30 var pos = x.offset(); | |
31 var that = { | |
32 x : pos.left, | |
33 y : pos.top | |
34 }; | |
35 } else { | |
36 // event object(?) | |
37 var that = { | |
38 x : x.pageX, | |
39 y : x.pageY | |
40 }; | |
41 } | |
42 } else { | |
43 var that = { | |
44 x : parseFloat(x), | |
45 y : parseFloat(y) | |
46 }; | |
47 } | |
730 | 48 that.equals = function (other) { |
606 | 49 return (this.x === other.x && this.y === other.y); |
50 }; | |
733 | 51 // add position other to this |
52 that.add = function (other) { | |
53 this.x += other.x; | |
54 this.y += other.y; | |
55 return this; | |
56 }; | |
730 | 57 // returns new position that is the difference between this and other |
58 that.delta = function (other) { | |
59 return position(other.x - this.x, other.y - this.y); | |
60 }; | |
61 // adjusts position $elem to this position | |
62 that.adjustDiv = function ($elem) { | |
63 $elem.offset({left : this.x, top : this.y}); | |
64 }; | |
733 | 65 // returns distance of this position to pos (length if pos == null) |
66 that.distance = function (pos) { | |
67 if (pos == null) { | |
68 pos = {x : 0, y : 0}; | |
69 } | |
70 var dx = pos.x - this.x; | |
71 var dy = pos.y - this.y; | |
72 return Math.sqrt(dx * dx + dy * dy); | |
73 }; | |
606 | 74 that.toString = function() { |
75 return (this.x + "," + this.y); | |
76 }; | |
77 return that; | |
78 }; | |
79 /* | |
80 * Rectangle class | |
81 */ | |
82 var rectangle = function (x, y, w, h) { | |
83 var that = {}; | |
84 if (typeof x === "object") { | |
654 | 85 if (x instanceof jQuery) { |
86 // jQuery object | |
87 var pos = x.offset(); | |
88 that = { | |
89 x : pos.left, | |
90 y : pos.top, | |
91 width : x.width(), | |
92 height : x.height() | |
93 }; | |
94 } else { | |
95 // assume x and y are Position | |
96 that = { | |
672
7f7536a5b6d9
image transform works now under rotation!!! (at least for multiples of 90deg)
robcast
parents:
671
diff
changeset
|
97 x : Math.min(x.x, y.x), |
7f7536a5b6d9
image transform works now under rotation!!! (at least for multiples of 90deg)
robcast
parents:
671
diff
changeset
|
98 y : Math.min(x.y, y.y), |
7f7536a5b6d9
image transform works now under rotation!!! (at least for multiples of 90deg)
robcast
parents:
671
diff
changeset
|
99 width : Math.abs(y.x - x.x), |
7f7536a5b6d9
image transform works now under rotation!!! (at least for multiples of 90deg)
robcast
parents:
671
diff
changeset
|
100 height : Math.abs(y.y - x.y) |
654 | 101 }; |
102 } | |
606 | 103 } else { |
104 that = { | |
105 x : parseFloat(x), | |
106 y : parseFloat(y), | |
107 width : parseFloat(w), | |
108 height : parseFloat(h) | |
109 }; | |
110 } | |
111 // returns a copy of this Rectangle | |
112 that.copy = function() { | |
113 return rectangle(this.x, this.y, this.width, this.height); | |
114 }; | |
115 // returns the position of this Rectangle | |
116 that.getPosition = function() { | |
117 return position(this.x, this.y); | |
118 }; | |
119 // returns the upper left corner position | |
120 that.getPt1 = that.getPosition; | |
121 // returns the lower right corner position of this Rectangle | |
122 that.getPt2 = function() { | |
123 return position(this.x + this.width, this.y + this.height); | |
124 }; | |
733 | 125 // sets the upper left corner position to pos |
126 that.setPosition = function(pos) { | |
606 | 127 this.x = pos.x; |
128 this.y = pos.y; | |
129 return this; | |
130 }; | |
733 | 131 that.setPt1 = that.setPosition; // TODO: not really the same |
132 // adds pos to the position | |
133 that.addPosition = function(pos) { | |
730 | 134 this.x += pos.x; |
135 this.y += pos.y; | |
136 return this; | |
137 }; | |
138 // sets the lower right corner to position pos | |
606 | 139 that.setPt2 = function(pos) { |
140 this.width = pos.x - this.x; | |
141 this.height = pos.y - this.y; | |
142 return this; | |
143 }; | |
730 | 144 // returns the center position of this Rectangle |
606 | 145 that.getCenter = function() { |
146 return position(this.x + this.width / 2, this.y + this.height / 2); | |
147 }; | |
730 | 148 // moves this Rectangle's center to position pos |
606 | 149 that.setCenter = function(pos) { |
150 this.x = pos.x - this.width / 2; | |
151 this.y = pos.y - this.height / 2; | |
152 return this; | |
153 }; | |
730 | 154 // returns the size of this Rectangle |
606 | 155 that.getSize = function() { |
156 return size(this.width, this.height); | |
157 }; | |
158 that.equals = function(other) { | |
159 // equal props | |
160 var eq = (this.x === other.x && this.y === other.y && | |
161 this.width === other.width); | |
162 return eq; | |
163 }; | |
730 | 164 // returns the area of this Rectangle |
606 | 165 that.getArea = function() { |
166 return (this.width * this.height); | |
167 }; | |
730 | 168 // eliminates negative width and height |
606 | 169 that.normalize = function() { |
170 var p = this.getPt2(); | |
171 this.x = Math.min(this.x, p.x); | |
172 this.y = Math.min(this.y, p.y); | |
173 this.width = Math.abs(this.width); | |
174 this.height = Math.abs(this.height); | |
175 return this; | |
176 }; | |
730 | 177 // returns if Position "pos" lies inside of this rectangle |
606 | 178 that.containsPosition = function(pos) { |
179 var ct = ((pos.x >= this.x) && (pos.y >= this.y) && | |
654 | 180 (pos.x <= this.x + this.width) && (pos.y <= this.y + this.height)); |
606 | 181 return ct; |
182 }; | |
730 | 183 // returns if rectangle "rect" is contained in this rectangle |
606 | 184 that.containsRect = function(rect) { |
185 return (this.containsPosition(rect.getPt1()) && this.containsPosition(rect.getPt2())); | |
186 }; | |
730 | 187 // changes this rectangle's x/y values so it stays inside of rectangle rect |
188 // keeping the proportions | |
606 | 189 that.stayInside = function(rect) { |
190 if (this.x < rect.x) { | |
191 this.x = rect.x; | |
192 } | |
193 if (this.y < rect.y) { | |
194 this.y = rect.y; | |
195 } | |
196 if (this.x + this.width > rect.x + rect.width) { | |
197 this.x = rect.x + rect.width - this.width; | |
198 } | |
199 if (this.y + this.height > rect.y + rect.height) { | |
200 this.y = rect.y + rect.height - this.height; | |
201 } | |
202 return this; | |
203 }; | |
730 | 204 // clips this rectangle so it stays inside of rectangle rect |
606 | 205 that.clipTo = function(rect) { |
206 var p1 = rect.getPt1(); | |
207 var p2 = rect.getPt2(); | |
208 var this2 = this.getPt2(); | |
209 this.setPt1(position(Math.max(this.x, p1.x), Math.max(this.y, p1.y))); | |
210 this.setPt2(position(Math.min(this2.x, p2.x), Math.min(this2.y, p2.y))); | |
211 return this; | |
212 }; | |
730 | 213 // returns the intersection of the given Rectangle and this one |
606 | 214 that.intersect = function(rect) { |
215 // FIX ME: not really, it should return null if there is no overlap | |
216 var sec = rect.copy(); | |
217 if (sec.x < this.x) { | |
218 sec.width = sec.width - (this.x - sec.x); | |
219 sec.x = this.x; | |
220 } | |
221 if (sec.y < this.y) { | |
222 sec.height = sec.height - (this.y - sec.y); | |
223 sec.y = this.y; | |
224 } | |
225 if (sec.x + sec.width > this.x + this.width) { | |
226 sec.width = (this.x + this.width) - sec.x; | |
227 } | |
228 if (sec.y + sec.height > this.y + this.height) { | |
229 sec.height = (this.y + this.height) - sec.y; | |
230 } | |
231 return sec; | |
232 }; | |
730 | 233 // returns a Rectangle that fits into this one (by moving first) |
606 | 234 that.fit = function(rect) { |
235 var sec = rect.copy(); | |
236 sec.x = Math.max(sec.x, this.x); | |
237 sec.y = Math.max(sec.y, this.x); | |
238 if (sec.x + sec.width > this.x + this.width) { | |
239 sec.x = this.x + this.width - sec.width; | |
240 } | |
241 if (sec.y + sec.height > this.y + this.height) { | |
242 sec.y = this.y + this.height - sec.height; | |
243 } | |
244 return sec.intersect(this); | |
245 }; | |
672
7f7536a5b6d9
image transform works now under rotation!!! (at least for multiples of 90deg)
robcast
parents:
671
diff
changeset
|
246 // adjusts position and size of $elem to this rectangle |
7f7536a5b6d9
image transform works now under rotation!!! (at least for multiples of 90deg)
robcast
parents:
671
diff
changeset
|
247 that.adjustDiv = function ($elem) { |
7f7536a5b6d9
image transform works now under rotation!!! (at least for multiples of 90deg)
robcast
parents:
671
diff
changeset
|
248 $elem.offset({left : this.x, top : this.y}); |
7f7536a5b6d9
image transform works now under rotation!!! (at least for multiples of 90deg)
robcast
parents:
671
diff
changeset
|
249 $elem.width(this.width).height(this.height); |
7f7536a5b6d9
image transform works now under rotation!!! (at least for multiples of 90deg)
robcast
parents:
671
diff
changeset
|
250 }; |
606 | 251 that.toString = function() { |
252 return this.width+"x"+this.height+"@"+this.x+","+this.y; | |
253 }; | |
254 return that; | |
255 }; | |
256 | |
257 /* | |
258 * Transform class | |
259 * | |
260 * defines a class of affine transformations | |
261 */ | |
262 var transform = function (spec) { | |
664
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
263 var that = { |
627 | 264 m00 : 1.0, |
265 m01 : 0.0, | |
266 m02 : 0.0, | |
267 m10 : 0.0, | |
268 m11 : 1.0, | |
664
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
269 m12 : 0.0, |
627 | 270 m20 : 0.0, |
271 m21 : 0.0, | |
272 m22 : 1.0 | |
664
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
273 }; |
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
274 if (spec) { |
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
275 jQuery.extend(that, spec); |
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
276 }; |
672
7f7536a5b6d9
image transform works now under rotation!!! (at least for multiples of 90deg)
robcast
parents:
671
diff
changeset
|
277 that.concat = function(trafA) { |
7f7536a5b6d9
image transform works now under rotation!!! (at least for multiples of 90deg)
robcast
parents:
671
diff
changeset
|
278 // add Transform trafA to this Transform (i.e. this = trafC = trafA * this) |
7f7536a5b6d9
image transform works now under rotation!!! (at least for multiples of 90deg)
robcast
parents:
671
diff
changeset
|
279 var trafC = {}; |
606 | 280 for (var i = 0; i < 3; i++) { |
281 for (var j = 0; j < 3; j++) { | |
282 var c = 0.0; | |
283 for (var k = 0; k < 3; k++) { | |
672
7f7536a5b6d9
image transform works now under rotation!!! (at least for multiples of 90deg)
robcast
parents:
671
diff
changeset
|
284 c += trafA["m"+i+k] * this["m"+k+j]; |
606 | 285 } |
672
7f7536a5b6d9
image transform works now under rotation!!! (at least for multiples of 90deg)
robcast
parents:
671
diff
changeset
|
286 trafC["m"+i+j] = c; |
606 | 287 } |
288 } | |
672
7f7536a5b6d9
image transform works now under rotation!!! (at least for multiples of 90deg)
robcast
parents:
671
diff
changeset
|
289 jQuery.extend(this, trafC); |
606 | 290 return this; |
291 }; | |
292 that.transform = function(rect) { | |
293 // returns transformed Rectangle or Position with this Transform applied | |
294 var x = this.m00 * rect.x + this.m01 * rect.y + this.m02; | |
295 var y = this.m10 * rect.x + this.m11 * rect.y + this.m12; | |
672
7f7536a5b6d9
image transform works now under rotation!!! (at least for multiples of 90deg)
robcast
parents:
671
diff
changeset
|
296 var pt = position(x, y); |
606 | 297 if (rect.width) { |
672
7f7536a5b6d9
image transform works now under rotation!!! (at least for multiples of 90deg)
robcast
parents:
671
diff
changeset
|
298 // transform the other corner point |
7f7536a5b6d9
image transform works now under rotation!!! (at least for multiples of 90deg)
robcast
parents:
671
diff
changeset
|
299 var pt2 = this.transform(rect.getPt2()); |
7f7536a5b6d9
image transform works now under rotation!!! (at least for multiples of 90deg)
robcast
parents:
671
diff
changeset
|
300 return rectangle(pt, pt2); |
606 | 301 } |
672
7f7536a5b6d9
image transform works now under rotation!!! (at least for multiples of 90deg)
robcast
parents:
671
diff
changeset
|
302 return pt; |
606 | 303 }; |
304 that.invtransform = function(rect) { | |
305 // returns transformed Rectangle or Position with the inverse of this Transform applied | |
306 var det = this.m00 * this.m11 - this.m01 * this.m10; | |
307 var x = (this.m11 * rect.x - this.m01 * rect.y - this.m11 * this.m02 + this.m01 * this.m12) / det; | |
672
7f7536a5b6d9
image transform works now under rotation!!! (at least for multiples of 90deg)
robcast
parents:
671
diff
changeset
|
308 var y = (-this.m10 * rect.x + this.m00 * rect.y + this.m10 * this.m02 - this.m00 * this.m12) / det; |
7f7536a5b6d9
image transform works now under rotation!!! (at least for multiples of 90deg)
robcast
parents:
671
diff
changeset
|
309 var pt = position(x, y); |
606 | 310 if (rect.width) { |
672
7f7536a5b6d9
image transform works now under rotation!!! (at least for multiples of 90deg)
robcast
parents:
671
diff
changeset
|
311 // transform the other corner point |
7f7536a5b6d9
image transform works now under rotation!!! (at least for multiples of 90deg)
robcast
parents:
671
diff
changeset
|
312 var pt2 = this.invtransform(rect.getPt2()); |
7f7536a5b6d9
image transform works now under rotation!!! (at least for multiples of 90deg)
robcast
parents:
671
diff
changeset
|
313 return rectangle(pt, pt2); |
606 | 314 } |
672
7f7536a5b6d9
image transform works now under rotation!!! (at least for multiples of 90deg)
robcast
parents:
671
diff
changeset
|
315 return pt; |
606 | 316 }; |
664
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
317 that.toString = function (pretty) { |
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
318 var s = '['; |
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
319 if (pretty) s += '\n'; |
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
320 for (var i = 0; i < 3; ++i) { |
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
321 s += '['; |
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
322 for (var j = 0; j < 3; ++j) { |
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
323 if (j) s += ','; |
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
324 s += this['m'+i+j]; |
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
325 } |
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
326 s += ']'; |
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
327 if (pretty) s += '\n'; |
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
328 } |
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
329 s += ']'; |
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
330 if (pretty) s += '\n'; |
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
331 return s; |
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
332 }; |
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
333 // add class methods to instance |
627 | 334 that.getRotation = transform.getRotation; |
664
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
335 that.getRotationAround = transform.getRotationAround; |
627 | 336 that.getTranslation = transform.getTranslation; |
675 | 337 that.getMirror = transform.getMirror; |
627 | 338 that.getScale = transform.getScale; |
655 | 339 |
606 | 340 return that; |
341 }; | |
342 | |
664
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
343 transform.getRotation = function (angle) { |
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
344 // returns a Transform that is a rotation by angle degrees around [0,0] |
606 | 345 if (angle !== 0) { |
664
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
346 var t = Math.PI * parseFloat(angle) / 180.0; |
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
347 var cost = Math.cos(t); |
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
348 var sint = Math.sin(t); |
606 | 349 var traf = { |
664
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
350 m00 : cost, |
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
351 m01 : -sint, |
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
352 m10 : sint, |
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
353 m11 : cost |
606 | 354 }; |
355 return transform(traf); | |
356 } | |
357 return transform(); | |
358 }; | |
359 | |
675 | 360 transform.getRotationAround = function (angle, pos) { |
361 // returns a Transform that is a rotation by angle degrees around pos | |
362 var traf = transform.getTranslation({x : -pos.x, y : -pos.y}); | |
363 traf.concat(transform.getRotation(angle)); | |
364 traf.concat(transform.getTranslation(pos)); | |
365 return traf; | |
366 }; | |
367 | |
606 | 368 transform.getTranslation = function (pos) { |
369 // returns a Transform that is a translation by [pos.x, pos,y] | |
370 var traf = { | |
371 m02 : pos.x, | |
372 m12 : pos.y | |
373 }; | |
374 return transform(traf); | |
375 }; | |
376 | |
675 | 377 transform.getMirror = function (type) { |
378 // returns a Transform that is a mirror about the axis type | |
379 if (type === 'x') { | |
380 var traf = { | |
381 m00 : 1, | |
382 m11 : -1 | |
383 }; | |
384 } else { | |
385 var traf = { | |
386 m00 : -1, | |
387 m11 : 1 | |
388 }; | |
389 } | |
390 return transform(traf); | |
664
d5a5ee4cbf04
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
656
diff
changeset
|
391 }; |
675 | 392 |
606 | 393 transform.getScale = function (size) { |
394 // returns a Transform that is a scale by [size.width, size.height] | |
395 var traf = { | |
396 m00 : size.width, | |
397 m11 : size.height | |
398 }; | |
399 return transform(traf); | |
400 }; | |
401 | |
402 // export functions | |
403 var that = { | |
404 size : size, | |
405 position : position, | |
406 rectangle : rectangle, | |
407 transform : transform | |
408 }; | |
409 | |
410 return that; | |
411 }; |