svn commit: r818 - trunk/mapbender/http/javascripts/geometry.js

christoph at osgeo.org christoph at osgeo.org
Tue Nov 14 08:30:41 EST 2006


Author: christoph
Date: 2006-11-14 13:30:41+0000
New Revision: 818

Added:
   trunk/mapbender/http/javascripts/geometry.js

Log:
new geometry class that contains everything from 

mod_highlight.php
mod_geometryArray.js

plus some more functions

Added: trunk/mapbender/http/javascripts/geometry.js
Url: https://mapbender.osgeo.org/source/browse/mapbender/trunk/mapbender/http/javascripts/geometry.js?view=auto&rev=818
==============================================================================
--- (empty file)
+++ trunk/mapbender/http/javascripts/geometry.js	2006-11-14 13:30:41+0000
@@ -0,0 +1,793 @@
+/* 
+* $Id: mod_geometryArray.js 776 2006-09-29 08:14:28Z christoph $
+* COPYRIGHT: (C) 2001 by ccgis. This program is free software under the GNU General Public
+* License (>=v2). Read the file gpl.txt that comes with Mapbender for details. 
+*/
+// http://www.mapbender.org/index.php/GeometryArray.js
+
+
+var geomTypePolygon = "polygon";
+var geomTypeLine = "line";
+var geomTypePoint = "point";
+
+var nameGeometryArray = "GeometryArray";
+var nameMultiGeometry = "MultiGeometry";
+var nameGeometry = "Geometry";
+
+// ----------------------------------------------------------------------------------------------------
+// GeometryArray
+// ----------------------------------------------------------------------------------------------------
+
+function GeometryArray(){
+
+	this.count = function(){ // public
+		return this.m.length;
+	}
+
+	this.union = function(geomArray){ // public
+		for (var i=0; i < geomArray.count(); i++) this.appendMember(geomArray.get(i));
+	}
+	
+	this.get = function(i){ // public
+		i = this.getIndex(i);
+		if (i != null) return this.m[i];
+		return false;
+	}
+
+	this.getGeometry = function(i,j){ // public
+		return this.get(i).get(j);
+	}
+
+	this.getPoint = function(i,j,k){ // public
+		return this.get(i).get(j).get(k);
+	}
+
+	this.addMember = function(geomtype){ // public
+		this.m[this.m.length] = new MultiGeometry(geomtype, -1);   
+	}
+	
+	this.appendMember = function(aMember){ // public
+		this.m[this.m.length] = cloneObject(aMember);
+	}
+
+	this.del = function(i){ // public
+		i = this.getIndex(i);
+		for(var z = i; z < this.m.length - 1; z++){
+			this.m[z] = this.m[z+1];
+		}
+		this.m.length -= 1;
+	}
+
+	this.findMultiGeometry = function(m) { // public
+		for (var i=0; i < this.count(); i++) {
+			if (this.get(i).equals(m)) return i;
+		}
+		return null;
+	}
+	
+	this.removeMultiGeometry = function(m) { // public
+		var i = true;
+		i = this.findMultiGeometry(m);
+		if (i != null) this.del(i);
+	}
+	
+	this.delGeometry = function(i,j){ // public
+		var res = this.get(i).del(j);
+		if (res == false) this.del(i);
+	}
+	
+	this.delPoint = function (i,j,k){ // public
+		var res = this.get(i).delPoint(j,k);
+		if (res == false) this.del(i);
+	}
+	
+	this.close = function(){ // public
+		this.get(-1).get(-1).close();
+		if (this.get(-1).get(-1).count() == 0) {this.get(-1).del(-1);}
+		if (this.get(-1).count() == 0) {this.del(-1);}
+	}
+
+	this.delAllPointsLike = function(point){ // public
+		var finished = false;
+		while (finished==false){
+			finished = true;
+			for (var i = 0 ; finished == true && i < this.count() ; i++){
+				for (var j = 0 ; finished == true && j < this.get(i).count() ; j++){
+					for (var k = 0 ; finished == true && k < this.get(i).get(j).count() ; k++){
+						if (this.getPoint(i,j,k).equals(point)){
+							this.delPoint(i,j,k);
+							finished = false;
+						}
+					}
+				} 
+			}
+		}
+	}
+	
+	this.updateAllPointsLike = function(oldP, newP){ // public
+		for (var i = 0; i < this.count(); i++){
+			this.get(i).updateAllPointsLike(oldP, newP);
+		}
+	}
+	
+	this.toString = function(){ // public
+		var str = "";
+		for (var i =0 ; i < this.count() ; i++){
+			str += this.get(i).toString();
+		}
+		return str;	
+	}
+
+	this.getIndex = function(i){ //private
+		if ((i >= 0 && i < this.m.length) || (i*(-1)>0 && i*(-1) <=this.m.length)){
+			if (i >= 0) return i; else return this.m.length+i;
+		}
+		else {
+			alert("exception: member index " + i + " is not valid");
+			return null;
+		}
+	}
+
+	this.geomTypePolygon = "polygon";
+	this.geomTypeLine = "line";
+	this.geomTypePoint = "point";
+	this.name = nameGeometryArray;
+	this.m = new Array();
+}
+
+// ----------------------------------------------------------------------------------------------------
+// MultiGeometry
+// ----------------------------------------------------------------------------------------------------
+
+function MultiGeometry(geomtype, wfs_conf){
+
+	this.count = function(){ // public
+		return this.g.length;
+	}
+
+	this.get = function(i){ // public
+		i = this.getIndex(i);
+		if (i != null) return this.g[i];
+		return false;
+	}
+
+	this.getPoint = function(j,k){ // public
+		return this.get(j).get(k);
+	}
+	
+	this.addGeometry = function(){ // public
+		this.g[this.g.length] = new Geometry(this.geomtype);
+	}
+	
+	this.equals = function(multigeom) { // public
+		if (this.geomtype != multigeom.geomtype) return false;
+		if (this.count() != multigeom.count()) return false;
+		if (this.getTotalPointCount() != multigeom.getTotalPointCount()) return false;
+		for (var i=0; i<this.count(); i++) {
+			if (!this.get(i).equals(multigeom.get(i))) return false;
+		}
+		return true;
+	}
+	
+	this.del = function(i){ // public
+		i = this.getIndex(i);
+		if (i != null){
+			var tmpLength = this.count() - 1;
+			for (var z = i; z < tmpLength ; z ++){
+				this.g[z] = this.g[z+1];
+				this.e[z] = this.e[z+1];
+			}
+			this.g.length -= 1;
+			if (this.g.length == 0)	return false;
+		}
+		return true;
+	}
+
+	this.delPoint = function(i,j){ // public
+		var res = this.get(i).del(j);
+		if (res == false) {return this.del(i);}
+		return true;
+	}
+
+	this.emptyMember = function(){ // public
+		this.g = new Array();
+	}
+	
+	this.updateAllPointsLike = function(oldP, newP){ // public
+		for (var i = 0; i < this.count(); i++) this.get(i).updateAllPointsLike(oldP, newP);
+	}
+
+	this.getBBox = function(){ // public
+		var q = this.get(0).get(0);
+		var min = cloneObject(q);
+		var max = cloneObject(q);
+		for(var i=0; i<this.count();i++){
+			var pos = this.get(i).getBBox();
+			if (pos[0].x < min.x) {min.x = pos[0].x;}
+			if (pos[1].x > max.x) {max.x = pos[1].x;}
+			if (pos[1].y > max.y) {max.y = pos[1].y;}
+			if (pos[0].y < min.y) {min.y = pos[0].y;}
+		}
+		return new Array(min, max);
+	}
+	
+	this.getCenter = function(){ // public
+		var tmp = this.getBBox();
+		var x = parseFloat(tmp[0].x) + parseFloat((tmp[1].x - tmp[0].x)/2);
+		var y = parseFloat(tmp[0].y) + parseFloat((tmp[1].y - tmp[0].y)/2);
+		return new Point(x,y);
+	}
+
+	this.getTotalPointCount = function(){ // public
+		var c = 0;
+		for (var i = 0 ; i < this.count(); i++)	c += this.get(i).count();
+		return c;
+	}
+
+	this.toString = function(){ // public
+		var str = "";
+		for (var i = 0 ; i < this.count(); i++)	str += this.get(i).toString();
+		return str;
+	}
+	
+	this.getIndex = function(i){ //private
+		if ((i >= 0 && i < this.g.length) || (i*(-1)>0 && i*(-1) <=this.g.length)){
+			if (i >= 0) {return i;}	else {return this.g.length+i;}
+		}
+		else {
+			alert("exception: geometry index is not valid");
+			return null;
+		}
+	}
+
+	this.g = new Array();
+	this.e = new Wfs_element();
+	this.geomtype = geomtype;
+	this.name = nameMultiGeometry;
+	this.wfs_conf = wfs_conf;
+}
+
+// ----------------------------------------------------------------------------------------------------
+// Geometry
+// ----------------------------------------------------------------------------------------------------
+
+function Geometry(aGeomtype){
+
+	this.count = function(){ // public
+		return this.v.length;
+	}
+
+	this.get = function(i){ // public
+		i = this.getIndex(i);
+		if (i != null){return this.v[i];}
+		return false;
+	}
+
+	this.getBBox = function(){ // public
+		var q = this.get(0);
+		var min = cloneObject(q);
+		var max = cloneObject(q);
+		
+		for (var j=0; j<this.count(); j++){
+			var pos = this.get(j);
+			if (pos.x < min.x) {min.x = pos.x;}
+			else if (pos.x > max.x) {max.x = pos.x;}
+			if (pos.y < min.y) {min.y = pos.y;}
+			else if (pos.y > max.y) {max.y = pos.y;}
+		}
+		return new Array(min, max);
+	}
+	
+	this.close = function(){
+		this.complete = true;
+		if (this.geomtype == geomTypePolygon){
+			if (this.count() > 0){this.addPoint(this.get(0));}
+		}
+	}
+	
+	this.addPointByCoordinates = function(x,y){ // public
+		this.v[this.v.length] = new Point(x,y);
+//		this.updateDist();
+	}
+
+	this.addPoint = function(aPoint){ // public
+		this.v[this.v.length] = new Point(aPoint.x, aPoint.y);
+//		this.updateDist();
+	}
+
+	this.addPointAtIndex = function(p,i){ // public
+		i = this.getIndex(i);
+		if (i != null){
+			for(var z = this.v.length; z > i; z--){
+				this.v[z] = this.v[z-1];
+			}
+			this.v[i] = new Point(p.x, p.y);
+//			this.updateDist();
+		}
+	}
+	
+	this.del = function(i){ // public
+		i = this.getIndex(i);
+		if (i != null) {
+			var tmpLength = this.count()-1;
+			
+			for (var z = i; z < tmpLength ; z ++){
+				this.v[z] = this.v[z+1];
+			}
+			this.v.length -= 1;
+		
+			if (this.geomtype == geomTypePolygon){
+				if (i == tmpLength) {this.v[0] = this.v[tmpLength-1];}
+				else if (i == 0) {this.v[tmpLength-1] = this.v[0];}
+				if (this.v.length == 1){return false;}
+			}
+//			this.updateDist();
+			if(this.v.length == 0) {return false;}
+			return true;
+		}
+		return false;
+	}
+	
+	this.updateGeometry = function(p, i){ // public
+		i = this.getIndex(i);
+		if ((i == 0 || i == this.count()-1) && this.geomtype == geomTypePolygon){
+			this.v[0] = p;
+			this.v[this.count()-1] = p;
+		}
+		else {this.v[i] = p;}
+//		this.updateDist();
+	}
+	
+	this.updateAllPointsLike = function(oldP, newP){ // public
+		var len = this.count();
+		for (var i = 0; i < len ; i++){
+			if (oldP.equals(this.get(i))){
+				if (i>0 && newP.equals(this.get(i-1))){
+					this.del(i);
+					len--;
+					i--;
+				}
+				else this.updateGeometry(newP, i);
+			}
+		}
+	}
+
+	this.isComplete = function() { // public
+		return this.complete;
+	}
+	
+	this.toString = function(){ // public
+		var str = "";
+		for (var i = 0 ; i < this.count(); i++)	str += this.get(i).toString();
+		return str;
+	}
+	
+	this.equals = function(geom) { // public
+		if (this.geomtype != geom.geomtype) return false;
+		if (this.count() != geom.count()) return false;
+		for (var i in this.v) {
+			if (!this.get(i).equals(geom.get(i))) return false;
+		}
+		return true;
+	}
+	
+	this.getIndex = function(i){ //private
+		if ((i >= 0 && i < this.v.length) || (i*(-1)>0 && i*(-1) <=this.v.length)){
+			if (i >= 0) {return i;}	else {return this.v.length+i;}
+		}
+		else {
+			alert("exception: point index is not valid");
+			return null;
+		}
+	}
+
+	this.updateDist = function(){ //private
+		this.dist[0] = 0;		
+		this.totaldist[0] = 0;		
+		for (var i = 1 ; i < this.count(); i++){
+			this.dist[i] = this.get(i-1).dist(this.get(i));
+			this.totaldist[i] = this.totaldist[i-1] + this.dist[i];
+		}
+	}
+	
+	this.geomtype = aGeomtype;
+	this.complete = false;
+	this.v = new Array();
+	this.dist = new Array();
+	this.totaldist = new Array();
+	this.name = nameGeometry;
+}
+
+// ----------------------------------------------------------------------------------------------------
+// Wfs_element (FIXME)
+// ----------------------------------------------------------------------------------------------------
+
+function Wfs_element(){
+
+	this.count = function(){ // public
+		return this.name.length;
+	}
+
+	this.getElementValueByName = function(elementName){ // public
+		var i = this.getElementIndexByName(elementName);
+		if (i == -1) {return false;}
+		return this.value[i];
+	}
+
+	this.getName = function(i){ // public
+		if (this.isValidElementIndex(i)) {return this.name[i];}
+		return false;
+	}
+	
+	this.getValue = function(i){ // public
+		if (this.isValidElementIndex(i)) {return this.value[i];}
+		return false;
+	}
+
+	this.setElement = function(aName, aValue){ // public
+		var i = this.getElementIndexByName(aName);
+		if (i == -1) i = this.name.length;
+		this.name[i] = aName;
+		this.value[i] = aValue;
+		return true;
+	}
+
+	this.getElementIndexByName = function(elementName){ // private
+		var arrayLength = this.name.length;
+		for (var j = 0 ; j < arrayLength ; j++){
+			if (this.name[j] == elementName) {return j;}
+		}
+		return -1;
+	}
+
+	this.isValidElementIndex = function(i){ //private
+		if (i>=0 && i<this.name.length) {return true;}
+		alert("exception: illegal element index");
+		return false;
+	}
+	
+	this.name  = new Array();
+	this.value = new Array();
+}
+
+// ----------------------------------------------------------------------------------------------------
+// Canvas
+// ----------------------------------------------------------------------------------------------------
+
+
+function Canvas(mapframe, tagname) {
+	
+	this.paint = function(gA) { // public
+		if (this.checkTag()) {
+			for (var q = 0; q < gA.count(); q++) {
+				var m = gA.get(q);
+				var t = m.geomtype;
+				if (t == geomTypePoint)	this.drawGeometry(t,m);
+				else {
+					if (this.isTooSmall(m)){
+						var newMember = new MultiGeometry(geomTypePoint);
+						newMember.addGeometry();
+						newMember.get(-1).addPoint(m.getCenter());
+						this.drawGeometry(geomTypePoint,newMember);
+					}
+					else{
+						if(t == geomTypeLine) this.drawGeometry(t,m);
+						else if(t == geomTypePolygon) this.drawGeometry(t,m);
+						else alert("unknown geomtype");
+					}
+				}
+			}
+			this.canvas.paint();
+		}
+	}
+	
+	this.drawGeometry = function(t,g){ // private
+		var mapObjInd = getMapObjIndexByName(this.mapframe);
+		width = mb_mapObj[mapObjInd].width;
+		height = mb_mapObj[mapObjInd].height;
+		for(var i=0; i < g.count(); i++){
+			if(t==geomTypePoint) {
+				var p = realToMap(this.mapframe,g.get(i).get(0));
+				if (p.x + this.diameter < mb_mapObj[mapObjInd].width && p.x - this.diameter > 0 &&
+					p.y + this.diameter < mb_mapObj[mapObjInd].height && p.y - this.diameter > 0) {
+					this.drawCircle(p.x-1,p.y-1,this.diameter,this.lineColor);
+				}
+			}
+			else if(t==geomTypeLine) {
+				for (var j=0; j<g.get(i).count()-1; j++) {
+					var pq = calculateVisibleDash(realToMap(this.mapframe,g.get(i).get(j)), realToMap(this.mapframe,g.get(i).get(j+1)), width, height);
+					if (pq) {
+						this.drawLine(new Array(pq[0].x, pq[1].x),new Array(pq[0].y, pq[1].y),this.lineColor);
+					}
+				}
+			}
+			else if(t==geomTypePolygon) {
+				for (var j=0; j<g.get(i).count()-1; j++) {
+					var pq = calculateVisibleDash(realToMap(this.mapframe,g.get(i).get(j)), realToMap(this.mapframe,g.get(i).get(j+1)), width, height);
+					if (pq) {
+						this.drawLine(new Array(pq[0].x, pq[1].x),new Array(pq[0].y, pq[1].y),this.lineColor);
+					}
+				}
+			}
+			else {
+				alert('unknown geomtype ' + t);
+			}
+		}
+		var el = window.frames[this.mapframe].document.getElementById(this.tagname);
+	}
+	
+	this.isTooSmall = function(g){ // private
+		var tmp = g.getBBox();
+		var min = realToMap(this.mapframe,tmp[0]);
+		var max = realToMap(this.mapframe,tmp[1]);
+		if((max.x - min.x < this.minWidth) && (max.y - min.y < this.minWidth)) return true;
+		return false;
+	}
+	
+	this.clean = function () { // private
+		var el = window.frames[this.mapframe].document.getElementById(this.tagname);
+		if (el) el.innerHTML = "";
+	}
+
+	this.drawCircle = function(x, y, diameter, color) { // private
+		this.canvas.setColor(color);
+		this.canvas.drawEllipse(x-diameter/2,y-diameter/2,diameter,diameter);
+	}
+
+	this.drawLine = function(x_array, y_array, color) { // private
+		this.canvas.setColor(color);
+		this.canvas.drawPolyline(x_array, y_array);
+	}
+
+	this.drawPolygon = function(x_array, y_array, color) { // private
+		this.canvas.setColor(color);
+		this.canvas.drawPolygon(x_array, y_array);
+	}
+
+	this.checkTag = function () { // private
+		if (typeof(window.frames[this.mapframe]) != 'undefined') {
+			var isTag = (window.frames[this.mapframe].document.getElementById(this.tagname))?1:0;
+			if(isTag == 0){
+				var draw = window.frames[this.mapframe].document.createElement("div");
+				var tmp = window.frames[this.mapframe].document.getElementsByTagName("body")[0].appendChild(draw);
+				tmp.setAttribute("id",this.tagname);
+				tmp.setAttribute("style","position:absolute;top:0px;left:0px;width:0px;height:0px;z-index:100");
+			}
+			else this.clean();
+			return true;
+		}
+		else {
+			alert('invalid mapframe ' + this.mapframe);
+		}
+		return false;
+	}
+	this.mapframe = mapframe;
+	this.tagname = tagname;
+	if (this.checkTag()) {
+		this.canvas = new jsGraphics(this.tagname, window.frames[this.mapframe]);
+		this.canvas.setStroke(3);
+	}
+	this.diameter = 8;
+	this.minWidth = 8;
+	this.lineColor = "#ff0000";
+}
+
+// ----------------------------------------------------------------------------------------------------
+// Highlight
+// ----------------------------------------------------------------------------------------------------
+
+function Highlight(target_array, tagname) {
+	this.del = function(m) { // public
+		this.gA.removeMultiGeometry(m);
+		this.paint();
+	}
+
+	this.add = function(m) { // public
+		this.gA.appendMember(m);
+		this.paint();
+	}
+	
+	this.clean = function() { // public
+		this.gA = new GeometryArray();
+		this.paint();
+	}
+
+	this.paint = function() { // public
+		for (q in this.canvas) this.canvas[q].clean();
+		for(var i=0; i<this.targets.length; i++){
+			if (typeof(this.canvas[this.targets[i]]) == 'undefined') {
+				this.canvas[this.targets[i]] = new Canvas(this.targets[i], this.tagname);
+			}
+			this.canvas[this.targets[i]].paint(this.gA);
+		}
+	}
+
+	this.tagname = 'mod_gaz_draw'+tagname; // private
+	this.targets = target_array; // private
+	this.canvas = new Array(); // private
+	this.gA = new GeometryArray(); // private
+
+	mb_registerPanSubElement(this.tagname);
+	this.paint();
+}
+
+// ----------------------------------------------------------------------------------------------------
+// Snapping
+// ----------------------------------------------------------------------------------------------------
+
+function Snapping(aTarget, tolerance, color){
+
+	this.check = function(currPoint){ // public
+		var minDist = false;
+		for (var i = 0 ; i < this.coord.length ; i++) {
+			var aDist = currPoint.dist(realToMap(this.target, this.coord[i]['coord']));
+			if (minDist == false || aDist < minDist) {
+				minDist = aDist;
+				if (minDist < this.tolerance) {this.min_i = i;}
+			}
+		}
+		if (this.coord.length>0 && minDist > this.tolerance) {this.min_i = -1;}
+		this.clean();
+		if (this.min_i != -1) this.draw(this.coord[this.min_i]['coord'], this.tolerance, 3);
+	}
+
+	this.store = function(geom, point){ // public
+		this.coord = new Array();
+		this.min_i = -1;
+	
+		for (var i = 0 ; i < geom.count(); i++){
+			if (geom.name == nameGeometryArray || geom.name == nameMultiGeometry){
+				for (var j = 0 ; j < geom.get(i).count() ; j++){
+					if (geom.get(i).name == nameMultiGeometry){
+						for (var k = 0 ; k < geom.get(i).get(j).count() ; k++){
+							if ((geom.get(i).get(j).isComplete() == true && typeof(point) == 'undefined') || (typeof(point) != 'undefined' && !geom.get(i).get(j).get(k).equals(point))){
+								this.add(geom, k, j, i);
+							}
+						}
+					}
+					else {
+						if ((geom.get(i).get(j).isComplete() == true && typeof(point) == 'undefined') || (typeof(point) != 'undefined' && !geom.get(i).get(j).get(k).equals(point))){
+							this.add(geom, j, i);
+						}
+					}
+				}
+			}
+			else {
+				if ((geom.get(i).get(j).isComplete() == true && typeof(point) == 'undefined') || (typeof(point) != 'undefined' && !geom.get(i).get(j).get(k).equals(point))){
+					this.add(geom, i);
+				}
+			}
+		}
+	}
+	
+	this.isSnapped = function(){ // private
+		if (this.min_i != -1) {return true;}
+		return false;
+	}
+	
+	this.getSnappedPoint = function(geom){ // private
+		var point;
+		var i = this.coord[this.min_i]['member'];
+		var j = this.coord[this.min_i]['geometry'];
+		var k = this.coord[this.min_i]['point']; 
+		if (geom.name == nameGeometryArray) {point = geom.getPoint(i,j,k);}
+		else if (geom.name == nameMultiGeometry) {point = geom.getPoint(j,k);}
+		else if (geom.name == nameGeometry) {point = geom.get(k);}
+		return point;
+	}
+	
+	this.add = function(geom, k, j, i){ // private
+		var cnt = this.coord.length;
+		this.coord[cnt] = new Array();
+
+		if (typeof(j) != 'undefined'){
+			if (typeof(i) != 'undefined') this.coord[cnt]['coord'] = geom.getPoint(i,j,k);
+			else this.coord[cnt]['coord'] = geom.getPoint(j,k);
+		}
+		else this.coord[cnt]['coord'] = geom.get(k);
+		
+		this.coord[cnt]['member'] = i;
+		this.coord[cnt]['geometry'] = j;
+		this.coord[cnt]['point'] = k;
+	}
+
+	this.draw = function(center,radius,size){ //private
+		mG = new MultiGeometry(geomTypePoint);
+		mG.addGeometry();
+		mG.get(-1).addPoint(center);
+		this.highlight.add(mG);
+	}
+	
+	this.clean = function(){ // private
+		this.highlight.clean();
+	}
+
+	this.tolerance = (typeof(tolerance) == 'undefined') ? 10 : tolerance;
+	this.target = aTarget;
+	this.coord = new Array(); // private
+	this.min_i = -1; //private
+	this.highlight = new Highlight(new Array(this.target), "snapping"+Math.round(Math.random()*Math.pow(10,10)));
+}
+
+// ----------------------------------------------------------------------------------------------------
+// misc. functions
+// ----------------------------------------------------------------------------------------------------
+
+function calculateVisibleDash (p0, p1, width, height) {
+	if (p0.x > p1.x) {var p_temp = p0; p0 = p1; p1 = p_temp; p_temp = null;}
+	var p = p0; var q = p1; var m; 
+	if (p1.x != p0.x) {
+		m = -(p1.y-p0.y)/(p1.x-p0.x); 
+		if (p0.x < width && p1.x > 0 && !(p0.y < 0 && p1.y < 0) && !(p0.y > height && p1.y > height) ) {
+			if (p0.x < 0) {
+				var iy = p0.y - m*(0-p0.x);
+				if (iy > 0 && iy < height) p = new Point(0, iy);
+				else if (iy > height) {
+				    var ix = p0.x+((p0.y - height)/m);
+				    if (ix > 0 && ix < width) p = new Point(ix, height); else return false;
+				}
+				else if (iy < 0) {
+				    var ix = p0.x+(p0.y/m);
+				    if (ix > 0 && ix < width) p = new Point(ix, 0); else return false;
+				}
+				else return false;
+			}
+			else if (p0.y >= 0 && p0.y <= height) {p = p0;}
+			else if (p0.y < 0) {
+			    var ix = p0.x+(p0.y/m);
+			    if (ix > 0 && ix < width) p = new Point(ix, 0); else return false;
+			}
+			else if (p0.y > height && m > 0) {
+			    var ix = p0.x+((p0.y - height)/m);
+			    if (ix > 0 && ix < width) p = new Point(ix, height); else return false;
+			}
+			else return false;
+			if (p1.x > width) {
+				var iy = p1.y - m*(width-p1.x);
+				if (iy > 0 && iy < height) {q = new Point(width, iy);}
+				else if (iy < 0) {
+				    var ix = p0.x+(p0.y/m);
+				    if (ix > 0 && ix < width) q = new Point(ix, 0); else return false;
+				}
+				else if (iy > height) {
+				    var ix = p0.x+((p0.y - height)/m);
+				    if (ix > 0 && ix < width) q = new Point(ix, height); else return false;
+				}
+				else return false;
+			}
+			else if (p1.y >= 0 && p1.y <= height) {q = p1;}
+			else if (p1.y < 0) {
+			    var ix = p1.x+(p1.y/m);
+			    if (ix > 0 && ix < width) q = new Point(ix, 0); else return false;
+			}
+			else if (p1.y > height) {
+			    var ix = p1.x+((p1.y- height)/m);
+			    if (ix > 0 && ix < width) q = new Point(ix, height); else return false;
+			}
+		}
+		else return false;
+	}
+	else {
+		if (!(p0.y < 0 && p1.y < 0) && !(p0.y > height && p1.y > height)) {
+			if (p0.y < 0) {p = new Point(p0.x, 0);}
+			else if (p0.y > height) {p = new Point(p0.x, height);}
+			else p = p0;
+			if (p1.y < 0) {q = new Point(p0.x, 0);}
+			else if (p1.y > height) {q = new Point(p0.x, height);}
+			else q = p1;
+		}
+		else return false;
+	}
+	return new Array(new Point(Math.round(q.x), Math.round(q.y)), new Point(Math.round(p.x), Math.round(p.y)));
+}
+
+function cloneObject(a){
+	var z = new Array();
+	
+	for (attr in a) {
+		var b = a[attr];
+		if (typeof(b) == "object") {z[attr] = cloneObject(b);}
+		else z[attr] = b;
+	}	
+	return z;
+}




More information about the Mapbender_commits mailing list