Slick Forums

Discuss the Slick 2D Library
It is currently Thu May 23, 2013 12:12 am

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: hashCode for Vec2
PostPosted: Fri Jan 02, 2009 5:25 am 
Offline

Joined: Sat Oct 04, 2008 3:58 am
Posts: 99
Here's a quickie you can copy/paste if you're feeling lazy :)
EDIT updated to use AndyKorth's hashCode function since mine was really really bad :D
Code:
      public boolean equals(Object obj)
      {
         if(obj instanceof Vec2)
         {
            Vec2 v = (Vec2) obj;
            if(this.x == v.x && this.y == v.y)
               return true;
         }
         return false;
      }
      @Override
      public int hashCode()
      {
         return 997 * ((int)x) ^ 991 * ((int)y); //large primes!
      }


Last edited by FireSlash on Sat Jan 03, 2009 2:14 am, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2009 5:40 am 
Offline
Oldbie

Joined: Tue Jun 17, 2008 5:11 pm
Posts: 336
Would the hash work for the following vectors?

Code:
Vector v1 = new Vector(0, 1);
Vector v2 = new Vector(1, 0);


Or would we expect those to be identical?

_________________
My website about game development http://anotherearlymorning.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2009 2:49 pm 
Offline

Joined: Sat Oct 04, 2008 3:58 am
Posts: 99
manunderground wrote:
Would the hash work for the following vectors?

Code:
Vector v1 = new Vector(0, 1);
Vector v2 = new Vector(1, 0);


Or would we expect those to be identical?

Yeah, I never said it was a good hashing function :wink:


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2009 10:18 pm 
Offline

Joined: Thu Nov 20, 2008 5:16 am
Posts: 76
Code:
    @Override
    public int hashCode() {
        return 997 * x ^ 991 * y; //large primes!
    }


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 03, 2009 2:12 am 
Offline

Joined: Sat Oct 04, 2008 3:58 am
Posts: 99
AndyKorth wrote:
Code:
    @Override
    public int hashCode() {
        return 997 * x ^ 991 * y; //large primes!
    }

Edited my post with your function, but I had to cast x and y as ints though, which will provide some accuracy issues for people hashing against close points.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 03, 2009 2:20 am 
Offline

Joined: Thu Nov 20, 2008 5:16 am
Posts: 76
Oh... Yeah, that was from a tile based game, so everything was on a tile.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2009 11:10 pm 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
Where's Vec2 ?

I've updated Vector2f's hashCode() for the large primes though.

Kev


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 17, 2009 12:40 am 
Offline

Joined: Sat Oct 04, 2008 3:58 am
Posts: 99
Haha, Vec2 is part of jbox2d.. :shock:

See kids? THIS is what happens when you start mixing libs together. :cry:


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 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