annotate client/digitallibrary/jquery/jquery.digilib.geometry.js @ 877:971b7122930f jquery

arrows: got basic functionality working
author hertzhaft
date Thu, 17 Mar 2011 16:43:59 +0100
parents c662bf335881
children
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 };
877
971b7122930f arrows: got basic functionality working
hertzhaft
parents: 847
diff changeset
188 // adds pos to the dimensions
971b7122930f arrows: got basic functionality working
hertzhaft
parents: 847
diff changeset
189 that.enlarge = function(pos) {
971b7122930f arrows: got basic functionality working
hertzhaft
parents: 847
diff changeset
190 this.width += pos.x;
971b7122930f arrows: got basic functionality working
hertzhaft
parents: 847
diff changeset
191 this.height += pos.y;
971b7122930f arrows: got basic functionality working
hertzhaft
parents: 847
diff changeset
192 return this;
971b7122930f arrows: got basic functionality working
hertzhaft
parents: 847
diff changeset
193 };
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
194 // sets the lower right corner to position pos
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
195 that.setPt2 = function(pos) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
196 this.width = pos.x - this.x;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
197 this.height = pos.y - this.y;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
198 return this;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
199 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
200 // returns the center position of this Rectangle
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
201 that.getCenter = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
202 return position({
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
203 x : this.x + this.width / 2,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
204 y : this.y + this.height / 2
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
205 });
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 // moves this rectangle's center to position pos
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
208 that.setCenter = function(pos) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
209 this.x = pos.x - this.width / 2;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
210 this.y = pos.y - this.height / 2;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
211 return this;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
212 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
213 // returns true if both rectangles have equal position and proportion
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
214 that.equals = function(other) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
215 var eq = (this.x === other.x && this.y === other.y && this.width === other.width);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
216 return eq;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
217 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
218 // returns the area of this Rectangle
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
219 that.getArea = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
220 return (this.width * this.height);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
221 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
222 // eliminates negative width and height
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
223 that.normalize = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
224 var p = this.getPt2();
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
225 this.x = Math.min(this.x, p.x);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
226 this.y = Math.min(this.y, p.y);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
227 this.width = Math.abs(this.width);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
228 this.height = Math.abs(this.height);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
229 return this;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
230 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
231 // returns if Position "pos" lies inside of this rectangle
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
232 that.containsPosition = function(pos) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
233 var ct = ((pos.x >= this.x) && (pos.y >= this.y)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
234 && (pos.x <= this.x + this.width) && (pos.y <= this.y
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
235 + this.height));
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
236 return ct;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
237 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
238 // returns true if rectangle "rect" is contained in this rectangle
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
239 that.containsRect = function(rect) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
240 return (this.containsPosition(rect.getPt1()) && this
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
241 .containsPosition(rect.getPt2()));
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
242 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
243 // returns true if rectangle "rect" and this rectangle overlap
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
244 that.overlapsRect = function(rect) {
812
b484631f37c1 better intersect function
hertzhaft
parents: 807
diff changeset
245 return this.intersect(rect) != null;
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
246 };
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
247 // 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
248 // "rect", keeping the proportions
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
249 that.stayInside = function(rect) {
812
b484631f37c1 better intersect function
hertzhaft
parents: 807
diff changeset
250 this.x = Math.max(this.x, rect.x);
b484631f37c1 better intersect function
hertzhaft
parents: 807
diff changeset
251 this.y = Math.max(this.y, rect.y);
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
252 if (this.x + this.width > rect.x + rect.width) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
253 this.x = rect.x + rect.width - this.width;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
254 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
255 if (this.y + this.height > rect.y + rect.height) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
256 this.y = rect.y + rect.height - this.height;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
257 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
258 return this;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
259 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
260 // clips this rectangle so it stays inside of rectangle "rect"
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
261 that.clipTo = function(rect) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
262 var p1 = rect.getPt1();
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
263 var p2 = rect.getPt2();
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
264 var this2 = this.getPt2();
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
265 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
266 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
267 return this;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
268 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
269 // returns the intersection of rectangle "rect" and this one
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
270 that.intersect = function(rect) {
847
c662bf335881 fixed a bug in geom.intersect
hertzhaft
parents: 812
diff changeset
271 var r = rect.copy();
c662bf335881 fixed a bug in geom.intersect
hertzhaft
parents: 812
diff changeset
272 var result = r.clipTo(this);
c662bf335881 fixed a bug in geom.intersect
hertzhaft
parents: 812
diff changeset
273 if (result.width < 0 || result.height < 0) result = null;
c662bf335881 fixed a bug in geom.intersect
hertzhaft
parents: 812
diff changeset
274 return result;
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
275 };
812
b484631f37c1 better intersect function
hertzhaft
parents: 807
diff changeset
276
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
277 // returns a copy of rectangle "rect" that fits into this one
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
278 // (moving it first)
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
279 that.fit = function(rect) {
847
c662bf335881 fixed a bug in geom.intersect
hertzhaft
parents: 812
diff changeset
280 var r = rect.copy();
c662bf335881 fixed a bug in geom.intersect
hertzhaft
parents: 812
diff changeset
281 r.x = Math.max(r.x, this.x);
c662bf335881 fixed a bug in geom.intersect
hertzhaft
parents: 812
diff changeset
282 r.y = Math.max(r.y, this.x);
c662bf335881 fixed a bug in geom.intersect
hertzhaft
parents: 812
diff changeset
283 if (r.x + r.width > this.x + this.width) {
c662bf335881 fixed a bug in geom.intersect
hertzhaft
parents: 812
diff changeset
284 r.x = this.x + this.width - r.width;
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
285 }
847
c662bf335881 fixed a bug in geom.intersect
hertzhaft
parents: 812
diff changeset
286 if (r.y + r.height > this.y + this.height) {
c662bf335881 fixed a bug in geom.intersect
hertzhaft
parents: 812
diff changeset
287 r.y = this.y + this.height - r.height;
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
288 }
847
c662bf335881 fixed a bug in geom.intersect
hertzhaft
parents: 812
diff changeset
289 return r.intersect(this);
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
290 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
291 // adjusts position and size of jQuery element "$elem" to this rectangle
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
292 that.adjustDiv = function($elem) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
293 $elem.offset({
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
294 left : this.x,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
295 top : this.y
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
296 });
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
297 $elem.width(this.width).height(this.height);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
298 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
299 // returns position and size of this rectangle in css-compatible format
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
300 that.getAsCss = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
301 return {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
302 left : this.x,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
303 top : this.y,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
304 width : this.width,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
305 height : this.height
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
306 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
307 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
308 // returns position and size of this rectangle formatted for SVG attributes
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
309 that.getAsSvg = function() {
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
310 return [this.x, this.y, this.width, this.height].join(" ");
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
311 };
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
312 // 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
313 that.toString = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
314 return this.width + "x" + this.height + "@" + this.x + "," + this.y;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
315 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
316 return that;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
317 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
318
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
319 /*
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
320 * Transform class
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
321 *
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
322 * defines a class of affine transformations
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
323 */
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
324 var transform = function(spec) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
325 var that = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
326 m00 : 1.0,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
327 m01 : 0.0,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
328 m02 : 0.0,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
329 m10 : 0.0,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
330 m11 : 1.0,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
331 m12 : 0.0,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
332 m20 : 0.0,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
333 m21 : 0.0,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
334 m22 : 1.0
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
335 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
336 if (spec) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
337 jQuery.extend(that, spec);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
338 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
339 ;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
340 that.concat = function(trafA) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
341 // add Transform trafA to this Transform (i.e. this = trafC = trafA
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
342 // * this)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
343 var trafC = {};
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
344 for ( var i = 0; i < 3; i++) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
345 for ( var j = 0; j < 3; j++) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
346 var c = 0.0;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
347 for ( var k = 0; k < 3; k++) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
348 c += trafA["m" + i + k] * this["m" + k + j];
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
349 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
350 trafC["m" + i + j] = c;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
351 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
352 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
353 jQuery.extend(this, trafC);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
354 return this;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
355 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
356 that.transform = function(rect) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
357 // returns transformed Rectangle or Position with this Transform
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
358 // applied
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
359 var x = this.m00 * rect.x + this.m01 * rect.y + this.m02;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
360 var y = this.m10 * rect.x + this.m11 * rect.y + this.m12;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
361 var pt = position(x, y);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
362 if (rect.width) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
363 // transform the other corner point
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
364 var pt2 = this.transform(rect.getPt2());
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
365 return rectangle(pt, pt2);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
366 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
367 return pt;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
368 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
369 that.invtransform = function(rect) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
370 // returns transformed Rectangle or Position with the inverse of
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
371 // this Transform applied
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
372 var det = this.m00 * this.m11 - this.m01 * this.m10;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
373 var x = (this.m11 * rect.x - this.m01 * rect.y - this.m11
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
374 * this.m02 + this.m01 * this.m12)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
375 / det;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
376 var y = (-this.m10 * rect.x + this.m00 * rect.y + this.m10
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
377 * this.m02 - this.m00 * this.m12)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
378 / det;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
379 var pt = position(x, y);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
380 if (rect.width) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
381 // transform the other corner point
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
382 var pt2 = this.invtransform(rect.getPt2());
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
383 return rectangle(pt, pt2);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
384 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
385 return pt;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
386 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
387 that.toString = function(pretty) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
388 var s = '[';
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
389 if (pretty)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
390 s += '\n';
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
391 for ( var i = 0; i < 3; ++i) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
392 s += '[';
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
393 for ( var j = 0; j < 3; ++j) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
394 if (j)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
395 s += ',';
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
396 s += this['m' + i + j];
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
397 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
398 s += ']';
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
399 if (pretty)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
400 s += '\n';
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
401 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
402 s += ']';
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
403 if (pretty)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
404 s += '\n';
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
405 return s;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
406 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
407 // add class methods to instance
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
408 that.getRotation = transform.getRotation;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
409 that.getRotationAround = transform.getRotationAround;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
410 that.getTranslation = transform.getTranslation;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
411 that.getMirror = transform.getMirror;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
412 that.getScale = transform.getScale;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
413
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
414 return that;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
415 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
416
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
417 transform.getRotation = function(angle) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
418 // returns a Transform that is a rotation by angle degrees around [0,0]
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
419 if (angle !== 0) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
420 var t = Math.PI * parseFloat(angle) / 180.0;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
421 var cost = Math.cos(t);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
422 var sint = Math.sin(t);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
423 var traf = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
424 m00 : cost,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
425 m01 : -sint,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
426 m10 : sint,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
427 m11 : cost
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
428 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
429 return transform(traf);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
430 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
431 return transform();
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
432 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
433
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
434 transform.getRotationAround = function(angle, pos) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
435 // returns a Transform that is a rotation by angle degrees around pos
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
436 var traf = transform.getTranslation(pos.neg());
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
437 traf.concat(transform.getRotation(angle));
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
438 traf.concat(transform.getTranslation(pos));
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
439 return traf;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
440 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
441
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
442 transform.getTranslation = function(pos) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
443 // returns a Transform that is a translation by [pos.x, pos,y]
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
444 var traf = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
445 m02 : pos.x,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
446 m12 : pos.y
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
447 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
448 return transform(traf);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
449 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
450
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
451 transform.getMirror = function(type) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
452 // returns a Transform that is a mirror about the axis type
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
453 if (type === 'x') {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
454 var traf = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
455 m00 : 1,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
456 m11 : -1
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
457 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
458 } else {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
459 var traf = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
460 m00 : -1,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
461 m11 : 1
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
462 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
463 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
464 return transform(traf);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
465 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
466
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
467 transform.getScale = function(size) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
468 // returns a Transform that is a scale by [size.width, size.height]
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
469 var traf = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
470 m00 : size.width,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
471 m11 : size.height
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
472 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
473 return transform(traf);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
474 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
475
786
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
476 // export constructor functions to digilib plugin
793
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
477 var geometry = {
786
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
478 size : size,
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
479 position : position,
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
480 rectangle : rectangle,
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
481 transform : transform
793
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
482 };
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
483 // install function called by digilib on plugin object
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
484 var install = function() {
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
485 // add constructor object to fn
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
486 this.fn.geometry = geometry;
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
487 };
793
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
488 // digilib plugin object
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
489 var plugin = {
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
490 name : 'geometry',
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
491 install : install,
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
492 fn : {},
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
493 // TODO: remove old init
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
494 init : init
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
495 };
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
496 // TODO: remove old version of init returning contructor
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
497 var init = function () {
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
498 return geometry;
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
499 };
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
500 // plug into digilib
786
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
501 if ($.fn.digilib == null) {
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
502 $.error("jquery.digilib.geometry must be loaded after jquery.digilib!");
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
503 } else {
793
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
504 $.fn.digilib('plugin', plugin);
786
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
505 }
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
506 })(jQuery);