MATLAB Answers

how do i generate a torus?

29 views (last 30 days)
NurFadhilah Samsuddin
NurFadhilah Samsuddin on 11 Jan 2021
Edited: John D'Errico on 11 Jan 2021
hi, can someone please help me on how to generate a torus. i did my code as follows and obtained the figure below but i have no idea how to generate a torus. thank you.
clc
clear all
close all
%control point for first surface
P0 = [6 4 0]; P1 = [8 2 0]; P2 = [8 2 1]; P3 = [6 4 1];
P4 = [6 6 0]; P5 = [8 8 0]; P6 = [8 8 1]; P7 = [6 6 1];
P8 = [4 6 0]; P9 = [2 8 0]; P10 = [2 8 1]; P11 = [4 6 1];
P12 = [4 4 0]; P13 = [2 2 0]; P14 = [2 2 1]; P15 = [4 4 1];
phi=1;%scaling factor
B1=-1;%shape parameter lambda
B2=-1;%shape parameter miu
G17=phi*((2+B1)/(2+B2))*(P3-P2)+P3
G21=phi*((2+B1)/(2+B2))*(P7-P6)+P7
G25=phi*((2+B1)/(2+B2))*(P11-P10)+P11
G29=phi*((2+B1)/(2+B2))*(P15-P14)+P15
%control point for second surface
P3; G17; P18 = [4 6 0]; P19 = [6 4 0];
P7; G21; P22 = [4 4 0]; P23 = [6 6 0];
P11; G25; P26 = [6 4 0]; P27 = [4 6 0];
P15; G29; P30 = [6 6 0]; P31 = [4 4 0];
%control point for third surface
%P44 = [6 -2 0]; P45 = [4 0 0]; P46 = [4 0 1]; P47 = [6 -2 1];
%P32 = [8 -4 0];P33 = [2 2 0]; P34 = [2 2 1]; P35 = [8 -4 1];
%P36 = [6 0 0];P37 = [4 -2 0]; P38 = [4 -2 1]; P39 = [6 0 1];
%P40 = [8 2 0];P41 = [2 -4 0]; P42 = [2 -4 1]; P43 = [8 2 1];
%G56=phi*((2+B1)/(2+B2))*(P47-P46)+P47
%G57=phi*((2+B1)/(2+B2))*(P35-P34)+P35
%G58=phi*((2+B1)/(2+B2))*(P39-P38)+P39
%G59=phi*((2+B1)/(2+B2))*(P43-P42)+P43
%control point for fourth surface
%P47;G56;P48 = [8 -4 0]; P49 = [6 -2 0];
%P35;G57;P50 = [14 -10 0]; P51 = [8 -4 0];
%P39;G58;P52 = [8 2 0]; P53 = [6 0 0];
%P43;G59;P54 = [14 8 0]; P55 = [8 2 0];
Gx=[P0(1) P1(1) P2(1) P3(1);P4(1) P5(1) P6(1) P7(1);P8(1) P9(1) P10(1) P11(1);P12(1) P13(1) P14(1) P15(1)];
Gy=[P0(2) P1(2) P2(2) P3(2);P4(2) P5(2) P6(2) P7(2);P8(2) P9(2) P10(2) P11(2);P12(2) P13(2) P14(2) P15(2)];
Gz=[P0(3) P1(3) P2(3) P3(3);P4(3) P5(3) P6(3) P7(3);P8(3) P9(3) P10(3) P11(3);P12(3) P13(3) P14(3) P15(3)];
Gx1=[P3(1) G17(1) P18(1) P19(1);P7(1) G21(1) P22(1) P23(1);P11(1) G25(1) P26(1) P27(1);P15(1) G29(1) P30(1) P31(1)];
Gy1=[P3(2) G17(2) P18(2) P19(2);P7(2) G21(2) P22(2) P23(2);P11(2) G25(2) P26(2) P27(2);P15(2) G29(2) P30(2) P31(2)];
Gz1=[P3(3) G17(3) P18(3) P19(3);P7(3) G21(3) P22(3) P23(3);P11(3) G25(3) P26(3) P27(3);P15(3) G29(3) P30(3) P31(3)];
%Gx2=[P44(1) P45(1) P46(1) P47(1);P32(1) P33(1) P34(1) P35(1);P36(1) P37(1) P38(1) P39(1);P40(1) P41(1) P42(1) P43(1)];
%Gy2=[P44(2) P45(2) P46(2) P47(2);P32(2) P33(2) P34(2) P35(2);P36(2) P37(2) P38(2) P39(2);P40(2) P41(2) P42(2) P43(2)];
%Gz2=[P44(3) P45(3) P46(3) P47(3);P32(3) P33(3) P34(3) P35(3);P36(3) P37(3) P38(3) P39(3);P40(3) P41(3) P42(3) P43(3)];
%Gx3=[P47(1) G56(1) P48(1) P49(1);P35(1) G57(1) P50(1) P51(1);P39(1) G58(1) P52(1) P53(1);P43(1) G59(1) P54(1) P55(1)];
%Gy3=[P47(2) G56(2) P48(2) P49(2);P35(2) G57(2) P50(2) P51(2);P39(2) G58(2) P52(2) P53(2);P43(2) G59(2) P54(2) P55(2)];
%Gz3=[P47(3) G56(3) P48(3) P49(3);P35(3) G57(3) P50(3) P51(3);P39(3) G58(3) P52(3) P53(3);P43(3) G59(3) P54(3) P55(3)];
N=20;
t=(1:N)'/N;
p=(pi/2)*t;
T=[p.^0 sin(p) (sin(p).^2) (sin(p).^3) cos(p) (cos(p).^2) (cos(p).^3)];
A=[1 0 0 1;
-(B1+2) (2+B1) 0 0;
((2*B1)+1) -(2+2*B1) 0 0;
-B1 B1 0 0;
0 0 2+B2 -(B2+2);
0 0 -((2*B2)+2) ((2*B2)+1);
0 0 B2 -B2];
TT=transpose(T);
AA=transpose(A);
z1 = T*A*Gx*AA*TT;
z2 = T*A*Gy*AA*TT;
z3 = T*A*Gz*AA*TT;
z4 = T*A*Gx1*AA*TT;
z5 = T*A*Gy1*AA*TT;
z6 = T*A*Gz1*AA*TT;
%z7 = T*A*Gx2*AA*TT;
%z8 = T*A*Gy2*AA*TT;
%z9 = T*A*Gz2*AA*TT;
%z10 = T*A*Gx3*AA*TT;
%z11 = T*A*Gy3*AA*TT;
%z12 = T*A*Gz3*AA*TT;
X = [z1 z4];
Y = [z2 z5];
Z = [z3 z6];
X1 =[z4 z1];
Y1 =[z5 z2];
Z1 =[z6 z3];
%X2 = [z7 z10];
%Y2 = [z8 z11];
%Z2 = [z9 z12];
%X3 =[z10 z7];
%Y3 =[z11 z8];
%Z3 =[z12 z9];
scatter3([P0(1),P1(1),P2(1),P3(1),P4(1),P5(1),P6(1),P7(1),P8(1),P9(1),P10(1),P11(1),P12(1),P13(1),P14(1),P15(1)],[P0(2),P1(2),P2(2),P3(2),P4(2),P5(2),P6(2),P7(2),P8(2),P9(2),P10(2),P11(2),P12(2),P13(2),P14(2),P15(2)],[P0(3),P1(3),P2(3),P3(3),P4(3),P5(3),P6(3),P7(3),P8(3),P9(3),P10(3),P11(3),P12(3),P13(3),P14(3),P15(3)],'filled','MarkerFaceColor',[1 0 0]);hold on
scatter3([P3(1),G17(1),P18(1),P19(1),P7(1),G21(1),P22(1),P23(1),P11(1),G25(1),P26(1),P27(1),P15(1),G29(1),P30(1),P31(1)],[P3(2),G17(2),P18(2),P19(2),P7(2),G21(2),P22(2),P23(2),P11(2),G25(2),P26(2),P27(2),P15(2),G29(2),P30(2),P31(2)],[P3(3),G17(3),P18(3),P19(3),P7(3),G21(3),P22(3),P23(3),P11(3),G25(3),P26(3),P27(3),P15(3),G29(3),P30(3),P31(3)],'filled','MarkerFaceColor',[1 0 0]);hold on
surf(X,Y,Z)
hold on;
surf(X1,Y1,Z1)
hold on;
%surf(X2,Y2,Z2);
%hold on;
%surf(X3,Y3,Z3);
%hold on;

  0 Comments

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 11 Jan 2021
Edited: John D'Errico on 11 Jan 2021
WAY too much effort here to generate a torus. Start here:
[theta,phi] = meshgrid(linspace(0,2*pi,50));
r = 2;
R = 5;
x = (R + r*cos(theta)).*cos(phi);
y = (R + r*cos(theta)).*sin(phi);
z = r*sin(theta);
surf(x,y,z)
shading interp
colormap hot
axis equal
Essentially, define the torus in parametric form, remapping a rctangular grid into the surface of a torus.

  0 Comments

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!