Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 9th, 2003, 07:24 PM
zygimantas zygimantas is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 16 zygimantas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
SQL, sphere, coords

Table of coords: id,x,y,z

1|2442|-4352|345
2|-435|55|0
3|3|234|-455
...


How to select all rows where all coords are in the sphere? E.g. if we have sphere with center at position 0:0:0 and radius of 500, only 2nd and 3rd row will be returned.

Thanks.

Reply With Quote
  #2  
Old August 11th, 2003, 05:16 PM
epl epl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: Dublin
Posts: 413 epl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 18 m 18 sec
Reputation Power: 8
return those with x squared plus y squared plus z squared less than r squared (25,000)

if speed / efficiency is important then you can first refuse anything with x greater than r, y greater than r or z greater than r and accept anything with (i think - you'll have to verify this yourself) x, y or z squared less than one third of r etc.

Reply With Quote
  #3  
Old August 11th, 2003, 06:57 PM
zygimantas zygimantas is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 16 zygimantas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks!

Reply With Quote
  #4  
Old August 11th, 2003, 07:01 PM
epl epl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: Dublin
Posts: 413 epl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 18 m 18 sec
Reputation Power: 8
you're welcome!

note: the final line above should read "less than one third of r squared etc." not "less than one third of r etc."

Reply With Quote
  #5  
Old August 11th, 2003, 07:51 PM
zygimantas zygimantas is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 16 zygimantas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thans again for the idea. I have found couple mistakes.
At first, we should take absolute values

abs(x) < 500 AND abs(y) < 500 AND abs(z) < 500
AND pow(x,2) + pow(y,2) + pow(z,2) < 500

And maybe note should be fixed, because:
3|-2|-2 r=4
3^2=9 not less than 4/3
maybe 3^-2 should be less than 4/3?

Reply With Quote
  #6  
Old August 11th, 2003, 07:58 PM
zygimantas zygimantas is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 16 zygimantas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Just finished.

abs(x) < 500 AND abs(y) < 500 AND abs(z) < 500
AND sqrt(abs(x)) < 500/3 AND sqrt(abs(y)) < 500/3 AND sqrt(abs(z)) < 500/3
AND pow(x,2) + pow(y,2) + pow(z,2) < 500

Unfortunatelly, pow(x,2) + pow(y,2) + pow(z,2) < 500 if enough to select all results and other conditions are optional and don't give any processing power.

Reply With Quote
  #7  
Old August 12th, 2003, 04:03 AM
epl epl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: Dublin
Posts: 413 epl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 18 m 18 sec
Reputation Power: 8
ok, here it is in c syntax if that's easier for you to read:
Code:
bool hitTest(int x,int y,int z,int r)
 {if (x<0) {x=-x;}
  if (y<0) {y=-y;}
  if (z<0) {z=-z;}

  if (r<x) {return false;} // outside outer / containing cube
  if (r<y) {return false;} // outside outer / containing cube
  if (r<z) {return false;} // outside outer / containing cube

  x*=x;y*=y;z*=z;r*=r;

  if ((3*x<r) // leave this bit out as is, or optimise it and include
    &&(3*y<r)
    &&(3*z<r)) {return true;} // inside inner cube (cube's not ideal shape here...)

  return (x+y+z<r);
 }

Last edited by epl : August 12th, 2003 at 04:18 AM.

Reply With Quote
  #8  
Old August 12th, 2003, 04:25 AM
epl epl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: Dublin
Posts: 413 epl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 18 m 18 sec
Reputation Power: 8
this also uses a rotated cube (ie like a diamond) as a second inner shape, for illustration
Code:
bool hitTest(int x,int y,int z,int r)
 {if (x<0) {x=-x;}
  if (y<0) {y=-y;}
  if (z<0) {z=-z;}

  if ((x+y+z<r) {return true;} // inside inner diamond

  if (r<x) {return false;} // outside outer / containing cube
  if (r<y) {return false;} // outside outer / containing cube
  if (r<z) {return false;} // outside outer / containing cube

  x*=x;y*=y;z*=z;r*=r;

  if ((3*x<r) // leave this bit out as is, or optimise it and include
    &&(3*y<r)
    &&(3*z<r)) {return true;} // inside inner cube 

  return (x+y+z<r);
 }

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > SQL, sphere, coords


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT