lineseg.java

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

JAVA
1,182
字号
/**
 *    $Id:LineSeg.java $
 *
 *    Copyright 2004 ~ 2005  JingFei International Cooperation LTD. All rights reserved. *
 */
package com.jfimagine.jfgraph.geom;


import java.awt.Graphics;

 
 /**
 * LineSeg class.  A class used to represent a line 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 LineSeg implements Cloneable {

  
   /**
    *   start endpoint of this line.
    */
   private	JFPoint	m_point1	=new JFPoint();

   /**
    *   end endpoint of this line.
    */
   private	JFPoint	m_point2	=new JFPoint();


   /**
    *   Get x coordiate of first endpoint.
    *
    *   @return  The x coordiate of first endpoint.
    *
    */ 	
   public double getX1(){
   	return m_point1.getX();
   }

   /**
    *   Set x coordiate of first endpoint.
    *
    *   @param val  A new x coordiate of first endpoint.
    *
    */ 	
   public void setX1(double val){
   	m_point1.setX(val);
   }

   /**
    *   Get y coordiate of first endpoint.
    *
    *   @return  The y coordiate of first endpoint.
    *
    */ 	
   public double getY1(){
   	return m_point1.getY();
   }

   /**
    *   Set y coordiate of first endpoint.
    *
    *   @param val  A new y coordiate of first endpoint.
    *
    */ 	
   public void setY1(double val){
   	m_point1.setY(val);
   }

   /**
    *   Get x coordiate of second endpoint.
    *
    *   @return  The x coordiate of second endpoint.
    *
    */ 	
   public double getX2(){
   	return m_point2.getX();
   }

   /**
    *   Set x coordiate of second endpoint.
    *
    *   @param val  A new x coordiate of second endpoint.
    *
    */ 	
   public void setX2(double val){
   	m_point2.setX(val);
   }

   /**
    *   Get y coordiate of second endpoint.
    *
    *   @return  The y coordiate of second endpoint.
    *
    */ 	
   public double getY2(){
   	return m_point2.getY();
   }

   /**
    *   Set y coordiate of second endpoint.
    *
    *   @param val  A new y coordiate of second endpoint.
    *
    */ 	
   public void setY2(double val){
   	m_point2.setY(val);
   }


   /**
    *   Get point1, i.e. the point at x1,y1
    *
    *   @return  The start point of this line.
    *
    */ 	
   public JFPoint getPoint1(){
   	return m_point1;
   }

   /**
    *   Set point1, i.e. the point at x1,y1
    *
    *   @param pnt	A new point.
    *
    */ 	
   public void setPoint1(JFPoint pnt){
   	m_point1.setValue(pnt);
   }

   /**
    *   Set point1, i.e. the point at x1,y1
    *
    *   @param x,&nbsp;y Coordinates of a new point.	
    *
    */ 	
   public void setPoint1(double x, double y){
   	m_point1.setValue(x,y);
   }

   /**
    *   Get point2, i.e. the point at x2,y2
    *
    *   @return  The end point of this line.
    *
    */ 	
   public JFPoint getPoint2(){
   	return m_point2;
   }

   /**
    *   Set point2, i.e. the point at x2,y2
    *
    *   @param pnt	A new point.
    *
    */ 	
   public void setPoint2(JFPoint pnt){
   	m_point2.setValue(pnt);
   }

   /**
    *   Set point2, i.e. the point at x2,y2
    *
    *   @param x,&nbsp;y Coordinates of a new point.	
    *
    */ 	
   public void setPoint2(double x, double y){
   	m_point2.setValue(x,y);
   }

   /**
    *   Set a line's value by a line segment.
    *
    *   @param lineSeg  A new line segment.
    *
    */ 	
   public void setValue(LineSeg line){
   	if (line!=null){
   		setValue(line.getPoint1(),line.getPoint2());
   	}
   }

	
   /**
    *   Set a line's value by two points.
    *
    *   @param point1 The start point
    *   @param point2 The end point
    *
    */ 	
   public void setValue(JFPoint point1, JFPoint point2){
   	m_point1.setValue(point1);
   	m_point2.setValue(point2);
   }


   /**
    *   Set a line's value by two points.
    *
    *   @param x1,&nbsp;y1 The start point
    *   @param x2,&nbsp;y2 The end point
    *
    */ 	
   public void setValue(double x1, double y1, double x2, double y2){
   	m_point1.setValue(x1,y1);
   	m_point2.setValue(x2,y2);
   }


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

   /**
    *   Constructor for LineSeg.
    *
    *   @param x1,y1  Coordiates of first endpoint.
    *
    *   @param x2,y2  Coordiates of second endpoint.
    *
    */ 	
   public LineSeg(double x1, double y1, double x2, double y2){
   	setValue(x1,y1,x2,y2);
   }

   /**
    *   Constructor for LineSeg.
    *
    *   @param startPoint  The first endpoint.
    *
    *   @param endPoint  The second endpoint.
    *
    */ 	
   public LineSeg(JFPoint startPoint, JFPoint endPoint){
   	setValue(startPoint,endPoint);
   }

   /**
    *   Constructor for LineSeg.
    *
    *   @param lineSeg A JFLine Segment.
    *
    */ 	
   public LineSeg(LineSeg lineSeg){
   	setValue(lineSeg);
   }

   /**
    *   Get the bounds of this line segment.
    *
    *   @return The bounds rectangle.
    *
    */ 	
   public Rect getBounds(){
   	return new Rect(Math.min(getX1(),getX2()),Math.min(getY1(),getY2()),
   			Math.abs(getX1()-getX2()), Math.abs(getY1()-getY2()));
   }

   /**
    *   Get the length of this line.
    *
    *   @return the length.
    *
    */ 	
   public double getLength(){
   	return getLength(m_point1.getX(),m_point1.getY(),m_point2.getX(),m_point2.getY());
   }

   /**
    *   Get the length between two endpoints of a line.
    *
    *   @param pnt1 One endpoint of the line.
    *
    *   @param pnt2 Other endpoint of the line.
    *
    *   @return the length.
    *
    */ 	
   public static double getLength(JFPoint pnt1, JFPoint pnt2){
   	return getLength(pnt1.getX(),pnt1.getY(),pnt2.getX(),pnt2.getY());
   }

   /**
    *   Get the length between two endpoints of a line.
    *
    *   @param x1,&nbsp;y1 One endpoint of the line.
    *
    *   @param x2,&nbsp;y2 Other endpoint of the line.
    *
    *   @return the length.
    *
    */ 	
   public static double getLength(double x1, double y1, double x2, double y2){
   	return Math.sqrt(Math.pow((x2 - x1),2) + Math.pow((y2-y1),2));
   }


   /**
    *   Move this line 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_point1.moveBy(x,y);
   	m_point2.moveBy(x,y);
   }

   /**
    *   Get a center point on this line.
    *
    *   @return The center point.
    *
    */ 	
   public JFPoint getCenter(){
	return new JFPoint((m_point1.getX()+m_point2.getX())/2, (m_point1.getY()+m_point2.getY())/2);
   }


   /**
    *   Rotate this line by an angle theta.
    *
    *   @param theta A rotate angle.
    *
    */ 	
   public void rotateBy(double theta){
   	  
   	  //the center point on this line
	  double centerX	=(m_point1.getX() + m_point2.getX())/2;
	  double centerY	=(m_point1.getY() + m_point2.getY())/2;
	  
	  rotateBy(centerX,centerY,theta);	
   }


   /**
    *   Rotate this line 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)
   		return;
	
	rotateBy(pnt.getX(),pnt.getY(),theta);   	
   }

   /**
    *   Rotate this line by a specified point and an angle theta.
    *
    *   @param baseX,&nbsp;baseY A rotate center coordinates.
    *
    *   @param theta A rotate angle.
    *
    */ 	
   public void rotateBy(double baseX,double baseY, double theta){
   	m_point1.rotateBy(baseX,baseY,theta);
   	m_point2.rotateBy(baseX,baseY,theta);
   }

   /**
    *   Mirror this line by a central x coordinate of this line. We make a up-down flip here.
    */ 	
   public void mirrorBy(){
   	double centralX	=(m_point1.getX() + m_point2.getX())/2;
   	mirrorBy(centralX);
   }

   /**
    *   Mirror this line by a x coordinate. We make a left-right mirror here.
    *
    *   @param baseX  A mirror base x coordinate.
    *
    */ 	
   public void mirrorBy(double baseX){
   	m_point1.mirrorBy(baseX);
   	m_point2.mirrorBy(baseX);
   }

   /**

⌨️ 快捷键说明

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