1

I want to draw a circle with a specified angle of inclination in 3D space using Python. Similar to the image below: Image

I can already draw circles in 2D. I modified my program by referring to the link below: Masking a 3D numpy array with a tilted disc

import numpy as np
import matplotlib.pyplot as plt

r = 5.0
a, b, c = (0.0, 0.0, 0.0)
angle = np.pi / 6 # "tilt" of the circle

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_xlim(-10,10)
ax.set_ylim(-10,10)
ax.set_zlim(-10,10)

phirange = np.linspace(0, 2 * np.pi, 300) #to make a full circle

x = a + r * np.cos(phirange)
y = b + r * np.sin(phirange)
z=  c


ax.plot(x, y, z )
plt.show()

Now I can draw the circle in 3D space, but I can't get the circle to tilt at the angle I want.

I tried to modify the code in the Z part, the circle can be tilted, but not the result I want.

z = c + r * np.cos(phirange) * np.sin(angle)

Result image:

enter image description here

Do the X and Y parts also need to be modified? What should I do?

update: the circle tilt with other axis des

Gojigu
  • 13
  • 4

1 Answers1

0

Let i = (1, 0, 0), j = (0, 1, 0). Those are the direction vectors of the x-axis and y-axis, respectively. Those two vectors form an orthonormal basis of the horizontal plane. Here "orthonormal" means the two vectors are orthogonal and both have length 1.

A circle on the horizontal plane with centre C and radius r consists in all points that can be written as C + r * (cos(theta) * i + sin(theta) * j), for all values of theta in range [0, 2 pi]. Note that this works with i and j, but it would have worked equally with any other orthonormal basis of the horizontal plane.

A circle in any other plane can be described exactly the same way, by replacing i and j with two vectors that form an orthonormal basis of that plane.

According to your image, the "tilted plane at angle tilt" has the following orthonormal basis:

a = (cos(tilt), 0, sin(tilt))
b = (0, 1, 0)

You can check that these are two vectors in your plane, that they are orthogonal and that they both have norm 1. Thus they are indeed an orthonormal basis of your plane.

Therefore a circle in your plane, with centre C and radius r, can be described as all the points C + r * (cos(theta) * a + sin(theta) * b), where theta is in range [0, 2 pi].

In terms of x,y,z, this translates into the following system of three parametric equations:

x = x_C + r * cos(theta) * x_a + r * sin(theta) * x_b
y = y_C + r * cos(theta) * y_a + r * sin(theta) * y_b
z = z_C + r * cos(theta) * z_a + r * sin(theta) * z_b

This simplifies a lot, because x_b, y_a, z_b are all equal to 0:

x = x_C + r * cos(theta) * x_a    # + sin(theta) * x_b,  but x_b == 0
y = y_C + r * sin(theta) * y_b    # + cos(theta) * y_a,  but y_a == 0
z = z_C + r * cos(theta) * z_a    # + sin(theta) * z_b,  but z_b == 0

Replacing x_a, y_b and z_a by their values:

x = x_C + r * cos(tilt) * cos(theta)
y = y_C + r * sin(theta)
z = z_C + r * sin(tilt) * cos(theta)

In python:

import numpy as np
import matplotlib.pyplot as plt

# parameters of circle
r = 5.0                         # radius
x_C, y_C, z_C = (0.0, 0.0, 0.0) # centre
tilt = np.pi / 6                # tilt of plane around y-axis

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_xlim(-10,10)
ax.set_ylim(-10,10)
ax.set_zlim(-10,10)

theta = np.linspace(0, 2 * np.pi, 300) #to make a full circle

x = x_C + r * np.cos(tilt) * np.cos(theta)
y = y_C + r * np.sin(theta)
z = z_C + r * np.sin(tilt) * np.cos(theta)

ax.plot(x, y, z )
plt.show()

3d plot of tilted circle

Stef
  • 13,242
  • 2
  • 17
  • 28
  • Thanks for the detailed instructions, this looks pretty close to what I want. If I want to change the direction of the tilt of the plane, I need to adjust the vectors of a and b, and adjust the equations of x, y, z in the program, right? Is it possible to make the circle tilt with other axis than the center? Like the picture at the end of the question, the circle is drawn on the Tilted plane. – Gojigu Nov 16 '22 at 04:30
  • @Gojigu Yes to all questions. You can make the circle in any plane, all you need is an orthonormal basis for that plane. Finding an orthonormal basis for the "tilted plane" that you asked was particularly easy. For another plane it might be a bit harder. But not too hard. – Stef Nov 16 '22 at 09:42