徐伟
matlab时钟
2024-3-11 15:22
阅读:548

用 matlab制作的时钟

clear;clc;close all

ff;

while 1

    t=clock;tm=t(6);th=t(5);tx=mod(t(4),12);

    if floor(tm)==0

        hold off

    end

    ff

    % 交叉形的图

    %fill([0.4*sind(tm*6),0.4*sind((tm+1)*6),0.6*sind(tm*6),0.6*sind((tm+1)*6)],[0.4*cosd(tm*6),0.4*cosd((tm+1)*6),0.6*cosd(tm*6),0.6*cosd((tm+1)*6)],'r');

    % 秒

    customColor = [0.8, 0.88, 0.97];     % 自定义颜色

    fhandm(0.,0.5,tm,6,customColor)

    hold on

    %  分的画法

    ry=[1 0.84 0];

    fhandm(0.5,0.7,th,6,ry)

    %  小时的画法

    rh=[0.8500 0.3250 0.0980];

    fhandm(0.7,0.85,tx,30,rh)

    pause(1)

end

hold off

function fhandm(r1,r2,x,n,s)

% 用于画出分针与时针的扫过的图, n=30 为时针, n=6 为分针

% x 为时间, r1,r2 为画出图形的半径, s 颜色选项

tx1=[0:0.25:x]*n;

    X=r1*sind(tx1);

    Y=r1*cosd(tx1);

    X1=r2*sind(tx1(end:-1:1));

    Y1=r2*cosd(tx1(end:-1:1));

    fill([X X1],[Y Y1],s)

end

function ff

% 绘制基本图形

x=-1:0.01:1;

xyk=[0:12]*30;

yk=cosd(xyk);xk=sind(xyk);

yk1=0.9*cosd(xyk);

plot([xk;sind(xyk)*0.9],[yk;yk1],'LineWidth',3)  % 设置小时点

hold on

tm=[0:60]*6;

tmk=0.95*cosd(tm);tmx=sind(tm);tmy=cosd(tm);

plot([tmx;sind(tm)*0.95],[tmy;tmk],'LineWidth',2)  % 设置分点

plot(x,sqrt(1-x.^2),'k',x,-sqrt(1-x.^2),'k','LineWidth',2)% 画时钟

get(gcf)

axis equal

axis([-1.1,1.1,-1.1,1.1])

hold on 

h=scatter(0,0,"k.");

text(-0.05,1.1,'12','FontSize',14)

text(-0.02,-1.1,'6','FontSize',14)

text(1.05,0,'3','FontSize',14)

text(-1.1,0,'9','FontSize',14)

axis off

end

转载本文请联系原作者获取授权,同时请注明本文来自徐伟科学网博客。

链接地址:https://wap.sciencenet.cn/blog-216525-1424903.html?mobile=1

收藏

分享到:

当前推荐数:0
推荐到博客首页
网友评论0 条评论
确定删除指定的回复吗?
确定删除本博文吗?