Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

given origin and radii, how to find out if p(x,y,z) is inside torus?

Tags:

geometry

lua

3d

Im trying to create a 3D torus in a block game, so i need to evaluate a range of coordinates to see if they are inside a torus. The way i did it with spheres is:

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

given height (on y axis), a minor and major radius (on xz axes), and a point of origin, none of the evaluation expressions i tried have given me anything close to a torus.

like image 767
user1836708 Avatar asked Oct 05 '22 22:10

user1836708


2 Answers

According to this it's a matter of testing for the sign of the expression:

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

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

like image 72
Paul Kulchenko Avatar answered Oct 10 '22 01:10

Paul Kulchenko


The formula above didn't work for me. Consider b = 0, the formula should reduce to a circle, when in fact in the accepted answer it reduces to a sphere.

From my calculation you must test the sign of the expression

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

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

edit: equivalently, and closer to the original accepted answer the expression would be

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

like image 27
Giles Coope Avatar answered Oct 10 '22 02:10

Giles Coope