Slick Forums

Discuss the Slick 2D Library
It is currently Wed Jun 19, 2013 3:11 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Wed Dec 08, 2010 1:28 pm 
Offline
Oldbie
User avatar

Joined: Fri Jul 20, 2007 9:25 am
Posts: 410
Location: Croatia
Circle.contains(Line) and Circle.intersects(Line) are both very slow as they resolve to shape level calculations

override Circle.contains() and .intersects() so it accepts Line directly, and the formulas are simple as it seems to me:
Circle.contains(Line): both line points must be inside circle
Circle.intersects(Line): exactly 1 must be outside, and 1 inside


Since Circle.contains(x,y) is really fast, this should be also.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 08, 2010 1:52 pm 
Offline
Slick Zombie

Joined: Fri Jan 29, 2010 7:02 pm
Posts: 1188
Circle.intersects(Line) is more complex. You need to compute the distance of the circle center to the line and compare that with the radius:
http://mathworld.wolfram.com/Circle-LineIntersection.html

_________________
TWL - The Themable Widget Library


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 08, 2010 2:02 pm 
Offline
Oldbie
User avatar

Joined: Fri Jul 20, 2007 9:25 am
Posts: 410
Location: Croatia
oh yeah, haven't really though of that case :oops: ... but I still think it can be done faster then with shape intersection. I'll take a look at the formula.

Circle.contains(Line):
Code:
   /**
    * Check if circle contains the line
    * @param line Line to check against
    * @return True if line inside circle
    */
   private boolean contains(Line line) {
        return contains(line.getX1(), line.getY1()) && contains(line.getX2(), line.getY2());
   }


edit:
I've did the formula, it's much faster.

Circle.intersects(Line):
Code:
    /**
     * Check if circle touches a line.
     * @param other The line to check against
     * @return True if they touch
     */
    private boolean intersects(Line other) {
        // put it nicely into vectors
        Vector2f lineSegmentStart = new Vector2f(other.getX1(), other.getY1());
        Vector2f lineSegmentEnd = new Vector2f(other.getX2(), other.getY2());
        Vector2f circleCenter = new Vector2f(getCenterX(), getCenterY());

        // calculate point on line closest to the circle center and then
        // compare radius to distance to the point for intersection result
        Vector2f closest;
        Vector2f segv = lineSegmentEnd.copy().sub(lineSegmentStart);
        Vector2f ptv = circleCenter.copy().sub(lineSegmentStart);
        float segvLength = segv.length();
        float projvl = ptv.dot(segv) / segvLength;
        if (projvl < 0)
        {
            closest = lineSegmentStart;
        }
        else if (projvl > segvLength)
        {
            closest = lineSegmentEnd;
        }
        else
        {
            Vector2f projv = segv.copy().scale(projvl / segvLength);
            closest = lineSegmentStart.copy().add(projv);
        }
        boolean intersects = circleCenter.copy().sub(closest).lengthSquared() <= getRadius()*getRadius();
       
        return intersects;
    }
}


Line.intersects() override:
Code:
   public boolean intersects(Shape shape)
    {
        if (shape instanceof Circle)
        {
            return shape.intersects(this);
        }
        return super.intersects(shape);
    }


Last edited by Kova on Wed Dec 08, 2010 3:44 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 09, 2011 1:47 pm 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
Resolved in SVN

Kev


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group