diamond.java

来自「用Java开发的、实现类似Visio功能的应用程序源码」· Java 代码 · 共 230 行

JAVA
230
字号
/**
 *    $Id:Diamond.java $
 *
 *    Copyright 2004 ~ 2005  JingFei International Cooperation LTD. All rights reserved. *
 */
package com.jfimagine.jfgraph.geom;

import com.jfimagine.jfgraph.geom.JFDimension;
import com.jfimagine.jfgraph.geom.JFPoint;
import com.jfimagine.jfgraph.geom.LineSeg;
import com.jfimagine.jfgraph.geom.Rect;
import com.jfimagine.jfgraph.geom.Arc;

 
 /**
 * Diamond class.  A class used to represent a diamond rectangle in the plane with double coordinates. 
 *   <p> Attention: Here we used a clockwise quadrant system.
 *   And the first quadrant is under right bottom corner.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.00 $
 */  
 public class Diamond extends Rect{

   /**An temporary rect object that used to get the actual rectangle of this diamond*/	
   private Rect m_diamond;
   
   /**
    *   Constructor for Diamond.
    */ 	
   public Diamond(){
   }

   /**
    *   Constructor for Diamond.
    *   
    *   @param diamond A diamond object.
    *
    */ 	
   public Diamond(Diamond diamond){
   	super.setValue(diamond);
   }

   /**
    *   initiate/create the actual rectangle object of this diamond.
    *
    */ 	
   public void initDiamond(){
   	if (m_diamond==null)
   		m_diamond	=new Rect();
   	
   	m_diamond.setVertex(Rect.VERTEXTYPE_LEFTTOP,m_leftTop.midPoint(m_rightTop));
   	m_diamond.setVertex(Rect.VERTEXTYPE_RIGHTTOP,m_rightTop.midPoint(m_rightBottom));
   	m_diamond.setVertex(Rect.VERTEXTYPE_LEFTBOTTOM,m_leftTop.midPoint(m_leftBottom));
   	m_diamond.setVertex(Rect.VERTEXTYPE_RIGHTBOTTOM,m_leftBottom.midPoint(m_rightBottom));
   }

   
   /**
    *   get the actual rectangle object of this dimaond.
    *
    *   @return The actual rectangle.
    *
    */ 	
   public Rect getDiamond(){
   	initDiamond();
   	return m_diamond;
   }
 
   /**
    *   Get a side line of this diamond.
    *   We we must consider each side will be shorten by two 'RADIUS' distances of the rounded corner.
    *
    *   @return A side line.
    *
    */ 	
   public LineSeg getSide(int sideType){
	initDiamond();
   	return m_diamond.getSide(sideType);
   }

 
    
   /**
    *   Test if a point(x, y coordinates for instead) is within this diamond.
    *
    *   @param x   X coordinate of this point.
    *
    *   @param y   Y coordinate of this point.
    *
    *   @return True if the point is inside this rectangle, false otherwise.
    *
    */ 	
   public boolean contains(double x, double y){
	initDiamond();
	return m_diamond.contains(x,y);   	
   }

   /**
    *   Pick a line segment of this rectangle by a point with a pickoffset.
    *   @param x   X coordinate of this point.
    *   @param y   Y coordinate of this point.
    *   @param pickOffset An analog pick offset that great equal than 0.
    *
    *   @return A line segment picked.
    *
    */ 	
   public LineSeg pickLine(double x, double y,double pickOffset){
	initDiamond();

   	LineSeg line	=new LineSeg();

	line.setPoint1(m_diamond.getVertex(Rect.VERTEXTYPE_LEFTTOP));
	line.setPoint2(m_diamond.getVertex(Rect.VERTEXTYPE_RIGHTTOP));
  	if (line.contains(x,y,pickOffset))  return line;
  	
	line.setPoint1(m_diamond.getVertex(Rect.VERTEXTYPE_RIGHTTOP));
	line.setPoint2(m_diamond.getVertex(Rect.VERTEXTYPE_RIGHTBOTTOM));
  	if (line.contains(x,y,pickOffset))  return line;

	line.setPoint1(m_diamond.getVertex(Rect.VERTEXTYPE_RIGHTBOTTOM));
	line.setPoint2(m_diamond.getVertex(Rect.VERTEXTYPE_LEFTBOTTOM));
  	if (line.contains(x,y,pickOffset))  return line;

	line.setPoint1(m_diamond.getVertex(Rect.VERTEXTYPE_LEFTBOTTOM));
	line.setPoint2(m_diamond.getVertex(Rect.VERTEXTYPE_LEFTTOP));
  	if (line.contains(x,y,pickOffset))  return line;
  	
  	return null;
   }


    /**
     * Tests if the specified point intersects any side of this diamond.
     *
     * @param x1,&nbsp;y1 the specified point.
     *	
     * @param pickOffset An analog offset for 'pick' this line.
     *
     * @return <code>true</code> if the specified point intersects one side of this rectangle.
     * false otherwise.
     */
    public boolean intersects(double x1, double y1, int pickOffset) {
	initDiamond();
	
	/*
	//test if one vertex intersects with this point.
	for (int vertex=Rect.VERTEXTYPE_LEFTTOP; vertex<=Rect.VERTEXTYPE_RIGHTBOTTOM; vertex++){
		
		JFPoint pnt	=getVertex(vertex);
		
		if (pnt.distance(x1,y1)<=pickOffset){
			return true;
		}
	}
	*/
	
	return m_diamond.intersects(x1,y1,pickOffset);
    }

    /**
     * Tests if the specified line segment intersects the interior of this
     * <code>Diamond</code>.
     * @param x1,&nbsp;y1 the first endpoint of the specified
     * line segment
     * @param x2,&nbsp;y2 the second endpoint of the specified
     * line segment
     * @return <code>true</code> if the specified line segment intersects
     * the interior of this <code>Diamond</code>; <code>false</code>
     * otherwise.
     */
    public boolean intersects(double x1, double y1, double x2, double y2) {
    	initDiamond();
    	return m_diamond.intersects(x1,y1,x2,y2);
    }


    /**
     * Tests if the specified line segment intersects the interior of this
     * <code>Diamond</code>.
     * @param line the specified {@link LineSeg} to test for intersection
     * with the interior of this <code>Diamond</code>
     * @return <code>true</code> if the specified <code>LineSeg</code>
     * intersects the interior of this <code>Diamond</code>;
     * <code>false</code> otherwise.
     * @since 1.2
     */
    public boolean intersects(LineSeg line) {
    	if (line==null)
    		return false;
    	else
    		return  intersects(line.getX1(),line.getY1(),line.getX2(),line.getY2());
    }

    /**
     * Tests if the specified rectangle intersects the interior of this
     * <code>Diamond</code>.
     * @param rect the specified {@link Diamond} to test for intersection
     * with the interior of this <code>Diamond</code>
     * @return <code>true</code> if the specified <code>Diamond</code>
     * intersects the interior of this <code>Diamond</code>;
     * <code>false</code> otherwise.
     * @since 1.2
     */
    public boolean intersects(Rect rect) {
	initDiamond();
	return m_diamond.intersects(rect);
    }


   /**
    *   Creates a new object of the same class and with the same contents as this object.
    * 
    *   @return  A clone of this instance.
    *
    */ 	
  public Object clone() throws CloneNotSupportedException{
  	try{
  		return new Diamond(this);
  		
	}catch(Exception e){
		throw new CloneNotSupportedException(e.getMessage());
	}
  }




}

⌨️ 快捷键说明

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