user14663792
user14663792

Reputation:

Get points of sphere in 3d list python

How would I get the points of a sphere with the radius r? Im looking for something like bresenham's circle drawing algorithm but in 3d. I tried just using the 2d algorithm and drawing circles in increasing sizes behind each other, but that doesnt quite work.

enter image description here

(The picture is distorted around the edges. Thats normal.)

Do you know of anything that could fix that? I could draw a nother smaler circle inside of the bigger one, but thats cheating. Yes, I went through the existing Questions but couldnt find any that fit my purpose.

Edit:

def drawCircle(self, xyz, r):
        xc, yc, zc = xyz
        coords = []
        
        def drawC(xc, yc, zc, x, y):
            coords.append((xc+x, yc+y, zc))
            coords.append((xc-x, yc+y, zc))
            coords.append((xc+x, yc-y, zc))
            coords.append((xc-x, yc-y, zc))
            coords.append((xc+y, yc+x, zc))
            coords.append((xc-y, yc+x, zc))
            coords.append((xc+y, yc-x, zc))
            coords.append((xc-y, yc-x, zc))
            
        x = 0
        y = r
        d = 3 - 2 * r
        drawC(xc, yc, zc, x, y) 
        while y >= x:
            x += 1
            if (d > 0):
                y -= 1 
                d = d + 4 * (x - y) + 10 
            else:
                d = d + 4 * x + 6
            drawC(xc, yc, zc, x, y) 
        
        for c in coords:
            self.drawPixel(c)
        return coords

This function takes a x,y,z point and a radius, calculates all points on the circle and draws them into a 3d array, witch can be drawn to the screen by another function. Here is an example with r = 10. (The circle is pink. Im just testing color.) Im looking or a version without np if possible.

pic

Upvotes: 1

Views: 403

Answers (1)

Prefect
Prefect

Reputation: 1777

You can create a centered sphere by using the code below.

import numpy as np


r=2
y,x,z = np.ogrid[-r:r+1, -r:r+1, -r:r+1,]
mask = x**2 + y**2 + z**2 <= r**2

print(mask)

Upvotes: 1

Related Questions