Draw Circle With Sin and Cos Matlab
If you want the vertices of the (jagged or not) circle to match the points of the surface, you do not need to recalculate whatever points. You can simply re-use some of the points of your surface, choosing the path which define the shape you lot want.
I'll employ your lawmaking to define the initial tore, with just a slight modification: I'll use the upper-case letter for variable which define a matrix (and lower example for variables which are simple 1D vector). So your initial tore becomes:
%% // define initial tore am = 1.; rm = iii.; t = linspace(-pi,pi,16); p = linspace(0,ii.*pi,16); [T,P] = meshgrid(p,t); 10 = (rm + am.*cos(P)).*cos(T); Y = (rm + am.*cos(P)).*sin(T); Z = am.*sin(P); hsurf = surf(X,Y,Z,'FaceColor','interp','FaceAlpha',0.v,'EdgeAlpha',0.25); axis equal ; agree on Not a big bargain but this allow to accept merely 1D vector coordinates in the case where you define your circle by equation as you were doing. It keeps your variables and workspace cleaner and reduce the risk of mistakes and dimension errors.
For the simple circle, you could redefine the coordinate by equation as you did (developed here for clarity)
%% // your uncomplicated circumvolve - calculated colID = 8 ; xs = (rm+am.*cos(p)).*cos(t(colID)) ; ys = (rm+am.*cos(p)).*sin(t(colID)) ; zs = am.*sin(p) ; hp = plot3(xs,ys,zs, 'b', 'LineWidth',2); %// This was generating 2D arrays (matrices) for xs, ys and zs when yous %// were using the meshed version of T and P. Using only the vector version of %// 't' and 'p' generate simply vector coordinates. => cleaner and safer. But I'd like to bring your attention to another style, just utilise the points of the surface already defined. This (i) eliminates the need for new calculations, and (2) insure that the vertices of the circumvolve will be matching exactly some vertices of the tore.
%% // another uncomplicated circle - extracted from the tore vertices colID = 8 ; xs = X(:,colID) ; ys = Y(:,colID) ; zs = Z(:,colID) ; hpc = plot3(xs,ys,zs, 'b', 'LineWidth',2); Again, at that place are extra lines for clarity but you tin can meaty the code in one case you understand what is going on. These 2 different means of producing a circumvolve result in the post-obit figures:
For illustration purpose, I highlighted the vertices of the tore (small black dots), so you can encounter if the circles vertices match or not.
Now for the jagged circle, the final method presented will take fifty-fifty more than interest. Every bit you observed, to become a circle coordinate nosotros simply extracted i column of each of the tore matrix coordinates. We used a fixed value for the colID to retrieve. To "jagg" your circumvolve, nosotros only need to introduce a small perturbation in the cavalcade number, so nosotros will occasionally call up the adjacent vertex coordinates.
The following lawmaking generate a column ID varying around the staring cavalcade ID. You can ascertain the maximum total spread as well as the maximum single increment:
%% // generate alphabetize of columns to exist retrieved idxcol = zeros(size(10,1),1) ; maxDeviation = v ; %// full maximum departure to the initial center line maxPerturbation = 2 ; %// max deviation for ane increment difference = cumsum(randi([-maxPerturbation maxPerturbation],size(X,one),1)) ; %// leap deviations to maximum values departure(deviation>maxDeviation) = maxDeviation ; deviation(deviation<-maxDeviation) = -maxDeviation ; startColID = 8 ; colID = startColID + divergence ; %// close the profile past repeating commencement bespeak in the end if information technology'southward not closed already if colID(end) ~= colID(i) ; colID(cease) = colID(1) ; stop If we now use coordinate from theses columns, we get:
npt = size(colID,1) ; xcj = zeros(npt) ; ycj = xcj ; zcj = xcj ; for yard=i:npt xcj(thousand) = X( k , colID(thousand) ) ; ycj(1000) = Y( thou , colID(thou) ) ; zcj(grand) = Z( m , colID(k) ) ; end hpc = plot3(xcj,ycj,zcj, 'b', 'LineWidth',ii);
It is a closed profile turning around the tore only the lines do non match the lines of the surface every bit in your drawn case. This is because the transition from one column ID to another happen at each change of 10 alphabetize.
To rectify that, nosotros can use the stairs function:
%% // create a stepped contour [xd,yd] = stairs(colID) ; %% // brandish rectified jagged circle npt = size(yd,1) ; xcj = zeros(npt) ; ycj = xcj ; zcj = xcj ; for grand=ane:npt xcj(thousand) = X( xd(1000) , yd(k) ) ; ycj(thou) = Y( xd(k) , yd(k) ) ; zcj(k) = Z( xd(thou) , yd(k) ) ; end hpc = plot3(xcj,ycj,zcj, 'b', 'LineWidth',two); This will at present generate the post-obit profile:
Much better ... However, when the cavalcade ID moves past more than 1 alphabetize for the aforementioned x, we all the same miss anchor points to go along the jagged circle profile matching the tore profile. If your bones column deviation increment is more than one, you will even have many more than of these artefact.
To completely rectify that, nosotros need to add the missing anchor points. I couldn't detect a built-in part to do that and then I went the good old loop mode. Feel free to optimize if y'all notice some style.
%% // recreate index vectors with all the necessary anchor points colX(1,i) = xd(1) ; colY(1,1) = yd(1) ; k = 2 ; for t=2:size(yd,1) inc = sign(yd(t)-yd(t-1)) ; %// define increment of 1 with correct sign ids = yd(t-1):inc:yd(t)-inc ; %// range of alphabetize to be covered if isempty(ids) ; ids = yd(t) ; stop %// catch corner cases %// now add these points to the listing n = length(ids) ; colX(k:yard+n-1,one) = xd(t-1) ; colY(thou:grand+n-one,1) = ids ; yard=k+n; end %// shut contour (add last point in the cases where information technology is necessary) if inc ~= 0 colX(end+1,i) = xd(stop) ; colY(end+i,1) = yd(end) ; end %% // display fully rectified jagged circumvolve npt = size(colX,one) ; xcj = zeros(npt) ; ycj = xcj ; zcj = xcj ; for yard=1:npt xcj(1000) = X( colX(yard) , colY(k) ) ; ycj(yard) = Y( colX(chiliad) , colY(k) ) ; zcj(g) = Z( colX(thou) , colY(k) ) ; finish And now we don't have gaps between two column ID any more than, all the points of the jagged circle match a signal of the surface profile:
Source: https://stackoverflow.com/questions/36157881/drawing-circles-on-a-torus-in-matlab
0 Response to "Draw Circle With Sin and Cos Matlab"
Post a Comment