How to draw any circle in a 3D space in python?

Suppose I have the center of the circle c=[x0, y0, z0], the radius of the circle r, and the normal to the circle n=[a, b, c]. The general equation of a circle in 3D space is:

((x - x0)^2 + (y - y0)^2 + (z - z0)^2 - r^2)^2 + (a(x - x0) + b(y - y0) + c(z - z0))^2 = 0

for example:

r=20
n = [1, 1.5, 1]
c = [2, 3, 4]

How to draw the the circle in python? I want the dots on the circle are equally distributed with a step size of theta.

theta = 1 # in degree

Solution 1:

You can easily do it using some trig. If we get the point on a unit circle, we can just scale it by the radius of the desired circle. This is because the radius of the unit circle is 1, hence being called a unit circle.

With Soh Cah Toa, we know sine will give us the opposite side of a triangle, our Y, and cosine will give us the adjacent side, our X. So to get a point at θ radians, we just need to do

import math
radius = 20
theta = math.pi
point = (math.cos(theta)*radius, math.sin(theta)*radius)
print(point)
"""(-20.0, 0.0) """

Since you want it to use degrees not radians, do a quick conversion of your degrees to radians beforehand by multiplying by π/180. From that point, you can just use a for loop and increase the angle you get the point at by theta amount each time. This will give you evenly spaced points.