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

📄 spatialobject.java

📁 Java source code for the Ant Colony Optimization Problem.
💻 JAVA
字号:
package jwo.jpss.spatial;       // Part of the spatial modelling package.
import jwo.jpss.spatial.gui.*;  // For GUI representation.
import java.io.Serializable;

//  *******************************************************
/** Class for defining spatial objects. All such spatial
  * objects have some geographical footprint and can compare
  * themselves with other geographical footprints.
  * @author Jo Wood
  * @version 1.4, 22nd October, 2001.
  */
//  *******************************************************

public class SpatialObject implements SpatialModel,Serializable
{        
    // ------------------ Object variables -------------------
    
    private Footprint footprint;	// Geographical footprint.
    private Header header;          // Model information (title, owner etc.)
    
    // ---------------------- Constructors --------------------
    
    /** Creates a spatial object with a default spatial footprint.
      */
    public SpatialObject()
    {
    	this(new Footprint(0,0));
    } 

    /** Creates an spatial object with the given spatial footprint.
      * @param footprint Spatial footprint of this object.
      */
    public SpatialObject(Footprint footprint)
    {
    	this(footprint, new Header("Spatial object"));
    }    	
    
    /** Creates an spatial object with the given spatial footprint and header.
      * @param footprint Spatial footprint of this object.
      * @param header Header associated with this spatial object.
      */
    public SpatialObject(Footprint footprint, Header header)
    {
    	this.footprint = footprint;
        this.header = header;
    }    

    // ------------------------- Methods ----------------------
    
    /** Makes a spatial comparison between this object and another. Assumes
      * that both objects are rectangular.
      * @param other Spatial model with which to make a comparison.
      * @return One of five topologic relations (WITHIN, MATCHES, OVERLAPS,
      * ENCLOSES, or SEPARATE).
      */
    public int compare(SpatialModel other)
    {
        Footprint otherFootprint = other.getBounds();

        // Store Minimum enclosing rectangles of both footprints.
        float minX1 = footprint.getXOrigin(),
        minY1 = footprint.getYOrigin(),
        maxX1 = footprint.getXOrigin()+footprint.getMERWidth(),
        maxY1 = footprint.getYOrigin()+footprint.getMERHeight(),
        minX2 = otherFootprint.getXOrigin(),
        minY2 = otherFootprint.getYOrigin(),
        maxX2 = otherFootprint.getXOrigin()+otherFootprint.getMERWidth(),   
        maxY2 = otherFootprint.getYOrigin()+otherFootprint.getMERHeight();
      	    
        // Compare the two MERs.
    	
    	if ((maxX1<minX2) || (minX1>maxX2) || (maxY1<minY2) || (minY1>maxY2))
    	    return SEPARATE;
    	
    	if ((minX1>minX2) && (maxX1<maxX2) && (minY1>minY2) && (maxY1<maxY2))
    	    return WITHIN;
    
    	if ((minX1<minX2) && (maxX1>maxX2) && (minY1<minY2) && (maxY1>maxY2))
    	    return ENCLOSES;
    	    
    	if ((minX1==minX2) && (maxX1==maxX2) && (minY1==minY2) && (maxY1==maxY2))
    	    return MATCHES;
    	    
    	return OVERLAPS;
    }

    /** Calculates the MER of the union of the given footprint with this MER.
      * @param otherFp Footprint of object to union with this MER.
      */
    public Footprint getUnionMER(Footprint otherFp)
    {
        // Store Minimum enclosing rectangles of both footprints.
        float minX,minY,maxX,maxY;
        minX = Math.min(footprint.getXOrigin(),otherFp.getXOrigin());   
        minY = Math.min(footprint.getYOrigin(),otherFp.getYOrigin());
        maxX = Math.max(footprint.getXOrigin()+footprint.getMERWidth(),
                        otherFp.getXOrigin()+otherFp.getMERWidth());
        maxY = Math.max(footprint.getYOrigin()+footprint.getMERHeight(),
                        otherFp.getYOrigin()+otherFp.getMERHeight());
        return new Footprint(minX,minY,maxX-minX,maxY-minY);
    }
    
    /** Moves the spatial object relatively by the given x,y coordinates.
      * @param xOffset Offset in the x direction.
      * @param yOffset Offset in the y direction.
      */
    public void move(float xOffset, float yOffset)
    {
        footprint.setXOrigin(footprint.getXOrigin()+xOffset);
        footprint.setYOrigin(footprint.getYOrigin()+yOffset);
    }  

    /** Reports the details of this spatial object.
      * @return Summary of this spatial object.
      */
    public String toString()
    {
        return footprint.toString();
    } 

    /** Creates a panel onto which this object may be drawn.
      * @return Panel onto which this object may be drawn.
      */
    public SpatialPanel createPanel()
    {
        return new SpatialPanel(this);
    }
    
    // --------------- Accessor/mutator variables ----------------
    
    /** Reports the type of spatial object.
      * @return Type of spatial object (e.g. SpatialModel.LINE, 
      *         SpatialModel.AREA etc.).
      */
    public int getType()
    {
        return footprint.getType();
    }

   /** Reports the attribute of the object at the given location.
     * @param fp Location to query.
     * @return Attribute at given location or NO_VALUE if none defined.
     */
    public float getAttribute(Footprint fp)
    {
        if (compare(new SpatialObject(fp)) == SEPARATE)
            return OUT_OF_BOUNDS;
        else
            return NO_VALUE;
    }

   /** Reports the header information associated with this object.
     * @return Header information (title, copyright etc).
     */
    public Header getHeader()
    {
        return header;
    }

   /** Sets a new the header to be associated with this object.
     * @param newHeader New header information (title, copyright etc).
     */
    public void setHeader(Header newHeader)
    {
        this.header = newHeader;
    }
    
    /** Sets the bounding rectangle of this object.
      * @param footprint Spatial footprint to assign to this object.
      */
    public void setBounds(Footprint footprint)
    {
        this.footprint = footprint;
    }

    /** Returns the 2d bounding rectangle of the object.
      * @return Minimum enclosing rectangle of object.
      */
    public Footprint getBounds()
    {
        return footprint;
    }
}

⌨️ 快捷键说明

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