polyline.java

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

JAVA
1,251
字号
  				i++;
  				node	=getNode(i);
  				if (node==null){
  					if (i>=m_nodeList.size()){
  						lastNode	=getNode(m_nodeList.size()-2);
  						node		=getNode(0);
  					}else
  			 	  		break;
  			 	}
  			}
  			
 			
  			line.setPoint1(lastNode);
  			line.setPoint2(node);
  			
  			if (lastNode.getY()!=node.getY()){
  				if (radiusLine.contains(lastNode)){
  					if (lastNode.getY()>node.getY()){
  						intersectNum++;
  					}

  				}else if (radiusLine.contains(node)){
  					if (node.getY()>lastNode.getY()){
  						intersectNum++;
  					}

  				}else if (line.intersects(radiusLine)){
  					intersectNum++;
  				}
  			}	
			
			
  			lastNode	=node;
  			i++;
  		}
  		
  		return intersectNum%2==1;
  	}
  	
  	return false;
   }

   /**
    *   Pick a line segment of this polyline 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){
        List lineList   =pickLineList(x,y,pickOffset,false);
        if (lineList!=null && lineList.size()>0){
                try{
                        return  (LineSeg)lineList.get(0);
                }catch(Exception e){
                }
        }
        
        return null;
   }

   /**
    *   Pick a line segment of this polyline by specified a point with a pickoffset, and start point and end point restricted.
    *   @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.
    *   @param startPoint   Start point of a line segment.
    *   @param endPoint  End point of a line segment.
    *
    *   @return A line segment picked.
    *
    */ 	
   public LineSeg pickLine(double x, double y, double pickOffset,JFPoint startPoint, JFPoint endPoint){
        List lineList   =pickLineList(x,y,pickOffset,true);
        if (lineList==null || lineList.size()==0){
                return null;
        }

        LineSeg line;
        JFPoint pnt1,pnt2;

        //complete same start point and end point                
        Iterator it     =lineList.iterator();
        while (it!=null && it.hasNext()){
                line    =(LineSeg)it.next();
                pnt1    =line.getPoint1();
                pnt2    =line.getPoint2();
                if (pnt1.equals(startPoint,true) && pnt2.equals(endPoint,true)||
                    pnt2.equals(startPoint,true) && pnt1.equals(endPoint,true)){
                        return line;
                }
        }

        //partly same start point or end point                
        it     =lineList.iterator();
        while (it!=null && it.hasNext()){
                line    =(LineSeg)it.next();
                if (line.contains(startPoint) && line.contains(endPoint)){
                        return line;
                }
        }


        //a little same start point or end point                
        it     =lineList.iterator();
        while (it!=null && it.hasNext()){
                line    =(LineSeg)it.next();
                if (line.contains(startPoint,GeomConst.PICK_OFFSET) && line.contains(endPoint,GeomConst.PICK_OFFSET)){
                        return line;
                }
        }
        
        //return one line segment of the list.
        try{
                line    =(LineSeg)lineList.get(0);

        }catch(Exception e){
                line    =null;
        }

        return line;
   }




   /**
    *   Pick a line segment list of this polyline 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.
    *   @param pickAll Pick all line matched.
    *   @return A line segment list picked.
    *
    */ 	
   private List pickLineList(double x, double y,double pickOffset,boolean pickAll){
        List ret        =new ArrayList();
   	LineSeg line	=new LineSeg();
   	JFPointNode lastNode=getNode(0);
   	JFPointNode node;
   	if (lastNode==null)
   		return ret;
  	
  	
  	int i=1;
  	while (i<m_nodeList.size()){
  		node=getNode(i);
  		if (node.getNodeType()==JFPointNode.NODETYPE_MIDDLE){
  			i++;
  			node	=getNode(i);
  			if (node==null){
  				if (i>=m_nodeList.size() && isPolygon()){
  					lastNode	=getNode(m_nodeList.size()-2);
  					node		=getNode(0);
  				}else
  			   		break;
  			}
  		}
  			
  		line.setPoint1(lastNode);
  		line.setPoint2(node);
  		if (line.contains(x,y,pickOffset)){
  			ret.add(new LineSeg(line));
  			if (!pickAll)
  			        break;
  		}
  		lastNode  =node;
  		i++;
  	} 		

  	
  	return ret;
   }



   /**
    *   Test if current polyline can add a port according to the position x,y.
    *   @param x   X coordinate of the port.
    *   @param y   Y coordinate of the port.
    *   @param startPoint,endPoint,portPoint Three major parameters of a port.
    *
    *   @return True if can add a new port, false otherwise.
    *
    */ 	
   public boolean canAddPort(double x, double y,JFPoint startPoint, JFPoint endPoint, JFPoint portPoint){
	LineSeg line	=pickLine(x,y,GeomConst.PICK_OFFSET);
	if (line==null)
		return false;
	
	startPoint.setValue(line.getPoint1());
	endPoint.setValue(line.getPoint2());
	portPoint.setValue(line.uprightFoot(x,y));
	return true;
   }


 
    /**
     * Tests if the specified line segment intersects the this 
     * <code>PolyLine</code>.
     * @param l the specified {@link PolyLine} to test for intersection
     * with this <code>PolyLine</code>
     * @return <code>true</code> if the specified <code>PolyLine</code>
     * intersects this <code>PolyLine</code>;
     * <code>false</code> otherwise.
     * @since 1.2
     */
    public boolean intersects(LineSeg l) {
    	if (l==null)
    		return false;
    	else
		return intersects(l.getX1(), l.getY1(), l.getX2(), l.getY2());
    }


    /**
     * Tests if the specified line segment intersects this
     * <code>PolyLine</code>.
     *
     * @param startPoint the first endpoint of the specified
     * line segment
     * @param endPoint the second endpoint of the specified
     * line segment
     * @return <code>true</code> if the specified line segment intersects
     * the this <code>PolyLine</code>; <code>false</code>
     * otherwise.
     */
    public boolean intersects(JFPointNode startPoint, JFPointNode endPoint) {
    	return intersects(startPoint.getX(),startPoint.getY(),endPoint.getX(),endPoint.getY());
   }

    /**
     * Tests if the specified line segment intersects this
     * <code>PolyLine</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 this <code>PolyLine</code>; <code>false</code>
     * otherwise.
     */
    public boolean intersects(double x1, double y1, double x2, double y2) {
   	LineSeg line	=new LineSeg();
   	JFPointNode lastNode=getNode(0);
   	JFPointNode node;
   	if (lastNode==null)
   		return false;
   	
   	int i=0;
   	while (i<m_nodeList.size()){
  		node=getNode(i);
  		if (node.getNodeType()==JFPointNode.NODETYPE_MIDDLE){
  			i++;
  			node	=getNode(i);
  			if (node==null){
  				if (i>=m_nodeList.size() && isPolygon()){
  					lastNode	=getNode(m_nodeList.size()-2);
  					node		=getNode(0);
  				}else
  			   		break;
  			}
  		}
  		line.setPoint1(lastNode);
  		line.setPoint2(node);
  		if (line.intersects(x1,y1,x2,y2))
  			return true;
  		lastNode  =node;
  		i++;
  	} 
	
	return false;
   }

    /**
     * Tests if the specified rectangle intersects the interior of this
     * <code>PolyLine</code>.
     * @param rect the specified {@link PolyLine} to test for intersection
     * with the interior of this <code>PolyLine</code>
     * @return <code>true</code> if the specified <code>PolyLine</code>
     * intersects the interior of this <code>PolyLine</code>;
     * <code>false</code> otherwise.
     */
    public boolean intersects(Rect rect) {
	
	//test if any node of this polyline is inside the rectangle.
	Iterator it	=m_nodeList.iterator();
	while (it!=null && it.hasNext()){
		JFPointNode node=(JFPointNode)it.next();
		if (rect.contains(node))
			return true;
	}

	//test each side of this polyline intersects with the rectangle.
   	LineSeg line	=new LineSeg();
   	JFPointNode lastNode=getNode(0);
   	JFPointNode node;
   	if (lastNode==null)
   		return false;
	
	int i=1;   		
  	while(i<m_nodeList.size()){
  		node=getNode(i);
  		if (node.getNodeType()==JFPointNode.NODETYPE_MIDDLE){
  			i++;
  			node	=getNode(i);
  			if (node==null){
  				if (i>=m_nodeList.size() && isPolygon()){
  					lastNode	=getNode(m_nodeList.size()-2);
  					node		=getNode(0);
  				}else
  			   		break;
  			}
  		}
  		
  		line.setPoint1(lastNode);
  		line.setPoint2(node);
  		if (line.intersects(rect))
  			return true;
  		lastNode  =node;
  		i++;
  	} 
  	
  	return false;
  	
    }


   /**
    *   Convert this object to String 
    * 
    *   @return  An string represents the content of the object
    *
    */ 	
   public String toString(){
  	StringBuffer buf =new StringBuffer();

	Iterator it	=m_nodeList.iterator();
	int i=0;
	while (it!=null && it.hasNext()){
		JFPointNode node=(JFPointNode)it.next();
		i++;
		buf.append("\nnode");
		buf.append(i);
		buf.append("=");
		buf.append(node.toString());
	}
  	
  	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 PolyLine(this);
  		
	}catch(Exception e){
		throw new CloneNotSupportedException(e.getMessage());
	}
  }


   /**
    *   Returns the hashcode for this Object.
    * 
    *   @return hash code for this Point2D.
    *
    */ 	
  public int hashCode(){
	int code=0;
	
	//test if any node of this polyline is inside the rectangle.
	Iterator it	=m_nodeList.iterator();
	while (it!=null && it.hasNext()){
		JFPointNode node=(JFPointNode)it.next();
		code	^=node.hashCode();
	}
	
	return code;	       	
  }


   /**
    *   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){

      if (obj == this)
             	return true;

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

      PolyLine	line	=(PolyLine)obj;
      
      return	m_nodeList.equals(line.m_nodeList);
  }




}

⌨️ 快捷键说明

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