# Drawing a rotating circle on a sphere

조회 수: 8(최근 30일)
Julian Blackthorne 2021년 9월 23일
댓글: Julian Blackthorne 2021년 9월 23일
I am trying to rotate a sphere like the eye on MATLAB and would like to simulate the pupil on the sphere that moves with the rotations as well.
[X,Y,Z] = sphere;
s1 = surf(X,Y,Z, 'FaceColor', [1 1 1], 'EdgeColor', 'none');
set(gca,'Color','k')
xdir = [1 0 0];
ydir = [0 1 0];
zdir = [0 0 1];
rotate(s1,zdir,10);
rotate(s1,ydir,10);
rotate(s1,xdir,10);
I have written a code for a sphere that rotates but I cannot figure out how to get the pupil plotted on the sphere centre so that the final figure after rotation looks somewhat like this - 댓글을 달려면 로그인하십시오.

### 채택된 답변

Fabio Freschi 2021년 9월 23일
편집: Fabio Freschi 2021년 9월 23일
The code should be self-explicative
[X,Y,Z] = sphere;
% figure
figure, hold on
axis equal, view([1 1 1]);
% sphere with transparence
s1 = surf(X,Y,Z, 'FaceColor', [1 1 1], 'EdgeColor', 'none','FaceAlpha',0.5);
light
% I don't like black background
% set(gca,'Color','k')
% radius of the eye (to be more general)
R = max(sqrt(X(:).^2+Y(:).^2+Z(:).^2));
r = 0.6;
% distance from the center
h = sqrt(R^2-r^2);
% create disc
n = 30;
a = linspace(0,2*pi,n);
s2 = fill3(h*ones(n,1),r*cos(a(:)),r*sin(a(:)),'b');
% rotation
xdir = [1 0 0];
ydir = [0 1 0];
zdir = [0 0 1];
rotate(s1,zdir,10);
rotate(s1,ydir,10);
rotate(s1,xdir,10);
% rotation of pupil
rotate(s2,zdir,10);
rotate(s2,ydir,10);
rotate(s2,xdir,10); ##### 댓글 수: 1표시숨기기 없음
Julian Blackthorne 2021년 9월 23일
@Fabio Freschi Yes, it is! Thank you so much for your help.

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!