Given start and radius, how do you know if p (x, y, r) is inside the torus?

I'm trying to create a 3D torus in a block game, so I need to evaluate the range of coordinates to see if they are inside the torus. The way I did this with the spheres:

shapefunc = function (pos,fields) map = {} pos.x = math.floor(pos.x+0.5) pos.y = math.floor(pos.y+0.5) pos.z = math.floor(pos.z+0.5) for x=-fields.radius,fields.radius do for y=-fields.radius,fields.radius do for z=-fields.radius,fields.radius do if x*x+y*y+z*z <= fields.radius*fields.radius then table.insert(map,{x=pos.x+x,y=pos.y+y,z=pos.z+z}) end end end end return map end 

the given height (along the y axis), small and large radius (along the xz axes) and the starting point, none of the evaluation expressions that I tried, gave me anything close to the torus.

+4
source share
2 answers

According to this, this is a testing question for expressing an expression:

(x ^ 2 + y ^ 2 + z ^ 2- (a ^ 2 + b ^ 2)) ^ 2 - 4 * a * b * (b ^ 2-z ^ 2)

where is the point {x, y, z}, and the small radius of the torus is b, and the main radius is a.

+3
source

The formula above did not work for me. Consider b = 0, the formula should be reduced to a circle, when in fact in the accepted answer it comes down to a sphere.

From my calculation you should check the sign of expression

(x ^ 2 + y ^ 2 + r ^ 2 + a ^ 2-b ^ 2) ^ 2-4a ^ 2 (x ^ 2 + y ^ 2)

where is the point {x, y, z}, and the small radius of the torus is b, and the main radius is a.

edit: equivalent, and closer to the original accepted answer the expression will be

(x ^ 2 + y ^ 2 + r ^ 2- (a ^ 2 + B ^ 2)) ^ 2-4a ^ 2 (b ^ 2-g ^ 2)

+1
source

Source: https://habr.com/ru/post/1447053/


All Articles