isoscelestriangle.java

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

JAVA
332
字号
/**
 *    $Id:IsoscelesTriangle.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;

 
 /**
 * IsoscelesTriangle class.  A class used to represent a isosceles triangle 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 IsoscelesTriangle extends Rect{
 
   /** A const vertext type represents the top vertex of an isosceles triangle*/	
   public static final int 	TRIANGLEVERTEX_TOP	=1;
   /** A const vertext type represents the left vertex of an isosceles triangle*/	
   public static final int 	TRIANGLEVERTEX_LEFT	=2;
   /** A const vertext type represents the right vertex of an isosceles triangle*/	
   public static final int 	TRIANGLEVERTEX_RIGHT	=3;

   /** A const vertext type represents the left inclined side of an isosceles triangle*/	
   public static final int 	TRIANGLESIDE_LEFT	=1;
   /** A const vertext type represents the right inclined side of an isosceles triangle*/	
   public static final int 	TRIANGLESIDE_RIGHT	=2;
   /** A const vertext type represents the bottom side of an isosceles triangle*/	
   public static final int 	TRIANGLESIDE_BOTTOM	=3;


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

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


   /**
    *   Get a vertex of this triangle.
    *   @param vertexType A triangle vertex type.
    *   @return A triangle vertex.
    *
    */ 	
   public JFPoint  getTriangleVertex(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);
   	
	switch (vertexType){
		case 	TRIANGLEVERTEX_TOP:
			return leftTop.midPoint(rightTop);

		case 	TRIANGLEVERTEX_LEFT:
			return leftBottom;

		case 	TRIANGLEVERTEX_RIGHT:
			return rightBottom;
	}
	
	return null;
   }

 
   /**
    *   Get a side line of this triangle.
    *   @param sideType A side type of this triangle.
    *   @return A side line.
    *
    */ 	
   public LineSeg getTriangleSide(int sideType){
   	
   	JFPoint top, left, right;
   	
   	switch (sideType){
   		case  TRIANGLESIDE_LEFT:
   			top	=getTriangleVertex(TRIANGLEVERTEX_TOP);
   			left	=getTriangleVertex(TRIANGLEVERTEX_LEFT);
   			return new LineSeg(top,left);
   			
   		case  TRIANGLESIDE_RIGHT:
   			top	=getTriangleVertex(TRIANGLEVERTEX_TOP);
   			right	=getTriangleVertex(TRIANGLEVERTEX_RIGHT);
   			return new LineSeg(top,right);

   		case  TRIANGLESIDE_BOTTOM:
   			left	=getTriangleVertex(TRIANGLEVERTEX_LEFT);
   			right	=getTriangleVertex(TRIANGLEVERTEX_RIGHT);
   			return new LineSeg(left,right);
   	}
   	
   	return null;
   }

    
   /**
    *   Test if a point(x, y coordinates for instead) is within this triangle.
    *
    *   @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 triangle.
	int intersectNum=0;
	
	for (int i=TRIANGLESIDE_LEFT; i<=TRIANGLESIDE_BOTTOM; i++){
   		LineSeg srcLine =getTriangleSide(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 triangle.
     *
     * @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;
		}
	}
	*/

   	JFPoint	top	=getTriangleVertex(TRIANGLEVERTEX_TOP);
   	JFPoint left	=getTriangleVertex(TRIANGLEVERTEX_LEFT);
   	JFPoint right	=getTriangleVertex(TRIANGLEVERTEX_RIGHT);

   	LineSeg srcLine =new LineSeg();
   	srcLine.setValue(top,left);
   	if (srcLine.contains(x1,y1,pickOffset))
   		return true;
   		
   	srcLine.setValue(top,right);
   	if (srcLine.contains(x1,y1,pickOffset))
   		return true;

   	srcLine.setValue(left,right);
   	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){

   	JFPoint	top	=getTriangleVertex(TRIANGLEVERTEX_TOP);
   	JFPoint left	=getTriangleVertex(TRIANGLEVERTEX_LEFT);
   	JFPoint right	=getTriangleVertex(TRIANGLEVERTEX_RIGHT);


   	LineSeg line	=new LineSeg();

	line.setValue(top,left);
  	if (line.contains(x,y,pickOffset))  return line;
  	
	line.setValue(top,right);
  	if (line.contains(x,y,pickOffset))  return line;

	line.setValue(left,right);
  	if (line.contains(x,y,pickOffset))  return line;
  	
  	return null;
   }

    /**
     * Tests if the specified line segment intersects the interior of this
     * <code>IsoscelesTriangle</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>IsoscelesTriangle</code>; <code>false</code>
     * otherwise.
     */
    public boolean intersects(double x1, double y1, double x2, double y2) {
   	JFPoint	top	=getTriangleVertex(TRIANGLEVERTEX_TOP);
   	JFPoint left	=getTriangleVertex(TRIANGLEVERTEX_LEFT);
   	JFPoint right	=getTriangleVertex(TRIANGLEVERTEX_RIGHT);

   	LineSeg srcLine =new LineSeg();
   	srcLine.setValue(top,left);
   	if (srcLine.intersects(x1,y1,x2,y2))
   		return true;
   		
   	srcLine.setValue(top,right);
   	if (srcLine.intersects(x1,y1,x2,y2))
   		return true;

   	srcLine.setValue(left,right);
   	if (srcLine.intersects(x1,y1,x2,y2))
   		return true;

	return false;	
    }


    /**
     * Tests if the specified line segment intersects the interior of this
     * <code>IsoscelesTriangle</code>.
     * @param line the specified {@link LineSeg} to test for intersection
     * with the interior of this <code>IsoscelesTriangle</code>
     * @return <code>true</code> if the specified <code>LineSeg</code>
     * intersects the interior of this <code>IsoscelesTriangle</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>IsoscelesTriangle</code>.
     * @param rect the specified {@link IsoscelesTriangle} to test for intersection
     * with the interior of this <code>IsoscelesTriangle</code>
     * @return <code>true</code> if the specified <code>IsoscelesTriangle</code>
     * intersects the interior of this <code>IsoscelesTriangle</code>;
     * <code>false</code> otherwise.
     * @since 1.2
     */
    public boolean intersects(Rect rect) {
   	JFPoint	top	=getTriangleVertex(TRIANGLEVERTEX_TOP);
   	JFPoint left	=getTriangleVertex(TRIANGLEVERTEX_LEFT);
   	JFPoint right	=getTriangleVertex(TRIANGLEVERTEX_RIGHT);
   	
   	if (rect.contains(top) || rect.contains(left) || rect.contains(right))
   		return true;

   	LineSeg srcLine =new LineSeg();
   	srcLine.setValue(top,left);
   	if (srcLine.intersects(rect))
   		return true;

   	srcLine.setValue(top,right);
   	if (srcLine.intersects(rect))
   		return true;

   	srcLine.setValue(left,right);
   	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 IsoscelesTriangle(this);
  		
	}catch(Exception e){
		throw new CloneNotSupportedException(e.getMessage());
	}
  }




}

⌨️ 快捷键说明

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