[Mapbender-commits] r3135 - trunk/mapbender/http/javascripts
svn_mapbender at osgeo.org
svn_mapbender at osgeo.org
Tue Oct 7 03:47:52 EDT 2008
Author: nimix
Date: 2008-10-07 03:47:52 -0400 (Tue, 07 Oct 2008)
New Revision: 3135
Modified:
trunk/mapbender/http/javascripts/geometry.js
Log:
add line buffering function
Modified: trunk/mapbender/http/javascripts/geometry.js
===================================================================
--- trunk/mapbender/http/javascripts/geometry.js 2008-10-07 07:47:26 UTC (rev 3134)
+++ trunk/mapbender/http/javascripts/geometry.js 2008-10-07 07:47:52 UTC (rev 3135)
@@ -1049,6 +1049,111 @@
return true;
};
+/**
+ * creates a polygon geometry object which form a buffer around the line geometry
+ *
+ * @param {float} real world units to buffer around the line
+ * @param {float} (optional) units to buffer around the line in Y direction
+ *
+ * @return linebuffer polygon
+ * @type Geometry
+ */
+Geometry.prototype.bufferLine = function(bufferX, bufferY){
+ if(typeof(bufferY)=='undefined')
+ bufferY = bufferX;
+ if(this.geomType!=geomType.line || this.count()<2)
+ return false;
+
+ var ret = new Geometry(geomType.polygon)
+
+ //get vector from point 0 to point 1
+ last_vec = this.get(1).minus(this.get(0));
+
+ //get 90° rotated vector
+ last_vec_o = new Point(-last_vec.y, last_vec.x);
+
+ //resize vectors with apropriate linebuffer length
+ last_vec_o = last_vec_o.dividedBy(last_vec_o.dist(new Point(0,0)));
+ last_vec_o.x*=bufferX; last_vec_o.y*=bufferY;
+ last_vec = last_vec.dividedBy(last_vec.dist(new Point(0,0)));
+ last_vec.x*=bufferX; last_vec.y*=bufferY;
+
+ //add first pointsets
+ ret.list.unshift(this.get(0).plus(last_vec_o).minus(last_vec));
+ ret.list.push(this.get(0).minus(last_vec_o).minus(last_vec));
+
+ for(var i=1;i<this.count()-1;i++){
+ //get vector from point n to point n+1
+ vec = this.get(i+1).minus(this.get(i));
+ //get orthogonal (90° rotated) vector
+ vec_o = new Point(-vec.y, vec.x);
+
+ //resize vectors to linebuffer length
+ vec_o = vec_o.dividedBy(vec_o.dist(new Point(0,0)));
+ vec_o.x*=bufferX; vec_o.y*=bufferY;
+ vec = vec.dividedBy(vec.dist(new Point(0,0)));
+ vec.x*=bufferX; vec.y*=bufferY;
+
+ //if direction is the same continue
+ if(vec.equals(last_vec))
+ continue;
+
+ // calculate directed angle between the two vectors by
+ // calculating the argument diffenrences between complex numbers
+ // arg(x + i*y) (because law of cosine can onlycalculate undirected angle)
+ var angle = (Math.atan2(vec.x,vec.y)-Math.atan2(last_vec.x,last_vec.y))
+ //ensure that angle is -180<=angle<=180
+ if(angle<-Math.PI)angle=2*Math.PI+angle;
+ if(angle>+Math.PI)angle=2*Math.PI-angle;
+ console.log(angle);
+
+ //calculate the distance between the next points on boundary
+ //and the line point
+ //the point will be in the direction of angle/2 relative to last_vec_o
+ //since cosine is adjacent side / hypothenuse and we know that
+ //the adjacent side is lineBuffer the hypothenus (our distance) is
+ var ndist = 1/(Math.cos(angle/2))
+ //direction of next points on boundary
+ var int_vec = vec_o.plus(last_vec_o);
+ //resize direction vector to our distance
+ int_vec = int_vec.times(ndist/int_vec.dist(new Point(0,0)));
+ int_vec.x*=bufferX; int_vec.y*=bufferY;
+
+ //look if we have an outer sharp corner (>90°)
+ if(angle>Math.PI/2){
+ //push cutted edge points
+ ret.list.unshift(this.get(i).plus(last_vec_o).plus(last_vec));
+ ret.list.unshift(this.get(i).plus(vec_o).minus(vec));
+ }
+ else{
+ //push inner/light edge
+ ret.list.unshift(this.get(i).plus(int_vec));
+ }
+
+ //look if we have an inner sharp corner (<-90°)
+ if(angle<-Math.PI/2){
+ //push cutted edge points
+ ret.list.push(this.get(i).minus(last_vec_o).plus(last_vec));
+ ret.list.push(this.get(i).minus(vec_o).minus(vec));
+ }
+ else{
+ //push inner/light edge
+ ret.list.push(this.get(i).minus(int_vec));
+ }
+
+ //copy for next point
+ last_vec = vec;
+ last_vec_o = vec_o;
+ }
+ //add last pointsets
+ ret.list.unshift(this.get(i).plus(last_vec_o).plus(last_vec));
+ ret.list.push(this.get(i).minus(last_vec_o).plus(last_vec));
+
+ ret.close();
+
+ return ret;
+}
+
Geometry.prototype.toString = function () {
var str = "";
@@ -1087,8 +1192,6 @@
return str;
};
-
-
/**
* @class an array of elements, each consisting of a name/value pair
*
More information about the Mapbender_commits
mailing list