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

📄 angle.java

📁 用Java开发的、实现类似Visio功能的应用程序源码
💻 JAVA
字号:
/**
 *    $Id:Angle.java $
 *
 *    Copyright 2004 ~ 2005  JingFei International Cooperation LTD. All rights reserved. *
 */
package com.jfimagine.jfgraph.geom;

 
 /**
 * Angle class.  A class used to encapsulate angle processes.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.00 $
 */  
 public class Angle{


  /**
   *   PI for the angle of  180 degrees.
   */
   public static final double  PI			=3.1415926;
 
   /**
    *   An angle from 0 to 2*PI.
    */
   private	double	m_angle	=0;
 

   /**
    *   Get the value of angle.
    *
    *   @return  The angle value.
    *
    */ 	
   public double getAngle(){
   	return m_angle;
   }

   /**
    *   Set angle value.
    *   We should force an angle between 0 ~ 2*PI here.   
    *
    *   @param val  A new rotate angle.
    *
    */ 	
   public void setAngle(double val){
   	m_angle	=getValidAngle(val);
   }


   /**
    *   Set angle value.
    *   We should force an angle between 0 ~ 2*PI here.   
    *
    *   @param val  A new rotate angle.
    *   @return a new angle between 0 ~ 2PI
    *
    */ 	
   public static double getValidAngle(double val){
   	while (val<0)
   		val	+=2 * PI;
   		
   	while (val>= 2 * PI)
   		val	-=2 * PI;
   	
   	return val;
   }


   /**
    *   Get the angle between the line from base point to target point1
    *   and the line from base point to target point2
    *
    *   @param basePoint  A base point.
    *   @param targetPoint1  The target point1.
    *   @param targetPoint2  The target point2.
    *   @return The angle.
    *
    */ 	
   public static double getAngle(JFPoint basePoint, JFPoint targetPoint1, JFPoint targetPoint2){	
   	return getAngle(basePoint,targetPoint1,targetPoint2,false);
   }

   /**
    *   Get the angle between the line from base point to target point1
    *   and the line from base point to target point2
    *
    *   @param basePoint  A base point.
    *   @param targetPoint1  The target point1.
    *   @param targetPoint2  The target point2.
    *   @param hasDirection  Indicate that if this angle will follow the direction that targetPoint2 is under 
    *           clockwise side of targetPoint1 according to basePoint, if so, the angle is a plus value, otherwise negative.
    *   @return The angle.
    *
    */ 	
   public static double getAngle(JFPoint basePoint, JFPoint targetPoint1, JFPoint targetPoint2, boolean hasDirection){	
   	if (basePoint==null || targetPoint1==null || targetPoint2==null)
   		return 0;
   	else{
   		return getAngle(basePoint.getX(),basePoint.getY(),
   			        targetPoint1.getX(),targetPoint1.getY(),
   			        targetPoint2.getX(), targetPoint2.getY(),hasDirection);
   	}
   }


   /**
    *   Get the angle between the line from base point to target point1
    *   and the line from base point to target point2
    *
    *   @param basePointX,&nbsp;basePointY  A base point.
    *   @param targetPoint1X,&nbsp;targetPoint1Y  The target point1.
    *   @param targetPoint2X,&nbsp;targetPoint2Y  The target point2.
    *   @return The angle, this angle will always less or equal than PI.
    *
    */ 	
   public static double getAngle(double basePointX, double basePointY, double targetPoint1X, double targetPoint1Y, double targetPoint2X, double targetPoint2Y){	
   	return getAngle(basePointX,basePointY,targetPoint1X,targetPoint1Y,targetPoint2X,targetPoint2Y,false);
   }

   /**
    *   Get the angle between the line from base point to target point1
    *   and the line from base point to target point2
    *
    *   @param basePointX,&nbsp;basePointY  A base point.
    *   @param targetPoint1X,&nbsp;targetPoint1Y  The target point1.
    *   @param targetPoint2X,&nbsp;targetPoint2Y  The target point2.
    *   @param hasDirection  Indicate that if this angle will follow the direction that targetPoint2 is under 
    *           clockwise side of targetPoint1 according to basePoint, if so, the angle is a plus value, otherwise negative.
    *   @return The angle, Math.abs(this angle) will always less or equal than PI.
    *
    */ 	
   public static double getAngle(double basePointX, double basePointY, double targetPoint1X, double targetPoint1Y, double targetPoint2X, double targetPoint2Y, boolean hasDirection){	
   	double angle1	=getAngle(basePointX, basePointY,targetPoint1X,targetPoint1Y);
   	double angle2	=getAngle(basePointX, basePointY,targetPoint2X,targetPoint2Y);
   	double result	=Math.abs(angle2-angle1);
  	if (result>PI)
   		result  =2 * PI - result;
   	if (!JFVector.underClockwiseSide(targetPoint2X,targetPoint2Y,basePointX,basePointY,
   				         targetPoint1X,targetPoint1Y,basePointX,basePointY)){
   		result	=-result;		        
         }
   		
   	return result;
   }


   /**
    *   Get the angle between the line from base point to target point
    *   and the horizontal line(from left to right).
    *
    *   @param basePoint  A base point.
    *   @param targetPoint  The target point.
    *   @return The angle.
    *
    */ 	
   public static double getAngle(JFPoint basePoint, JFPoint targetPoint){	
   	return getAngle(basePoint,targetPoint,true);
   }

   /**
    *   Get the angle between the line from base point to target point
    *   and the horizontal line(from left to right).
    *
    *   @param baseX,&nbsp;baseY  Coordinates of the base point.
    *   @param targetX,&nbsp;targetY Coordinates of the target point.
    *   @return The angle.
    *
    */ 	
   public static double getAngle(double baseX, double baseY,double targetX,double targetY){	
	return getAngle(baseX,baseY,targetX,targetY,true);   	
   }

   /**
    *   Get the angle between the line from base point to target point
    *   and the horizontal line(from left to right).
    *
    *   @param basePoint  A base point.
    *   @param targetPoint  The target point.
    *   @param counterClockwiseAngle We treat the return angle is under a counter-clockwise quadrant system.
    *   @return The angle.
    *
    */ 	
   public static double getAngle(JFPoint basePoint, JFPoint targetPoint, boolean counterClockwiseAngle){	
   	return getAngle(basePoint.getX(),basePoint.getY(),targetPoint.getX(),targetPoint.getY(),counterClockwiseAngle);
   }
   	

   /**
    *   Get the angle between the line from base point to target point
    *   and the horizontal line(from left to right).
    *
    *   @param baseX,&nbsp;baseY  Coordinates of the base point.
    *   @param targetX,&nbsp;targetY Coordinates of the target point.
    *   @param counterClockwiseAngle We treat the return angle is under a counter-clockwise quadrant system.
    *   @return The angle.
    *
    */ 	
   public static double getAngle(double baseX, double baseY,double targetX,double targetY, boolean counterClockwiseAngle){	

   	double angle=0;
	if (baseX==targetX && baseY==targetY)
		return 0;
	   	
   	//vertical line from basePoint to targetPoint
   	if ((float)baseX==(float)targetX){
   		if (counterClockwiseAngle){
   			if (baseY>targetY)
   				angle	=  PI/2;
   			else
   				angle	=  -1 * PI/2;
   		}else{
   			if (baseY<=targetY)
   				angle	=  PI/2;
   			else
   				angle	=  -1 * PI/2;
   		}
   	
   	//horizontal line from basePoint to targetPoint
   	}else if ((float)baseY==(float)targetY){
   		if (baseX>targetX)
   			angle	=PI;
   		else
   			angle	=0;
   	}else{

   		double slope	=(targetY-baseY)/(targetX-baseX);

   		angle		=Math.atan(slope);

		if (counterClockwiseAngle){	
   			//for a counter clockwise quadrant system, we will adjust the angle to an  ACTUAL slople in a counter-clockwise quadrant system.
   			if (slope>0){
   				//the angle will always under the first/third quadrant.
   				if (targetX < baseX){
   					angle	= Angle.PI - angle;
   				}else{
   					angle	= -1 * angle;
   				}
   			}else{
   				//the angle will always under the second/fourth quadrant.
   				if (targetX < baseX){
   					angle	= Angle. PI + (Angle.PI * 2 -angle);
   				}else{
   					angle   = -1 * angle;
   				}
   			}
   		}else{
   			//for a clockwise quadrant system, we will also adjust the angle.
   			if (slope>0){
   				//the angle will always under the first/third quadrant.
   				if (targetX < baseX){
   					angle	= Angle.PI + angle;
   				}else{
   					angle	= angle;
   				}
   			}else{
   				//the angle will always under the second/fourth quadrant.
   				if (targetX < baseX){
   					angle	= Angle. PI - Math.abs(angle);
   				}else{
   					angle   = angle;
   				}
   			}
   		}
   	}


   	return getValidAngle(angle);
   }


   /**
    *   Calculate a new slope after rotating by an angle.
    *
    *   @param slope original slope of a line.
    *
    *   @param angle new radian angle that rotated.
    *
    *   @return A new slope.
    *
    */ 	
   public static double newSlopeByAddingAngle(double slope,double angle){
	double newSlope	=0;
	//force angle to 0 ~ 2PI
	angle	=getValidAngle(angle);
	if ((float)angle==(float)PI/2 ||(float)angle==(float)PI * 3/2){
		newSlope	=GeomConst.LARGE_VALUE;
	}else{
		newSlope	=Math.tan(angle);
	}
	return newSlopeByAddingSlope(slope,newSlope);
   }

   /**
    *   Calculate a new slope by append a new slope that rotated by a new angle.
    *
    *   @param slope original slope of a line.
    *
    *   @param newSlope a new slope appended.
    *
    *   @return A new slope.
    *
    */ 	
   public static double newSlopeByAddingSlope(double slope,double newSlope){
   	
   	if (slope==0){
   		return newSlope;

   	}else if (slope==GeomConst.LARGE_VALUE){
   		if (newSlope==0)
   			return GeomConst.LARGE_VALUE;
   		
   		else if (newSlope==GeomConst.LARGE_VALUE)	
   			return 0;
   		
   		else
   			return 	-1/newSlope;
   	
   	}else if (newSlope==0){
   		return slope;
   	

   	}else if (newSlope==GeomConst.LARGE_VALUE){
   		return -1/slope;	

	}else{
		if ((float)slope==(float)1/newSlope)
			return GeomConst.LARGE_VALUE;
		else
   			//tan(alpha+beta)=(tan(alpha)+tan(beta))/(1-tan(alpha)*tan(beta))
   			return (slope+newSlope)/(1-slope*newSlope);
   	}
   }


   	  	
   /**
    *   Add a new angle value to existance angle.
    *
    *   @param angle A new angle value to be added.
    *
    */ 	
   public void addAngle(double angle){
   	setAngle(m_angle + angle);
   }


   /**
    *   Constructor for Angle.
    *   Default to 0 for x and y coordinates.
    *
    */ 	
   public Angle(){
   	m_angle	=0;
   }

   /**
    *   Constructor for Angle.
    *   
    *   @param rect A rectangle object.
    *
    */ 	
   public Angle(Angle angle){
   	if (angle!=null){
   		m_angle	=angle.getAngle();
   	}
   }


}

⌨️ 快捷键说明

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