annotate client/digitallibrary/jquery/jquery.digilib.geometry.js @ 812:b484631f37c1 jquery

better intersect function
author hertzhaft
date Mon, 21 Feb 2011 01:00:26 +0100
parents 1b1728926534
children c662bf335881
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
1 /** required digilib geometry plugin
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
2 */
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
3
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
4 (function($) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
5 //var dlGeometry = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
6 /*
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
7 * Size class
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
8 */
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
9 var size = function(w, h) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
10 var that;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
11 if (typeof w === "object") {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
12 // assume its size
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
13 that = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
14 width : w.width,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
15 height : w.height
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
16 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
17 } else {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
18 that = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
19 width : parseFloat(w),
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
20 height : parseFloat(h)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
21 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
22 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
23 that.equals = function(other) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
24 return (this.width === other.width && this.height === other.height);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
25 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
26 that.toString = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
27 return (this.width + "x" + this.height);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
28 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
29 return that;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
30 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
31
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
32 /*
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
33 * Position class
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
34 */
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
35 var position = function(x, y) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
36 var that;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
37 if (typeof x === "object") {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
38 if (x instanceof jQuery) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
39 // jQuery object
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
40 var pos = x.offset();
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
41 that = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
42 x : pos.left,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
43 y : pos.top
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
44 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
45 } else {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
46 if (x.x != null) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
47 // position object
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
48 that = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
49 x : x.x,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
50 y : x.y
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
51 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
52 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
53 if (x.pageX != null) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
54 // event object
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
55 that = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
56 x : x.pageX,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
57 y : x.pageY
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
58 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
59 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
60 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
61 } else {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
62 that = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
63 x : parseFloat(x),
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
64 y : parseFloat(y)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
65 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
66 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
67 that.equals = function(other) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
68 return (this.x === other.x && this.y === other.y);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
69 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
70 // add position other to this
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
71 that.add = function(other) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
72 this.x += other.x;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
73 this.y += other.y;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
74 return this;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
75 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
76 // returns negative position
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
77 that.neg = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
78 return position({
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
79 x : -this.x,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
80 y : -this.y
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
81 });
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
82 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
83 // returns new position that is the difference between this and other
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
84 that.delta = function(other) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
85 return position({
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
86 x : other.x - this.x,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
87 y : other.y - this.y
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
88 });
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
89 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
90 // adjusts position $elem to this position
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
91 that.adjustDiv = function($elem) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
92 $elem.offset({
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
93 left : this.x,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
94 top : this.y
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
95 });
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
96 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
97 // returns distance of this position to pos (length if pos == null)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
98 that.distance = function(pos) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
99 if (pos == null) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
100 pos = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
101 x : 0,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
102 y : 0
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
103 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
104 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
105 var dx = pos.x - this.x;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
106 var dy = pos.y - this.y;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
107 return Math.sqrt(dx * dx + dy * dy);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
108 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
109 that.toString = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
110 return (this.x + "," + this.y);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
111 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
112 return that;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
113 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
114 /*
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
115 * Rectangle class
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
116 */
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
117 var rectangle = function(x, y, w, h) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
118 var that = {};
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
119 if (typeof x === "object") {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
120 if (x instanceof jQuery) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
121 // jQuery object
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
122 var pos = x.offset();
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
123 that = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
124 x : pos.left,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
125 y : pos.top,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
126 width : x.width(),
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
127 height : x.height()
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
128 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
129 } else if (y == null) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
130 // assume x is rectangle
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
131 that = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
132 x : x.x,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
133 y : x.y,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
134 width : x.width,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
135 height : x.height
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
136 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
137 } else {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
138 // assume x and y are Position
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
139 that = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
140 x : Math.min(x.x, y.x),
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
141 y : Math.min(x.y, y.y),
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
142 width : Math.abs(y.x - x.x),
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
143 height : Math.abs(y.y - x.y)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
144 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
145 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
146 } else {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
147 that = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
148 x : parseFloat(x),
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
149 y : parseFloat(y),
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
150 width : parseFloat(w),
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
151 height : parseFloat(h)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
152 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
153 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
154 // returns a copy of this Rectangle
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
155 that.copy = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
156 return rectangle(this);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
157 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
158 // returns the position of this Rectangle
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
159 that.getPosition = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
160 return position(this);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
161 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
162 // returns the size of this Rectangle
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
163 that.getSize = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
164 return size(this);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
165 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
166 // returns the upper left corner position
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
167 that.getPt1 = that.getPosition;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
168 // returns the lower right corner position of this Rectangle
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
169 that.getPt2 = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
170 return position({
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
171 x : this.x + this.width,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
172 y : this.y + this.height
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
173 });
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
174 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
175 // sets the upper left corner position to pos
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
176 that.setPosition = function(pos) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
177 this.x = pos.x;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
178 this.y = pos.y;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
179 return this;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
180 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
181 // adds pos to the position
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
182 that.setPt1 = that.setPosition; // TODO: not really the same
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
183 that.addPosition = function(pos) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
184 this.x += pos.x;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
185 this.y += pos.y;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
186 return this;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
187 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
188 // sets the lower right corner to position pos
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
189 that.setPt2 = function(pos) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
190 this.width = pos.x - this.x;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
191 this.height = pos.y - this.y;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
192 return this;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
193 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
194 // returns the center position of this Rectangle
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
195 that.getCenter = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
196 return position({
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
197 x : this.x + this.width / 2,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
198 y : this.y + this.height / 2
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
199 });
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
200 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
201 // moves this rectangle's center to position pos
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
202 that.setCenter = function(pos) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
203 this.x = pos.x - this.width / 2;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
204 this.y = pos.y - this.height / 2;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
205 return this;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
206 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
207 // returns true if both rectangles have equal position and proportion
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
208 that.equals = function(other) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
209 var eq = (this.x === other.x && this.y === other.y && this.width === other.width);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
210 return eq;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
211 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
212 // returns the area of this Rectangle
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
213 that.getArea = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
214 return (this.width * this.height);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
215 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
216 // eliminates negative width and height
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
217 that.normalize = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
218 var p = this.getPt2();
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
219 this.x = Math.min(this.x, p.x);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
220 this.y = Math.min(this.y, p.y);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
221 this.width = Math.abs(this.width);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
222 this.height = Math.abs(this.height);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
223 return this;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
224 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
225 // returns if Position "pos" lies inside of this rectangle
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
226 that.containsPosition = function(pos) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
227 var ct = ((pos.x >= this.x) && (pos.y >= this.y)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
228 && (pos.x <= this.x + this.width) && (pos.y <= this.y
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
229 + this.height));
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
230 return ct;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
231 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
232 // returns true if rectangle "rect" is contained in this rectangle
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
233 that.containsRect = function(rect) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
234 return (this.containsPosition(rect.getPt1()) && this
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
235 .containsPosition(rect.getPt2()));
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
236 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
237 // returns true if rectangle "rect" and this rectangle overlap
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
238 that.overlapsRect = function(rect) {
812
b484631f37c1 better intersect function
hertzhaft
parents: 807
diff changeset
239 return this.intersect(rect) != null;
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
240 };
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
241 // changes this rectangle's x/y values so it stays inside of rectangle
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
242 // "rect", keeping the proportions
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
243 that.stayInside = function(rect) {
812
b484631f37c1 better intersect function
hertzhaft
parents: 807
diff changeset
244 this.x = Math.max(this.x, rect.x);
b484631f37c1 better intersect function
hertzhaft
parents: 807
diff changeset
245 this.y = Math.max(this.y, rect.y);
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
246 if (this.x + this.width > rect.x + rect.width) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
247 this.x = rect.x + rect.width - this.width;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
248 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
249 if (this.y + this.height > rect.y + rect.height) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
250 this.y = rect.y + rect.height - this.height;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
251 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
252 return this;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
253 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
254 // clips this rectangle so it stays inside of rectangle "rect"
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
255 that.clipTo = function(rect) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
256 var p1 = rect.getPt1();
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
257 var p2 = rect.getPt2();
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
258 var this2 = this.getPt2();
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
259 this.setPosition(position(Math.max(this.x, p1.x), Math.max(this.y, p1.y)));
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
260 this.setPt2(position(Math.min(this2.x, p2.x), Math.min(this2.y, p2.y)));
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
261 return this;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
262 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
263 // returns the intersection of rectangle "rect" and this one
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
264 that.intersect = function(rect) {
812
b484631f37c1 better intersect function
hertzhaft
parents: 807
diff changeset
265 var res = rect.clipTo(this);
b484631f37c1 better intersect function
hertzhaft
parents: 807
diff changeset
266 if (res.width < 0 || res.height < 0) res = null;
b484631f37c1 better intersect function
hertzhaft
parents: 807
diff changeset
267 return res;
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
268 };
812
b484631f37c1 better intersect function
hertzhaft
parents: 807
diff changeset
269
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
270 // returns a copy of rectangle "rect" that fits into this one
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
271 // (moving it first)
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
272 that.fit = function(rect) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
273 var sec = rect.copy();
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
274 sec.x = Math.max(sec.x, this.x);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
275 sec.y = Math.max(sec.y, this.x);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
276 if (sec.x + sec.width > this.x + this.width) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
277 sec.x = this.x + this.width - sec.width;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
278 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
279 if (sec.y + sec.height > this.y + this.height) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
280 sec.y = this.y + this.height - sec.height;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
281 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
282 return sec.intersect(this);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
283 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
284 // adjusts position and size of jQuery element "$elem" to this rectangle
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
285 that.adjustDiv = function($elem) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
286 $elem.offset({
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
287 left : this.x,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
288 top : this.y
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
289 });
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
290 $elem.width(this.width).height(this.height);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
291 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
292 // returns position and size of this rectangle in css-compatible format
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
293 that.getAsCss = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
294 return {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
295 left : this.x,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
296 top : this.y,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
297 width : this.width,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
298 height : this.height
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
299 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
300 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
301 // returns position and size of this rectangle formatted for SVG attributes
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
302 that.getAsSvg = function() {
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
303 return [this.x, this.y, this.width, this.height].join(" ");
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
304 };
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
305 // returns size and position of this rectangle formatted for ??? (w x h@x,y)
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
306 that.toString = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
307 return this.width + "x" + this.height + "@" + this.x + "," + this.y;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
308 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
309 return that;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
310 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
311
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
312 /*
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
313 * Transform class
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
314 *
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
315 * defines a class of affine transformations
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
316 */
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
317 var transform = function(spec) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
318 var that = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
319 m00 : 1.0,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
320 m01 : 0.0,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
321 m02 : 0.0,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
322 m10 : 0.0,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
323 m11 : 1.0,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
324 m12 : 0.0,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
325 m20 : 0.0,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
326 m21 : 0.0,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
327 m22 : 1.0
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
328 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
329 if (spec) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
330 jQuery.extend(that, spec);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
331 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
332 ;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
333 that.concat = function(trafA) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
334 // add Transform trafA to this Transform (i.e. this = trafC = trafA
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
335 // * this)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
336 var trafC = {};
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
337 for ( var i = 0; i < 3; i++) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
338 for ( var j = 0; j < 3; j++) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
339 var c = 0.0;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
340 for ( var k = 0; k < 3; k++) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
341 c += trafA["m" + i + k] * this["m" + k + j];
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
342 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
343 trafC["m" + i + j] = c;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
344 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
345 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
346 jQuery.extend(this, trafC);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
347 return this;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
348 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
349 that.transform = function(rect) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
350 // returns transformed Rectangle or Position with this Transform
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
351 // applied
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
352 var x = this.m00 * rect.x + this.m01 * rect.y + this.m02;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
353 var y = this.m10 * rect.x + this.m11 * rect.y + this.m12;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
354 var pt = position(x, y);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
355 if (rect.width) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
356 // transform the other corner point
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
357 var pt2 = this.transform(rect.getPt2());
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
358 return rectangle(pt, pt2);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
359 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
360 return pt;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
361 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
362 that.invtransform = function(rect) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
363 // returns transformed Rectangle or Position with the inverse of
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
364 // this Transform applied
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
365 var det = this.m00 * this.m11 - this.m01 * this.m10;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
366 var x = (this.m11 * rect.x - this.m01 * rect.y - this.m11
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
367 * this.m02 + this.m01 * this.m12)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
368 / det;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
369 var y = (-this.m10 * rect.x + this.m00 * rect.y + this.m10
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
370 * this.m02 - this.m00 * this.m12)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
371 / det;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
372 var pt = position(x, y);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
373 if (rect.width) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
374 // transform the other corner point
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
375 var pt2 = this.invtransform(rect.getPt2());
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
376 return rectangle(pt, pt2);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
377 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
378 return pt;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
379 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
380 that.toString = function(pretty) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
381 var s = '[';
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
382 if (pretty)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
383 s += '\n';
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
384 for ( var i = 0; i < 3; ++i) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
385 s += '[';
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
386 for ( var j = 0; j < 3; ++j) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
387 if (j)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
388 s += ',';
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
389 s += this['m' + i + j];
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
390 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
391 s += ']';
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
392 if (pretty)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
393 s += '\n';
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
394 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
395 s += ']';
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
396 if (pretty)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
397 s += '\n';
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
398 return s;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
399 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
400 // add class methods to instance
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
401 that.getRotation = transform.getRotation;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
402 that.getRotationAround = transform.getRotationAround;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
403 that.getTranslation = transform.getTranslation;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
404 that.getMirror = transform.getMirror;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
405 that.getScale = transform.getScale;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
406
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
407 return that;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
408 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
409
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
410 transform.getRotation = function(angle) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
411 // returns a Transform that is a rotation by angle degrees around [0,0]
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
412 if (angle !== 0) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
413 var t = Math.PI * parseFloat(angle) / 180.0;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
414 var cost = Math.cos(t);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
415 var sint = Math.sin(t);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
416 var traf = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
417 m00 : cost,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
418 m01 : -sint,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
419 m10 : sint,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
420 m11 : cost
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
421 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
422 return transform(traf);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
423 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
424 return transform();
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
425 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
426
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
427 transform.getRotationAround = function(angle, pos) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
428 // returns a Transform that is a rotation by angle degrees around pos
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
429 var traf = transform.getTranslation(pos.neg());
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
430 traf.concat(transform.getRotation(angle));
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
431 traf.concat(transform.getTranslation(pos));
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
432 return traf;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
433 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
434
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
435 transform.getTranslation = function(pos) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
436 // returns a Transform that is a translation by [pos.x, pos,y]
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
437 var traf = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
438 m02 : pos.x,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
439 m12 : pos.y
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
440 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
441 return transform(traf);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
442 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
443
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
444 transform.getMirror = function(type) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
445 // returns a Transform that is a mirror about the axis type
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
446 if (type === 'x') {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
447 var traf = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
448 m00 : 1,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
449 m11 : -1
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
450 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
451 } else {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
452 var traf = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
453 m00 : -1,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
454 m11 : 1
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
455 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
456 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
457 return transform(traf);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
458 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
459
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
460 transform.getScale = function(size) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
461 // returns a Transform that is a scale by [size.width, size.height]
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
462 var traf = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
463 m00 : size.width,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
464 m11 : size.height
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
465 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
466 return transform(traf);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
467 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
468
786
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
469 // export constructor functions to digilib plugin
793
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
470 var geometry = {
786
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
471 size : size,
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
472 position : position,
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
473 rectangle : rectangle,
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
474 transform : transform
793
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
475 };
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
476 // install function called by digilib on plugin object
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
477 var install = function() {
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
478 // add constructor object to fn
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
479 this.fn.geometry = geometry;
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
480 };
793
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
481 // digilib plugin object
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
482 var plugin = {
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
483 name : 'geometry',
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
484 install : install,
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
485 fn : {},
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
486 // TODO: remove old init
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
487 init : init
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
488 };
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
489 // TODO: remove old version of init returning contructor
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
490 var init = function () {
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
491 return geometry;
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
492 };
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
493 // plug into digilib
786
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
494 if ($.fn.digilib == null) {
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
495 $.error("jquery.digilib.geometry must be loaded after jquery.digilib!");
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
496 } else {
793
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
497 $.fn.digilib('plugin', plugin);
786
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
498 }
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
499 })(jQuery);