Determining if a point occurs within a sphere

C439d07239bb78b3d02dddc437ef0656
0
Midspec 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.

B2d356f97a3d0dec8ae2d8c1e1fa1c2d
0
Nerd_Skywalker 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;
}
99f6aeec9715bb034bba93ba2a7eb360
0
Nick 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²

C439d07239bb78b3d02dddc437ef0656
0
Midspec 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!

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 168 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.

C439d07239bb78b3d02dddc437ef0656
0
Midspec 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.