Java Code Examples for java.awt.geom.Line2D#intersectsLine()

The following examples show how to use java.awt.geom.Line2D#intersectsLine() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LocalAreaUtil.java    From mars-sim with GNU General Public License v3.0 6 votes vote down vote up
public static Set<Point2D> getLinePathCollisionPoints(Line2D line, LocalBoundedObject object) {

		Set<Point2D> result = new HashSet<Point2D>();

		Iterator<Line2D> i = getLocalBoundedObjectLineSegments(object).iterator();
		while (i.hasNext()) {
			Line2D lineSegment = i.next();
			if (line.intersectsLine(lineSegment)) {

				Point2D intersectionPt = getLineIntersectionPoint(line, lineSegment);
				result.add(intersectionPt);
			}
		}

		return result;
	}
 
Example 2
Source File: GeometryUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param line1
 * @param line2
 * @param extrapolate
 * @return
 */
public static Point2D getIntersectionPoint(Line2D line1, Line2D line2,
        boolean extrapolate) {
    if (extrapolate || line1.intersectsLine(line2)) {
        float x1 = (float) line2.getX1();
        float y1 = (float) line2.getY1();
        float x2 = (float) line2.getX2();
        float y2 = (float) line2.getY2();

        float xp1 = (float) line1.getX1();
        float yp1 = (float) line1.getY1();
        float xp2 = (float) line1.getX2();
        float yp2 = (float) line1.getY2();

        float y = 0;
        float x = 0;
        float dy = y2 - y1;
        float s = (x2 - x1) / dy;

        float dpy = yp2 - yp1;
        float sp = (xp2 - xp1) / dpy;

        if (y1 == y2) {
            if (dpy == 0) {
                return null;
            }
            y = y1;
            x = xp1 + sp * (y - yp1);
        } else if (yp1 == yp2) {
            if (dy == 0) {
                return null;
            }
            y = yp1;
            x = x1 + s * (y - y1);
        } else {
            if (dy == 0 || dpy == 0 || (s - sp) == 0) {
                return null;
            }
            y = (xp1 - x1 + s * y1 - sp * yp1) / (s - sp);
            x = x1 + s * (y - y1);
        }

        return new Point2D.Float(x, y);
    }

    return null;
}
 
Example 3
Source File: Wall.java    From BLELocalization with MIT License 5 votes vote down vote up
public double computeNumIntersect(State locTrans, State locReceive){
	Point2D pT = locationToPoint2D(locTrans);
	Point2D pR = locationToPoint2D(locReceive);
	Line2D lineOfSight = new Line2D.Double(pT, pR);
	int count = 0;
	Line2D wallLine = this.getLine2D();
	if(lineOfSight.intersectsLine(wallLine)){
		count ++ ;
	}
	return count;
}
 
Example 4
Source File: Intersection.java    From dsworkbench with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the intersection point of two lines.
 *
 * @param   line1   First line
 * @param   line2   Second line
 * @return  The Point object where the two lines intersect. This method
 * returns null if the two lines do not intersect.
 * @throws  <tt>MultipleIntersectionException</tt> when the two lines
 * have more than one intersection point.
 */
