0
101 May 28, 2010 at 19:20

Does anyone know of an algorithm or code sample that can determine if a point in 3d (x,y,z axis) space occurs within a 3d sphere who’s center point and radius is known?

#### 5 Replies

Please log in or register to post a reply.

0
101 May 28, 2010 at 19:26

Psuedocode:

bool Sphere::inside(Vector3 point) {
if(distance_from(this.center, point) <= this.radius) {
return true;
}
return false;
}

0
102 May 28, 2010 at 21:06

Computing the distance is expensive as it requires a square root. However, both sides of the inequality can be squared so you lose the square root and end up with basically this:

(x - x0)² + (y - y0)² + (z - z0)² <= r²

0
101 May 29, 2010 at 19:56

@Nick

Computing the distance is expensive as it requires a square root. However, both sides of the inequality can be squared so you lose the square root and end up with basically this: (x - x0)² + (y - y0)² + (z - z0)² <= r²

Ahh I see. you are essentially applying a pythagorean theorem approach twice. once applied to a triangle who’s two sides are along the x and y planes, and once using either the x or y plane and z plane.

Very nice. Thanx!

0
165 May 29, 2010 at 19:58

Yep, that’s right. That’s the standard way to measure distance between two points in 3D space.

Since you seem to be new to 3D math, you should check out vectors and matrices.

0
101 May 29, 2010 at 20:52

@Reedbeta

Yep, that’s right. That’s the standard way to measure distance between two points in 3D space. Since you seem to be new to 3D math, you should check out vectors and matrices.

I am very new to 3d math in concept. I am not new to programming though. Most of the math I’ve done in code has involved business logic. I’ve taken an interest in 3d graphics only recently. I’ve been considering attempting to learn how to code with a 3d engine, but before doing that, I thought it’d be best to learn the basics first. That link to vectors and matrices is pretty much where I am at on the learning curve thus far.