⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 georectangle.java

📁 geotools的源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * @(#)GeoRectangle.java  11 October 1999 James Macgill
 *
 */
package uk.ac.leeds.ccg.geotools;

import java.awt.*;
import java.lang.Math;
import java.io.*;
import java.util.*;

/**
 * A double presision rectangle.<p>
 * It is usful for describing a GeoReferanced Rectangle
 * Used by GeoPolygon to return a bounding box.
 *
 * @version 0.50, 17 Apr 1997
 * @author James Macgill
 */

public class GeoRectangle extends GeoShape implements Serializable,Cloneable {

	private final static boolean DEBUG=true;
	private boolean initialised = false;
	public double height;
	public double width;
	public double x;
	public double y;

	public boolean equals(GeoRectangle r){
		if(r==null) return false;
		//System.out.println("x "+(x==r.x));
		//System.out.println("y "+(y==r.y));
		//System.out.println("width "+(width==r.width));
		//System.out.println("height "+(height==r.height));
		return(x==r.x&&y==r.y&&width==r.width&&height==r.height);
	}
	/**
	 * Initalise Rectangle to be infinatly 'inside-out', very useful for bounding box creation
	 */
 	public GeoRectangle() {
		height = 0;
		width = 0;
		x = Double.POSITIVE_INFINITY;
		y = Double.POSITIVE_INFINITY;
		initialised = false;
	}

	/**
	 * Initalise GeoRectangle with another one.
	 *
	 * @param rect Existing GeoRectangle to initalise new one
	 */
	public GeoRectangle(GeoRectangle rect) {
		this.x = rect.x;
		this.y = rect.y;
		this.width = rect.width;
		this.height = rect.height;
		initialised = true;
	}

	/**
	 * Initalise Rectangle with a list of points
	 *
	 * @param x left most coordinate of GeoRectangle
	 * @param y bottom most coordinate of GeoRectangle
	 * @param width Width of GeoRectangle
	 * @param height Height of GeoRectangle
	 */
	public GeoRectangle(double x,double y,double width,double height) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		initialised = true;
	}

    /**
	 * Initalise Rectangle with a single point
	 *
	 * @param p A GeoPoint
	 */
	public GeoRectangle(GeoPoint p) {
		this(p.x,p.y,0d,0d);
	}

	/**
	* Compares a point with the rectangle, if it is outside then the rectangle is expanded to
	* to fit.
	*
	* @param px X coordinate of point to fit into GeoRectangle
	* @param py Y coordinate of point to fit into GeoRectangle
	*/
	public void add(double px,double py) {
		if(initialised){
		    if (px < x){
		        width = (x+width)-px;
		        x = px;
	    	}
		    if (py < y){
		        height = height + Math.abs(py-y);
		        y = py;
	    	}
		    if (px > x + width) width = px-x;
		    if (py > y + height) height = py-y;
		}
		else{
		    x=px;
		    y=py;
		    initialised = true;
		}
	}

	/** determines if this georectangle is empty or not
	 */
	public boolean isEmpty(){
		return(width==0||height==0);
	}

	/**
	* Compares a point with the rectangle, if it is outside then the rectangle is expanded to
	* to fit.
	*
	* @param p GeoPoint  to fit into GeoRectangle
	*/
	public void add(GeoPoint p) {
	    add(p.x,p.y);
	}
	
	public Object clone() {
	    return new GeoRectangle(this);
	}
	
	/**
	 * Expands the rectangle so that it is large enough to hold the
	 * specified GeoRectangle
	 *
	 * @param rect The GeoRectangle to be inserted
	 */
	public void add(GeoRectangle rect) {
	    if(!rect.initialised)return;
		//turn rectangle into points
		double x1 = rect.x;
		double x2 = rect.x + rect.width;
		double y1 = rect.y;
		double y2 = rect.y + rect.height;
		//Add each point to the GeoRectangle
		this.add(x1,y1);
		this.add(x2,y2);
	}

	/**
	 * Gets the bounds of this georectanle which,<br>
	 * by defenition is a clone of the GeoRectangle
	 * @return The GeoRectangle which bounds this georectangle
	 */
	public GeoRectangle getBounds(){
	    return (GeoRectangle)clone();
	}

        /**
         * Initialise the data in GeoRectangle
         */
	public void setBounds(GeoRectangle r){
            setBounds(r.x,r.y,r.width,r.height);
	}

        /**
         * Initialise the data in GeoRectangle
         */
	public void setBounds(double x_new, double y_new, double width_new, double height_new) {
            x=x_new;
            y=y_new;
            height=height_new;
            width=width_new;
	}

        /**
         * Initialise the data in GeoRectangle based on a
         * <a href="http://opengis.org">Open GIS</a> BBox format, ie
         * "xmin,ymin,xmax,ymax".  If the string is incorrectly formatted,
         * or has invalid data, then the data is set to GeoRectangle() and
         * false is returned.
         * @param BBox A string of the format "xmin,ymin,xmax,ymax".
         * @return Boolean specifying TRUE if the geoRectangle was created
         * successfully, false otherwise.
         */
	public boolean setBounds(String BBox)
        {
            boolean valid=false;
            StringTokenizer st = new StringTokenizer(BBox,",");
            if (st.countTokens()==4) {
                double x1 = Double.valueOf(st.nextToken()).doubleValue();
                double y1 = Double.valueOf(st.nextToken()).doubleValue();
                double x2 = Double.valueOf(st.nextToken()).doubleValue();
                double y2 = Double.valueOf(st.nextToken()).doubleValue();
                setBounds(x1,y1,x2-x1,y2-y1);
                valid=true;
            }
            return valid;
        }


	public void extendBounds(double x,double y){
	    add(x,y);
	}
	public void extendBounds(GeoPoint p){
	    add(p);
	}

	/**
	 * subtracts the given GeoRectangle from this one
	 * @author Mathieu van Loon <mathieu@PLAYcollective.com>
	 */
	public GeoRectangle subtract(GeoRectangle box2) {
		GeoRectangle reply = new GeoRectangle(this.x-box2.x, this.y-box2.y, this.width, this.height);
		return reply;
	}

	public Vector getPoints(){
	    double x1,y1,x2,y2; // coords of the first box

		x1=this.x;
		y1=this.y; // bottom left
		x2=this.x+this.width;
		y2=this.y+this.height; // top right

		Vector p = new Vector();
		p.addElement(new GeoPoint(x1,y1));
		p.addElement(new GeoPoint(x1,y2));
		p.addElement(new GeoPoint(x2,y1));
		p.addElement(new GeoPoint(x2,y2));
		return p;

	}
	
	/**
	 * Tests specificaly for GeoRectangle->GeoRectanle intersectsion
	 * otherwise uses GeoShape.instersects test.
	 * 
	 * @return A boolean, true if intersection occures.
	 */
    public boolean intersects(GeoShape s){
        if(s instanceof GeoRectangle){return intersects((GeoRectangle)s);}
				if(DEBUG)System.out.println("going super");
        return super.intersects(s);
    }
	/**
	 * GeoRectangle specific interesect test
	 * @return true if two GeoRectangles intersect.
	 */
	public boolean intersects(GeoRectangle box2){
	  double b1x1,b1y1,b1x2,b1y2; // coords of the first box
		double b2x1,b2y1,b2x2,b2y2; // coords of the second box

		GeoPoint [] corners1 = new GeoPoint[4];
		GeoPoint [] corners2 = new GeoPoint[4];
		corners1[0]=new GeoPoint(this.x,this.y); // BL
		corners1[1]=new GeoPoint(this.x+this.width,this.y); // BR
		corners1[2]=new GeoPoint(this.x,this.y+this.height); // TL

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -