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

📄 hexagon.java

📁 用Java开发的、实现类似Visio功能的应用程序源码
💻 JAVA
字号:
/**
 *    $Id:Hexagon.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.GeomConst;

 
 /**
 * Hexagon class.  A class used to represent a hexagon 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 Hexagon extends Rect{
 
   /** A const vertext type represents the left top vertex of an hexagon*/	
   public static final int 	HEXAGONVERTEX_LEFTTOP		=1;
   /** A const vertext type represents the left middle vertex of an hexagon*/	
   public static final int 	HEXAGONVERTEX_LEFTMIDDLE 	=2;
   /** A const vertext type represents the left bottom vertex of an hexagon*/	
   public static final int 	HEXAGONVERTEX_LEFTBOTTOM	=3;
   /** A const vertext type represents the right top vertex of an hexagon*/	
   public static final int 	HEXAGONVERTEX_RIGHTTOP		=4;
   /** A const vertext type represents the right middle vertex of an hexagon*/	
   public static final int 	HEXAGONVERTEX_RIGHTMIDDLE 	=5;
   /** A const vertext type represents the right bottom vertex of an hexagon*/	
   public static final int 	HEXAGONVERTEX_RIGHTBOTTOM	=6;

   /** A const vertext type represents the left top inclined side of an hexagon*/	
   public static final int 	HEXAGONSIDE_LEFTTOP		=1;
   /** A const vertext type represents the  top side of an hexagon*/	
   public static final int 	HEXAGONSIDE_TOP			=2;
   /** A const vertext type represents the right top inclined side of an hexagon*/	
   public static final int 	HEXAGONSIDE_RIGHTTOP		=3;
   /** A const vertext type represents the right bottom inclined side of an hexagon*/	
   public static final int 	HEXAGONSIDE_RIGHTBOTTOM		=4;
   /** A const vertext type represents the bottom side of an hexagon*/	
   public static final int 	HEXAGONSIDE_BOTTOM		=5;
   /** A const vertext type represents the left bottom inclined side of an hexagon*/	
   public static final int 	HEXAGONSIDE_LEFTBOTTOM		=6;


   /**
    *   Constructor for Hexagon.
    */ 	
   public Hexagon(){
   }

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


   /**
    *   Get a vertex of this hexagon.
    *   @param vertexType A hexagon vertex type.
    *   @return A hexagon vertex.
    *
    */ 	
   public JFPoint  getHexagonVertex(int vertexType){
   	//parameters for rectangle.
   	JFPoint leftTop		=getVertex(Rect.VERTEXTYPE_LEFTTOP);
   	JFPoint rightTop	=getVertex(Rect.VERTEXTYPE_RIGHTTOP);
   	JFPoint rightBottom	=getVertex(Rect.VERTEXTYPE_RIGHTBOTTOM);
   	JFPoint leftBottom	=getVertex(Rect.VERTEXTYPE_LEFTBOTTOM);
	
	//the distance from leftTop of rectangle to lefttop of hexagon.	
	double offset	=(leftTop.distance(rightTop) /2) * (1-Math.tan(Angle.PI/6));
	   	
	switch (vertexType){
   		case HEXAGONVERTEX_LEFTTOP:
   			return leftTop.nearPoint(rightTop,offset);
   			
   		case HEXAGONVERTEX_LEFTMIDDLE:
   			return leftTop.midPoint(leftBottom);
   			
   		case HEXAGONVERTEX_LEFTBOTTOM:
   			return leftBottom.nearPoint(rightBottom,offset);
   			
   		case HEXAGONVERTEX_RIGHTTOP:
   			return rightTop.nearPoint(leftTop,offset);
   			
   		case HEXAGONVERTEX_RIGHTMIDDLE:
   			return rightTop.midPoint(rightBottom);
   			
   		case HEXAGONVERTEX_RIGHTBOTTOM:
			return rightBottom.nearPoint(leftBottom,offset);

	}
	
	return null;
   }

 
   /**
    *   Get a side line of this hexagon.
    *   @param sideType A side type of this hexagon.
    *   @return A side line.
    *
    */ 	
   public LineSeg getHexagonSide(int sideType){
   	
   	JFPoint start, end;
   	
   	switch (sideType){

  		case HEXAGONSIDE_LEFTTOP:
  			start	=getHexagonVertex(HEXAGONVERTEX_LEFTMIDDLE);
  			end	=getHexagonVertex(HEXAGONVERTEX_LEFTTOP);
			return new LineSeg(start,end);  			
  			
  		case HEXAGONSIDE_TOP:
  			start	=getHexagonVertex(HEXAGONVERTEX_LEFTTOP);
  			end	=getHexagonVertex(HEXAGONVERTEX_RIGHTTOP);
			return new LineSeg(start,end);  			

  		case HEXAGONSIDE_RIGHTTOP:
  			start	=getHexagonVertex(HEXAGONVERTEX_RIGHTTOP);
  			end	=getHexagonVertex(HEXAGONVERTEX_RIGHTMIDDLE);
			return new LineSeg(start,end);  			

  		case HEXAGONSIDE_RIGHTBOTTOM:
  			start	=getHexagonVertex(HEXAGONVERTEX_RIGHTMIDDLE);
  			end	=getHexagonVertex(HEXAGONVERTEX_RIGHTBOTTOM);
			return new LineSeg(start,end);  			

  		case HEXAGONSIDE_BOTTOM:
  			start	=getHexagonVertex(HEXAGONVERTEX_RIGHTBOTTOM);
  			end	=getHexagonVertex(HEXAGONVERTEX_LEFTBOTTOM);
			return new LineSeg(start,end);  			

  		case HEXAGONSIDE_LEFTBOTTOM:
  			start	=getHexagonVertex(HEXAGONVERTEX_LEFTBOTTOM);
  			end	=getHexagonVertex(HEXAGONVERTEX_LEFTMIDDLE);
			return new LineSeg(start,end);  			
   	}
   	
   	return null;
   }

    
   /**
    *   Test if a point(x, y coordinates for instead) is within this hexagon.
    *
    *   @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){

   	//we make a 'radial' line from x,y to left
   	LineSeg destLine =new LineSeg();
   	destLine.setValue(x,y,x-GeomConst.LARGE_VALUE,y);

	//test if the radial line above intersects any side of hexagon.
	int intersectNum=0;
   	for (int i=HEXAGONSIDE_LEFTTOP; i<=HEXAGONSIDE_LEFTBOTTOM; i++){
   		LineSeg srcLine =getHexagonSide(i);
   		JFPoint start	=srcLine.getPoint1();
   		JFPoint end	=srcLine.getPoint2();
   		if (start.getY()!=end.getY() && srcLine.intersects(destLine)){
   			if (destLine.contains(start)){
   				if (start.getY()>end.getY())	intersectNum++;
   			}else if (destLine.contains(end)){
   				if (end.getY()>start.getY())	intersectNum++;
   			}else
   				intersectNum++;
   		}
   	}
   		
	
	//True if the intersect number is only ONE.	
	return  intersectNum   	==1;
   }


    /**
     * Tests if the specified point intersects any side of this hexagon.
     *
     * @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) {
    	/*
	//test if one vertex of rectangle 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;
		}
	}
	*/

   	for (int i=HEXAGONSIDE_LEFTTOP; i<=HEXAGONSIDE_LEFTBOTTOM; i++){
   		LineSeg srcLine =getHexagonSide(i);
   		if (srcLine.contains(x1,y1,pickOffset))
   			return true;
   	}
	return false;
    }

   /**
    *   Pick a line segment of this triangle 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){

   	for (int i=HEXAGONSIDE_LEFTTOP; i<=HEXAGONSIDE_LEFTBOTTOM; i++){
   		LineSeg line =getHexagonSide(i);
   		if (line.contains(x,y,pickOffset))
   			return line;
   	}
	return null;
   }


    /**
     * Tests if the specified line segment intersects the interior of this
     * <code>Hexagon</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>Hexagon</code>; <code>false</code>
     * otherwise.
     */
    public boolean intersects(double x1, double y1, double x2, double y2) {
   	for (int i=HEXAGONSIDE_LEFTTOP; i<=HEXAGONSIDE_LEFTBOTTOM; i++){
   		LineSeg srcLine =getHexagonSide(i);
   		if (srcLine.intersects(x1,y1,x2,y2))
   			return true;
   	}
	return false;
    }


    /**
     * Tests if the specified line segment intersects the interior of this
     * <code>Hexagon</code>.
     * @param line the specified {@link LineSeg} to test for intersection
     * with the interior of this <code>Hexagon</code>
     * @return <code>true</code> if the specified <code>LineSeg</code>
     * intersects the interior of this <code>Hexagon</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>Hexagon</code>.
     * @param rect the specified {@link Hexagon} to test for intersection
     * with the interior of this <code>Hexagon</code>
     * @return <code>true</code> if the specified <code>Hexagon</code>
     * intersects the interior of this <code>Hexagon</code>;
     * <code>false</code> otherwise.
     * @since 1.2
     */
    public boolean intersects(Rect rect) {
   	for (int i=HEXAGONSIDE_LEFTTOP; i<=HEXAGONSIDE_LEFTBOTTOM; i++){
   		LineSeg srcLine =getHexagonSide(i);
   		if (srcLine.intersects(rect))
   			return true;
   	}
	return false;
    }


   /**
    *   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 Hexagon(this);
  		
	}catch(Exception e){
		throw new CloneNotSupportedException(e.getMessage());
	}
  }




}

⌨️ 快捷键说明

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