[Mapbender-commits] r3122 - branches/nimix_dev/http/javascripts

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Mon Oct 6 05:04:49 EDT 2008


Author: nimix
Date: 2008-10-06 05:04:49 -0400 (Mon, 06 Oct 2008)
New Revision: 3122

Modified:
   branches/nimix_dev/http/javascripts/usemap.js
Log:
add line buffering algorithm to usemap.js

Modified: branches/nimix_dev/http/javascripts/usemap.js
===================================================================
--- branches/nimix_dev/http/javascripts/usemap.js	2008-10-06 06:30:32 UTC (rev 3121)
+++ branches/nimix_dev/http/javascripts/usemap.js	2008-10-06 09:04:49 UTC (rev 3122)
@@ -117,7 +117,101 @@
 	this.setLineAttributes = function(area, m, target){
 		//apply line shape to area
 		area.setAttribute("shape", 'poly');
-//TODO calculate buffered line	
+		if(m.count()==1)
+			return this.setPointAttributes(area,m,target);
+		
+		points = [];
+		
+		first_point= realToMap(target,m.get(0));
+		this_point = realToMap(target,m.get(1));
+		//get vector from point 0 to point 1
+		last_vec = this_point.minus(first_point);
+		//get 90° rotated vector
+		last_vec_o = new Point(-last_vec.y, last_vec.x);
+		//calculate vectors with linebuffer length
+		last_vec_o = last_vec_o.times(this.lineBuffer/last_vec_o.dist(new Point(0,0)));
+		last_vec = last_vec.times(this.lineBuffer/last_vec.dist(new Point(0,0)));
+		
+		//add first pointsets
+		points.unshift(first_point.plus(last_vec_o).minus(last_vec));
+		points.push(first_point.minus(last_vec_o).minus(last_vec));
+		
+		for(var i=1;i<m.count()-1;i++){
+			next_point = realToMap(target,m.get(i+1));
+			//get vector from point n to point n+1
+			vec = next_point.minus(this_point);
+			//get orthogonal (90° rotated) vector		
+			vec_o = new Point(-vec.y, vec.x);
+
+			//resize vectors to linebuffer length
+			vec_o = vec_o.times(this.lineBuffer/vec_o.dist(new Point(0,0)));
+			vec = vec.times(this.lineBuffer/vec.dist(new Point(0,0)));
+			
+			//if direction is the same continue
+			if(vec.equals(last_vec))
+				continue;
+			
+			// calculate angle between the two vectors by 
+			// calculating the argument diffenrences between complex numbers
+			// arg(x + i*y)  
+			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;
+			
+			//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 = this.lineBuffer/(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)));
+			
+			//look if we have a sharp corner (>90°)
+			if(Math.abs(angle)>Math.PI/2){
+				//look where we have the outer edge of corner > 90°
+				if(angle<0){
+					//angle is negative so the outer edge is "on top"
+					//push cutted edge points
+					points.push(this_point.minus(last_vec_o).plus(last_vec));
+					points.push(this_point.minus(vec_o).minus(vec));
+					//TODO look if we need the inner edge or maybe even not the last inserted point
+					//push inner edge
+					points.unshift(this_point.plus(int_vec));
+				}
+				else{
+					//angle is positive so the outer edge is "on bottom"
+					//push cutted edge points
+					points.unshift(this_point.plus(last_vec_o).plus(last_vec));
+					points.unshift(this_point.plus(vec_o).minus(vec));
+					//TODO look if we need the inner edge or maybe even not the last inserted point
+					//push inner edge
+					points.push(this_point.minus(int_vec));
+				}
+			}
+			//otherwise only calculate intersection of bufferboundary lines
+			else{
+				points.unshift(this_point.plus(int_vec));
+				points.push(this_point.minus(int_vec));
+			}
+			//copy for next point
+			last_vec = vec;
+			last_vec_o = vec_o;
+			this_point = next_point;
+		}
+		//add last pointsets
+		points.unshift(this_point.plus(last_vec_o).plus(last_vec));
+		points.push(this_point.minus(last_vec_o).plus(last_vec));
+
+		coords = [];
+		for (var i=0; i<points.length; i++) {
+			coords.push(String(parseInt(points[i].x)));
+			coords.push(String(parseInt(points[i].y)));
+		}
+		area.setAttribute("coords", coords.join(","));
 	};
 	this.setPolygonAttributes = function(area, m, target){
 		//apply polygon shape to area



More information about the Mapbender_commits mailing list