Slick Forums

Discuss the Slick 2D Library
It is currently Fri May 24, 2013 10:39 am

All times are UTC




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

Joined: Fri Jul 20, 2007 9:25 am
Posts: 410
Location: Croatia
use circle equation to greatly improve speed on Circle.contains(x, y):

Code:
    @Override
    public boolean contains(float x, float y)
    {
        return (x - getX()) * (x - getX()) + (y - getY()) * (y - getY()) < getRadius() * getRadius();
    }


the results were (details in this this topic) 200x faster calculations then
Code:
Circle.contains(new Circle(x,y,0));

which is painfully slow

if there were a Point Shape (I'm wondering why is it missing...) the same equation would simply apply for intersection also but with "=" instead of "<"


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

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

Kev


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 17, 2011 12:22 pm 
Offline
Regular

Joined: Sat Aug 16, 2008 7:04 pm
Posts: 117
The new method is not correct.

Proof:
Code:
System.out.println("contains? " + new Circle(53, 47, 4).contains(46, 44));

returns
Code:
contains? true


53 and 46 are already x-dist 7 from each other and the radius is 4 so...


I googled a bit and found this:
http://stackoverflow.com/questions/4811 ... e-a-circle

So the error was getX/Y instead of getCenterX/Y.

And because I dont know the operator orderings I think we may just move sqrdist to a local variable or put another pair of brackets around it

Fixed algorithm:

float sqrdist = (x - getCenterX()) * (x - getCenterX()) + (y - getCenterY()) * (y - getCenterY());
return sqrdist < (getRadius() * getRadius());

Patch file for Circle.java:
Code:
Index: src/org/newdawn/slick/geom/Circle.java
===================================================================
--- src/org/newdawn/slick/geom/Circle.java   (revision 1556)
+++ src/org/newdawn/slick/geom/Circle.java   (working copy)
@@ -118,7 +118,9 @@
    */
     public boolean contains(float x, float y)
     {
-        return (x - getX()) * (x - getX()) + (y - getY()) * (y - getY()) < getRadius() * getRadius();
+      float sqrdist = (x - getCenterX()) * (x - getCenterX())
+            + (y - getCenterY()) * (y - getCenterY());
+      return sqrdist < (getRadius() * getRadius());
     }
     
     /**

_________________
Gamedev link-collection (german page)
My Gamedev Blog
Play Cyber Dungeon Quest!


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


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