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

📄 curve.java

📁 用Java开发的、实现类似Visio功能的应用程序源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

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


import com.jfimagine.jfgraph.geom.JFPoint;
import com.jfimagine.jfgraph.geom.JFCurvePoint;
import com.jfimagine.jfgraph.geom.LineSeg;
import com.jfimagine.jfgraph.geom.Rect;
import com.jfimagine.jfgraph.geom.Angle;

 
 /**
 * Curve class.  A class used to represent an bezier curve shape.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.00 $
 */  
 public class Curve implements Cloneable {
  
   /**
    *   start point of curve
    */
   private	JFPoint	m_startPoint	=new JFPoint();

   /**
    *  end point of curve.
    */
   private	JFPoint m_endPoint	=new JFPoint();

   /**
    *  one control point of curve.
    */
   private	JFPoint m_controlPoint1	=new JFPoint();

   /**
    *  the other control point of curve.
    */
   private	JFPoint m_controlPoint2	=new JFPoint();


   //*****************  start point *************************	
   
   /**
    *   Get x coordinates of start point.
    *
    *   @return  The x coordinates.
    *
    */ 	
   public double getX1(){
   	return m_startPoint.getX();
   }

   /**
    *   Get y coordinates of start point.
    *
    *   @return  The y coordinates.
    *
    */ 	
   public double getY1(){
   	return m_startPoint.getY();
   }

   /**
    *   Set coordinates of start point.
    *
    *   @param x; y  Coordinates of the start point.
    *
    */ 	
   public void setStartPoint(double x, double y){
   	m_startPoint.setX(x);
   	m_startPoint.setY(y);
   }

   /**
    *   Set coordinates of start point.
    *
    *   @param startPoint  Coordinates of the start point.
    *
    */ 	
   public void setStartPoint(JFPoint startPoint){
   	setStartPoint(startPoint.getX(),startPoint.getY());
   }

   /**
    *   Get the start point.
    *
    *   @return the start point.
    *
    */ 	
   public JFPoint getStartPoint(){
   	return m_startPoint;
   }


   //*****************  control point 1*************************	

   /**
    *   Get x coordinates of control point1. 
    *
    *   @return  The x coordinates.
    *
    */ 	
   public double getX2(){
   	return m_controlPoint1.getX();
   }

   /**
    *   Get y coordinates of control point1.
    *
    *   @return  The y coordinates.
    *
    */ 	
   public double getY2(){
   	return m_controlPoint1.getY();
   }


   /**
    *   Set coordinates of control point1.
    *
    *   @param x; y  Coordinates of the control point1.
    *
    */ 	
   public void setControlPoint1(double x, double y){
   	m_controlPoint1.setX(x);
   	m_controlPoint1.setY(y);
   }

   /**
    *   Set coordinates of control point1.
    *
    *   @param controlPoint1  Coordinates of the control point1.
    *
    */ 	
   public void setControlPoint1(JFPoint controlPoint1){
   	setControlPoint1(controlPoint1.getX(),controlPoint1.getY());
   }

   /**
    *   Get the control point1.
    *
    *   @return the control point1.
    *
    */ 	
   public JFPoint getControlPoint1(){
   	return m_controlPoint1;
   }

   //*****************  control point 2*************************	

   /**
    *   Get x coordinates of control point2. 
    *
    *   @return  The x coordinates.
    *
    */ 	
   public double getX3(){
   	return m_controlPoint2.getX();
   }

   /**
    *   Get y coordinates of control point2.
    *
    *   @return  The y coordinates.
    *
    */ 	
   public double getY3(){
   	return m_controlPoint2.getY();
   }

   /**
    *   Set coordinates of control point2.
    *
    *   @param x; y  Coordinates of the control point2.
    *
    */ 	
   public void setControlPoint2(double x, double y){
   	m_controlPoint2.setX(x);
   	m_controlPoint2.setY(y);
   }

   /**
    *   Set coordinates of control point2.
    *
    *   @param controlPoint2  Coordinates of the control point2.
    *
    */ 	
   public void setControlPoint2(JFPoint controlPoint2){
   	setControlPoint2(controlPoint2.getX(),controlPoint2.getY());
   }

   /**
    *   Get the control point2.
    *
    *   @return the control point2.
    *
    */ 	
   public JFPoint getControlPoint2(){
   	return m_controlPoint2;
   }


   //*****************  end point *************************	

   /**
    *   Get x coordinates of end point.
    *
    *   @return  The x coordinates.
    *
    */ 	
   public double getX4(){
   	return m_endPoint.getX();
   }

   /**
    *   Get y coordinates of end point.
    *
    *   @return  The y coordinates.
    *
    */ 	
   public double getY4(){
   	return m_endPoint.getY();
   }

   /**
    *   Set coordinates of end point.
    *
    *   @param x; y  Coordinates of the end point.
    *
    */ 	
   public void setEndPoint(double x, double y){
   	m_endPoint.setX(x);
   	m_endPoint.setY(y);
   }

   /**
    *   Set coordinates of end point.
    *
    *   @param endPoint  Coordinates of the end point.
    *
    */ 	
   public void setEndPoint(JFPoint endPoint){
   	setEndPoint(endPoint.getX(),endPoint.getY());
   }

   /**
    *   Get the end point.
    *
    *   @return the end point.
    *
    */ 	
   public JFPoint getEndPoint(){
   	return m_endPoint;
   }


   //*****************  value setters *************************	

   /**
    *   Set value of a curve.
    *
    *   @param curve A curve object.
    */ 	
   public void setValue(Curve curve){
   	setValue(curve.getStartPoint(),curve.getControlPoint1(),curve.getControlPoint2(),curve.getEndPoint());
   }

   /**
    *   Set value of a curve.
    *
    *   @param startPoint Coordinates of start point.
    *   @param controlPoint1 Coordinates of control point1.
    *   @param controlPoint2 Coordinates of control point2.
    *   @param endPoint Coordinates of end point.
    *
    */ 	
   public void setValue(JFPoint startPoint, JFPoint controlPoint1, JFPoint controlPoint2, JFPoint endPoint){
   	setValue(startPoint.getX(),startPoint.getY(),
   		 controlPoint1.getX(),controlPoint1.getY(),
   		 controlPoint2.getX(),controlPoint2.getY(),
   		 endPoint.getX(),endPoint.getY());
   }

   /**
    *   Set value of a curve.
    *
    *   @param x1; y1 Coordinates of start point.
    *   @param x2; y2 Coordinates of control point1.
    *   @param x3; y3 Coordinates of control point2.
    *   @param x4; y4 Coordinates of end point.
    *
    */ 	
   public void setValue(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4){
   	m_startPoint.setValue(x1,y1);
   	m_controlPoint1.setValue(x2,y2);
   	m_controlPoint2.setValue(x3,y3);
   	m_endPoint.setValue(x4,y4);
   }


   /**
    *   Set value of a curve. We use default two control points here.
    *
    *   @param startX; startY Coordinates of start point.
    *   @param endX; endY Coordinates of end point.
    *
    */ 	
   public void setValue(double startX, double startY, double endX, double endY){
   	setValue(new JFPoint(startX,startY), new JFPoint(endX,endY));
   }

   /**
    *   Set value of a curve. We use default two control points here.
    *
    *   @param startPoint Coordinates of start point.
    *   @param endPoint Coordinates of end point.
    *
    */ 	
   public void setValue(JFPoint startPoint, JFPoint endPoint){
   	
   	//we will force an original rectangle to produce a new bezier curve.
   	//this rectangle will always has two short side which is half length from start point to end point,
   	//and the line segment from start point to end point is diagnal line of this rectangle.
   	//this rectangle will has four vertex:
   	//   left-top is equal as start point,
   	//   right-top is on the counter-clockwise of line from start to end,
   	//   right-bottom is equal as end point,
   	//   left-bottom is on the clockwise of line from start to end,
   	

	// the angle that right-top > left-top > right-bottom is ALPHA = 30 degrees.
	
	// set the slope of the line from start point to end point is k1
	// so we get the slope of the line from left-top to right-bottom is k2,
	// k2=(k1 + tan(ALPHA))/(1-k1 * tan(ALPHA))
	// the slope of the line from right-top to right-bottom is k3,
	// k3 = - 1/k2
	
	// we get the two equation for the lines that left-top to right-top and right-top to right-bottom
	// y=k2(x-x1) + y1
	// y=k3(x-x2) + y2
	// so x=(y2-k3 * x2 - y1 + k2 * x1)/(k2-k3)	 	
	
	//start point(left-top)	
	double x1	=startPoint.getX();
	double y1	=startPoint.getY();
	//end point(right-bottom)
	double x2	=endPoint.getX();
	double y2	=endPoint.getY();
		
	double ALPHA	=Angle.PI/6; //30 degrees
	//distance from start point to end point
	double dist		=startPoint.distance(endPoint);
	if ((float)dist==0)
		return;
		
	double halfDist		=dist/2;

	double rightTopX=0, rightTopY=0;

	if ((float)x1==(float)x2){
		if (y1>y2){
			rightTopX  =x1 + halfDist * Math.cos(ALPHA);
			rightTopY  =y2 - halfDist * Math.sin(ALPHA);
		}else{
			rightTopX  =x1 - halfDist * Math.cos(ALPHA);
			rightTopY  =y2 + halfDist * Math.sin(ALPHA);
		}		

	}else if ((float)y1==(float)y2){
		if (x1>x2){
			rightTopX  =x2 - halfDist * Math.sin(ALPHA);
			rightTopY  =y1 - halfDist * Math.cos(ALPHA);
		}else{
			rightTopX  =x2 + halfDist * Math.sin(ALPHA);
			rightTopY  =y1 + halfDist * Math.cos(ALPHA);
		}		

	}else{
		double k1	=startPoint.getSlope(endPoint);
		double divisor  =k1 + Math.tan(ALPHA);
		double dividend =1-k1 * Math.tan(ALPHA);
		if ((float)dividend==0){

⌨️ 快捷键说明

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