static public Point getIntersection(Line2D line1, Line2D line2)
        throws Exception {
    double dyline1, dxline1;
    double dyline2, dxline2, e, f;
    double x1line1, y1line1, x2line1, y2line1;
    double x1line2, y1line2, x2line2, y2line2;

    if (!line1.intersectsLine(line2)) {
        return null;
    }

    /* first, check to see if the segments intersect by parameterization
    on s and t; if s and t are both between [0,1], then the
    segments intersect */
    x1line1 =  line1.getX1();
    y1line1 =  line1.getY1();
    x2line1 =  line1.getX2();
    y2line1 =  line1.getY2();

    x1line2 =  line2.getX1();
    y1line2 =  line2.getY1();
    x2line2 =  line2.getX2();
    y2line2 =  line2.getY2();

    /* check to see if the segments have any endpoints in common. If they do,
    then return the endpoints as the intersection point */
    if ((x1line1 == x1line2) && (y1line1 == y1line2)) {
        return (new Point((int) x1line1, (int) y1line1));
    }
    if ((x1line1 == x2line2) && (y1line1 == y2line2)) {
        return (new Point((int) x1line1, (int) y1line1));
    }
    if ((x2line1 == x1line2) && (y2line1 == y1line2)) {
        return (new Point((int) x2line1, (int) y2line1));
    }
    if ((x2line1 == x2line2) && (y2line1 == y2line2)) {
        return (new Point((int) x2line1, (int) y2line1));
    }

    dyline1 = -(y2line1 - y1line1);
    dxline1 = x2line1 - x1line1;

    dyline2 = -(y2line2 - y1line2);
    dxline2 = x2line2 - x1line2;

    e = -(dyline1 * x1line1) - (dxline1 * y1line1);
    f = -(dyline2 * x1line2) - (dxline2 * y1line2);

    /* compute the intersection point using
    ax+by+e = 0 and cx+dy+f = 0
    
    If there is more than 1 intersection point between two lines,
     */
    if ((dyline1 * dxline2 - dyline2 * dxline1) == 0) {
        throw new Exception("ZERO!");
    }
    return (new Point(
            (int) (-(e * dxline2 - dxline1 * f) / (dyline1 * dxline2 - dyline2 * dxline1)),
            (int) (-(dyline1 * f - dyline2 * e) / (dyline1 * dxline2 - dyline2 * dxline1))));
}
 
Example 5
Source File: clsUtilityGE.java    From mil-sym-java with Apache License 2.0 4 votes vote down vote up
/**
 * tests of a Line2D intersects a polygon by using line.intersectsLine on each segment of the polygon
 * assumes clip clipping area was parsed to shift points of vertical segments to make them not vertical
 * @param line a clipping line in the clipping polygon
 * @param clipPts array of clip points assumed to be closed
 * @return true if the line intersects the clip bounds
 */
private static boolean lineIntersectsClipArea(Line2D line, 
        ArrayList<Point2D> clipPts)
{
    boolean result=false;
    try
    {
        int j=0;           
        
        //test if polygon contains an end point
        Polygon poly=new Polygon();
        for(j=0;j<clipPts.size();j++)            
            poly.addPoint((int)clipPts.get(j).getX(),(int)clipPts.get(j).getY());
        
        if(poly.contains(line.getX1(),line.getY1()))
            return true;
        if(poly.contains(line.getX2(),line.getY2()))
            return true;
        //end section
        
        Line2D currentSegment=null;
        for(j=0;j<clipPts.size()-1;j++)
        {
            currentSegment=new Line2D.Double(clipPts.get(j).getX(),clipPts.get(j).getY(),clipPts.get(j+1).getX(),clipPts.get(j+1).getY());
            if(line.intersectsLine(currentSegment)==true)
                return true;            
        }
        //if the clipPts are not closed then the above loop did not test the closing segment            
        Point2D pt0=clipPts.get(0);
        Point2D ptLast=clipPts.get(clipPts.size()-1);
        //int n=clipPts.size()-1;            
        if(pt0.getX()!=ptLast.getX() || pt0.getY()!=ptLast.getY())
        {
            //currentSegment=new Line2D.Double(clipPts.get(n).getX(),clipPts.get(n).getY(),clipPts.get(0).getX(),clipPts.get(0).getY());
            currentSegment=new Line2D.Double(ptLast.getX(),ptLast.getY(),pt0.getX(),pt0.getY());
            if(line.intersectsLine(currentSegment)==true)
                return true;                            
        }
    }
    catch (Exception exc) {
        ErrorLogger.LogException(_className, "lineIntersectsClipArea",
                new RendererException("Failed inside lineIntersectsClipArea", exc));
    }
    return result;
}