title: Python checking if a point is in sphere with center x, y ,z
body: <p>I'm trying to check if a point is within a sphere with a center point of (x, y, z) where (x, y, z) is not (0, 0, 0). </p>
  
  <p>This code I'm using to generate the points I want to check:</p>
  
  <pre><code>def generatecoords(self, i):
      x, y, z = generatepoint()
  
      if i &gt;= 1:
          valid = False
  
          while valid == False:
              coords = self.checkpoint(x, y, z)
  
              for b in world.starlist:
                  if coords == world.starlist[b].coords:
                      coords = self.checkpoint(x, y, z)
  
                  else:
                      valid = True
  
      else:
          coords = self.checkpoint(x, y, z)
  
      return coords
  
  def checkpoint(self, x, y, z):
      d = math.sqrt(x * x + y * y + z * z)
  
      while d &gt;= self.radius:
          x, y, z = generatepoint()
          d = math.sqrt(x * x + y * y + z * z)
  
      coords = (int(x), int(y), int(z))
  
      return coords
  
  def generatepoint():
      x, y, z = [int(random.uniform(-self.radius, self.radius)) \
                 for b in range(3)]
  
      return x, y, z
  </code></pre>
  
  <p>These function are called in a for loop to generate the points in a dictionary, while also checking the unlikely chance that points aren't placed on top of another(mostly because I can).</p>
  
  <p>I trying to figure out what I need to add to <code>math.sqrt(x * x + y * y + z * z)</code> so that it accounts for a center that isn't <code>(0, 0, 0)</code>. I do know of one way to do it, but it would require several lines of code and I'd rather do it in one. I would have asked this in the comments of the answer in another question, but I'm not allowed to comment on answers yet.</p>