How to draw circle in a 3D space?
26 views (last 30 days)
Show older comments
Salad Box
on 23 Sep 2019
Commented: Fabio Freschi
on 25 Sep 2019
Hi,
I would like to draw 2 circles. One in plane [P1 P6 P5], the other in plane [P1 P8 P5].
RGB values of points P1, P5, P6 and P8 are known. E.g., the RGB values of P1 is [R1 G1 B1]; the RGB values of P5 is [R5 G5 B5].
Requirements:
- Using line P1P5 as the diameter
- Using the mid point of line P1P5 as the center
- P6 and P8 are points only used for deciding the location of the planes, P6 and P8 does not necessarily need to be contained in the circles. They could be outside of the circles.
Any thoughts please?
0 Comments
Accepted Answer
Fabio Freschi
on 23 Sep 2019
Edited: Fabio Freschi
on 23 Sep 2019
The solution requires the definition of a rotation matrix. There are several ways to use it. Matlab has the rotx, roty and rotz functions, but they only work with one rotation at time. My implementation (see attachment) works by defining the new coordinate system identified by the position of the new center P0, any point along the new z axis Pz, and any point along the new x axis Px. So the call is R = rotationMatrix(P0,Pz,Px); I understand that these few words can be complex to understand but if you need details, I can explain further. Now the code
clear variables, close all
% points
P1 = [0 0 0]; P8 = [0 0 1]; P5 = [1 .5 2]; P6 = [2 2 3];
% plot
h = figure; hold on
plot3(P1(1),P1(2),P1(3),'o')
plot3(P8(1),P8(2),P8(3),'o')
plot3(P5(1),P5(2),P5(3),'o')
plot3(P6(1),P6(2),P6(3),'o')
axis equal, view([1 1 1])
% center
C0 = (P1+P5)/2; % center
figure(h), plot3(C0(1),C0(2),C0(3),'*')
% radius
R = sqrt(sum((P5-P1).^2,2))/2;
% circumpherence in local coordinates
nPt = 100;
alpha = linspace(0,2*pi,nPt).';
P = [R.*cos(alpha) R.*sin(alpha) zeros(nPt,1)];
% direction orthogonal to circumpherence 1
v1 = cross(P8-P1,P5-P1);
figure(h), quiver3(C0(1),C0(2),C0(3),v1(1),v1(2),v1(3))
% rotation matrix
R1 = rotationMatrix(C0,C0+v1,P5);
% circumpherence 1 rotate points
Pc1 = P*R1'+C0;
plot3(Pc1(:,1),Pc1(:,2),Pc1(:,3),'-');
% direction orthogonal to circumpherence 2
v2 = cross(P6-P1,P5-P1);
figure(h), quiver3(C0(1),C0(2),C0(3),v2(1),v2(2),v2(3))
% rotation matrix
R2 = rotationMatrix(C0,C0+v2,P5);
% circumpherence 2 rotate points
Pc2 = P*R2'+C0;
plot3(Pc2(:,1),Pc2(:,2),Pc2(:,3),'-');
Please check if I have provided everything you need, because I have many personal utility functions in matalb path
10 Comments
Fabio Freschi
on 25 Sep 2019
P are the points along the green circle R1 is the rotation matrix, C0 the center of the roto-translated circle. So: the green circumpherence is rotated (P*R1') and then translated to the new center (P*R1'+C0)
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!