jfpoint.java

来自「用Java开发的、实现类似Visio功能的应用程序源码」· Java 代码 · 共 776 行 · 第 1/2 页

JAVA
776
字号
   		y2	=getY() - dist;
	
	}else if ((float)slope==0){
   		x1	=getX() + dist;
   		y1	=getY();
   		x2	=getX() - dist;
   		y2	=getY();
	
	}else{
   		//for a line from x1,y1 to x2,y2,with a slope k,will have equation as below,
   		//y=k(x-x1)+y1
   		//for a point close to x1,y1 and on the line from x1,y1 to x2,y2, with distance A,
   		//will have equation according to Pythagorean proposition as below,
   		//(y-y1)^2+(x-x1)^2=A^2
   		//so we get x=x1+sqrt(A^2/(1+k^2)) or x=x1-sqrt(A^2/(1+k^2))
   		x1=getX()+Math.sqrt(dist*dist/(1+slope*slope));
   		x2=getX()-Math.sqrt(dist*dist/(1+slope*slope));
   	
   		y1=slope * (x1-getX()) + getY();
   		y2=slope * (x2-getX()) + getY();
   	}
	
	JFPoint newPoint	=new JFPoint();
	newPoint.setValue(x1,y1);
	if (JFVector.underClockwiseSide(this,newPoint,this,refPoint)==direction)	
		return newPoint;
	else{
		newPoint.setValue(x2,y2);
		return newPoint;
	}
   }


   /**
    *   Get a point that is on the middle upright line of  the line from start point to end point,
    *   the point will has a specified offset according to upright foot,and according to clockwise term.
    *
    *   @param startPoint The start point of this arc.
    *
    *   @param endPoint The end point of this arc.
    *
    *   @param offset   The distance of the point shifts from upright foot.
    *
    *   @param clockwise if the result point is on clockwise side of the vector from start pointn to end point. False default.
    *
    *   @return The result point.
    *
    */ 	
   public static JFPoint getMiddleUprightPoint(JFPoint startPoint, JFPoint endPoint, double offset, boolean clockwise){
   	if (startPoint==null || endPoint==null)
   		return null;

	//center point between startpoint and end point.   		
   	JFPoint centerPoint	=startPoint.midPoint(endPoint);
   	
   	//result point
   	JFPoint resultPoint	=new JFPoint(centerPoint);
   	
   	//slope of the line from startpoint to endpoint.
   	double  slope1		=startPoint.getSlope(endPoint);

   	if (slope1==GeomConst.LARGE_VALUE){
   		if (startPoint.getY() < endPoint.getY())
   			if (clockwise)
				resultPoint.setX(centerPoint.getX()-offset);   		
			else
				resultPoint.setX(centerPoint.getX()+offset);   		
		else
			if (clockwise)
				resultPoint.setX(centerPoint.getX()+offset);   		
			else
				resultPoint.setX(centerPoint.getX()-offset);   		
	}else if ((float)slope1==0){
		if (startPoint.getX() < endPoint.getX())
			if (clockwise)
				resultPoint.setY(centerPoint.getY()+offset);   		
			else
				resultPoint.setY(centerPoint.getY()-offset);   		
		else
			if (clockwise)
				resultPoint.setY(centerPoint.getY()-offset);   		
			else
				resultPoint.setY(centerPoint.getY()+offset);   		
	}else{
   		//slope of the upright line of the line from startpoint to endpoint.
   		double  slope2	=-1 / slope1;
   		
   		//equation of the upright line:
   		//y=k(x-x0) + y0
   		//equation of the control point(A=offset):
   		//(y-y0)^2 + (x-x0) ^ 2 = A ^ 2
   		//slove the two equation ablove:
   		//x = sqrt(A ^ 2/(1+k^2)) + x0
   		double x0	=centerPoint.getX();
   		double y0	=centerPoint.getY();
   		double delta    =offset * offset/(1 + slope2 * slope2);
   		
   		double x1	=Math.sqrt(delta) + x0;
   		double y1	=slope2 * (x1-x0) + y0;
   		
   		double x2	=-1 * Math.sqrt(delta) + x0;
   		double y2	=slope2 * (x2-x0) + y0;
   		
   		
   		if (JFVector.underClockwiseSide(startPoint.getX(), startPoint.getY(), x1, y1,
   				       		startPoint.getX(), startPoint.getY(), endPoint.getX(), endPoint.getY())){
			if (clockwise)
   				resultPoint.setValue(x1,y1);	
   			else
   				resultPoint.setValue(x2,y2);
   		}else{
   			if (clockwise)
   				resultPoint.setValue(x2,y2);
   			else
   				resultPoint.setValue(x1,y1);
   		}
   	}
   	
   	return resultPoint;
   }

   /**
    *   Get the slope of the line from this point to specified point.
    *
    *   @param pnt An end point of line to get slope.
    *
    *   @return the slope.
    *
    */ 	
   public double getSlope(JFPoint pnt){
   	if (pnt==null)
   		return 0;
   	else
   		return getSlope(pnt.getX(), pnt.getY());
   }

   /**
    *   Get the slope of the line from this point to specified point.
    *
    *   @param x,&nbsp;y  An end point of line to get slope.
    *
    *   @return the slope.
    *
    */ 	
   public double getSlope(double x, double y){
   	if ((float)m_x ==(float)x)
   		return GeomConst.LARGE_VALUE;
   	else
   	if ((float)m_y ==(float)y)
   		return 0;
   	else
   		return (y-m_y)/(x-m_x);
   }


   /**
    *   Move this point by specific x and y coordinates.
    *
    *   @param x   X coordiate to moveby. 
    *
    *   @param y   Y coordiate to moveby.
    *
    */ 	
   public void moveBy(double x, double y){
   	m_x	+=x;
   	m_y	+=y;
   }

   /**
    *   Rotate this point by a specified point and an angle theta.
    *
    *   @param pnt  A rotate center point.
    *
    *   @param theta A rotate angle.
    *
    */ 	
   public void rotateBy(JFPoint pnt, double theta){
   	if (pnt!=null)
   		rotateBy(pnt.getX(),pnt.getY(),theta);
   }
	  
   /**
    *   Rotate this point by a specified point and an angle theta.
    *
    *   @param baseX,&nbsp;baseY  A rotate center point.
    *
    *   @param theta A rotate angle.
    *
    */ 	
   public void rotateBy(double baseX, double baseY, double theta){
	  //rotate radius
	  double radius		=distance(baseX,baseY);
	  if (radius==0)
	  	return; //zero radius
	  			  
	  //original angle between the line from base point(pnt) to this point and x axis.
	  double angle	=Angle.getAngle(baseX,baseY,getX(),getY(),false);

  
	  //new angle.
	  angle		+=theta;

	  
	  //new coordinates of this point.
	  m_x		=baseX + radius * Math.cos(angle);
	  m_y		=baseY + radius * Math.sin(angle);
  
   }


   /**
    *   Mirror this point by a x coordinate. We make a left-right mirror here.
    *
    *   @param baseX  A mirror base x coordinate.
    *
    */ 	
   public void mirrorBy(double baseX){
   	/*
   	 * a mirror operation should not change current point's y coordinate.
   	 * this should only change current point's x coordinate.
   	 */
   	m_x	=baseX - (m_x - baseX);
   }

   /**
    *   Reverse this point by a y coordinate. We make a up-down flip here.
    *
    *   @param baseY  A flip base y coordinate.
    *
    */ 	
   public void flipBy(double baseY){
   	/*
   	 * a revese operation should not change current point's x coordinate.
   	 * this should only change current point's y coordinate.
   	 */
   	m_y	=baseY - (m_y - baseY);
   }

   /**
    *   Scale this point by a point, and specified scale.
    *
    *   @param pnt  A scale reference point.
    *
    *   @param scale  A scale value.
    *
    */ 	
   public void scaleBy(JFPoint pnt, double scale){
   	if (pnt!=null)
   		scaleBy(pnt.getX(),pnt.getY(),scale);
   }


   /**
    *   Scale this point by a point, and specified scale.
    *
    *   @param baseX,&nbsp;baseY  A scale reference point.
    *
    *   @param scale  A scale value.
    *
    */ 	
   public void scaleBy(double baseX, double baseY, double scale){
   	scaleBy(baseX,baseY,scale,scale);
   }

   /**
    *   Scale this point by a point, and specified scale.
    *
    *   @param baseX,&nbsp;baseY  A scale reference point.
    *
    *   @param xScale  A scale value in x coordinate.
    *   @param yScale  A scale value in y coordinate.
    *
    */ 	
   public void scaleBy(double baseX, double baseY, double xScale, double yScale){

	if (xScale<0)  xScale	=0;
	if (yScale<0)  yScale	=0;
	
	m_x	=baseX + (m_x - baseX) * xScale;
	m_y	=baseY + (m_y - baseY) * yScale;
   }



   /**
    *   Convert this object to String 
    * 
    *   @return  An string represents the content of the object
    *
    */ 	
   public String toString(){
  	StringBuffer buf =new StringBuffer();
  	buf.append(";x=");  	buf.append(m_x);
  	buf.append(";y=");  	buf.append(m_y);
  	
  	return buf.toString();
   }

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


   /**
    *   Returns the hashcode for this Object.
    * 
    *   @return hash code for this Point2D.
    *
    */ 	
  public int hashCode(){
       	
  	long  x   =Double.doubleToLongBits(m_x);
  	long  y   =Double.doubleToLongBits(m_y);
  	
  	return 	(int)(x ^ (x >>> 32)) ^ (int)(y ^ (y >>> 32));
  }


   /**
    *   Determines whether or not two objects are equal. 
    * 
    * 	@param obj  an object to be compared with this object 
    * 
    *   @return true if the object to be compared is an instance of Port and has the same values; false otherwise.
    *
    */ 	
  public boolean equals(Object obj){
	return equals(obj,false);
  }

   /**
    *   Determines whether or not two objects are equal. 
    * 
    * 	@param obj  an object to be compared with this object 
    * 	@param analog True if use integer type to compare two points,False use double.
    * 
    *   @return true if the object to be compared is an instance of Port and has the same values; false otherwise.
    *
    */ 	
  public boolean equals(Object obj, boolean analog){

      if (obj == this)
             	return true;

      if (!(obj instanceof JFPoint))
            	return false;

      JFPoint	pnt	=(JFPoint)obj;
      
      if (!analog)
      		return	m_x==pnt.getX() && m_y==pnt.getY();
      else
      		return	(int)m_x==(int)pnt.getX() && (int)m_y==(int)pnt.getY();
  }



   /**
    *   Determines whether or not this point equals the coordinates given.
    * 
    * 	@param x,y Coordinates of the other point.
    * 	@param analog True if use integer type to compare two points,False use double.
    * 
    *   @return true if equal ,false otherwise.
    *
    */ 	
  public boolean equals(double x, double y, boolean analog){
      if (!analog)
      		return	m_x==x && m_y==y;
      else
      		return	(int)m_x==(int)x && (int)m_y==(int)y;
  }



}

⌨️ 快捷键说明

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