⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ball.m

📁 《精通matlab与c++混合编程》的光盘内容
💻 M
字号:
function varargout = ball(varargin)
% BALL M-file for ball.fig
%      BALL, by itself, creates a new BALL or raises the existing
%      singleton*.
%
%      H = BALL returns the handle to a new BALL or the handle to
%      the existing singleton*.
%
%      BALL('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in BALL.M with the given input arguments.
%
%      BALL('Property','Value',...) creates a new BALL or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before ball_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to ball_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help ball

% Last Modified by GUIDE v2.5 30-Sep-2004 23:17:12

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @ball_OpeningFcn, ...
                   'gui_OutputFcn',  @ball_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before ball is made visible.
function ball_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to ball (see VARARGIN)

% Choose default command line output for ball
handles.output = hObject;
%初始化当前figure对象
set(gcf,'color',[0.5 0.7 0.3]);%
set(gcf,'doublebuffer','on');
%建立并初始化用于动画绘制的坐标轴
axes(handles.axes1);
set(gca,'color',1-[0.5 0.7 0.3]);
set(gca,'position',[[0 0 1 1]]);
axis([0 1 0 1]);
hold on;
%绘制趣味弹球动画的背景
handles.h1 = fill([0 1 1 0],[0 0 1 1],1-[0.5 0.7 0.3]);
handles.h2 = fill([0 1 1 0],[0 0 1 1],1-[0.5 0.7 0.3]);
%初始化两个弹球
handles.direction1 = 30/180*2*pi+rand(1,1)*30/180*2*pi;%方向
handles.direction2 = 30/180*2*pi+rand(1,1)*30/180*2*pi;
axis off 
handles.go = 1;
handles.position1 = [0.5 0.5];%位置
handles.position2 = [0.3 0.3];
handles.dx = 0.01;
handles.dy = 0.01;
handles.xdata1 = 0.03*cos(0:0.1:2*pi)+handles.position1(1);%弹球1的区域坐标
handles.ydata1 = 0.03*sin(0:0.1:2*pi)+handles.position1(2);
handles.xdata2 = 0.03*cos(0:0.1:2*pi)+handles.position2(1);%弹球2的区域坐标
handles.ydata2 = 0.03*sin(0:0.1:2*pi)+handles.position2(2);
while handles.go < 100
	 %计算弹球的运动的下一个位置
     [handles.xdata1,handles.ydata1,handles.position1,handles.direction1]=...
                getnextball(handles.xdata1,handles.ydata1,...
handles.position1,handles.direction1,handles.dx,...
handles.dy);
     [handles.xdata2,handles.ydata2,handles.position2,handles.direction2]=...
                getnextball(handles.xdata2,handles.ydata2,...
handles.position2,handles.direction2,handles.dx,...
handles.dy);
    %更新弹球的位置
set(handles.h1,'XData',handles.xdata1,'Ydata',handles.ydata1);  
    set(handles.h2,'XData',handles.xdata2,'Ydata',handles.ydata2);  
    drawnow;    
    pause(0.03);%程序运行暂停0.03s,用于控制弹球的运动速度
end

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes ball wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = ball_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

function [newxdata,newydata,newposition,newdire]=...
getnextball(xdata,ydata,preposition,predire,dx,dy)
%function [newxdata,newydata,newposition,newdire]=
%							getnextball(xdata,ydata,preposition,predire,dx,dy)
%计算弹球运行的下一个位置
newxdata = xdata + dx*cos(predire); 
newydata = ydata + dy*sin(predire);
newposition = preposition + [dx*cos(predire) dy*sin(predire)];
%弹球碰到右壁面
if newposition(1)+0.03+0.001>=1
    d1 = newposition - preposition;
    d1(1) = -d1(1);
    newdire = atan2(d1(2),d1(1));  
    return;
end

%弹球碰到左壁面
if newposition(1)-0.03-0.001<=0
    d1 = newposition - preposition;
    d1(1) = -d1(1);
    newdire = atan2(d1(2),d1(1));    
    return;
end

%弹球碰到上壁面
if newposition(2)-0.03-0.001<=0
    d1 = newposition - preposition;
    d1(2) = -d1(2);
    newdire = atan2(d1(2),d1(1));    
    return;
end

%弹球碰到下壁面
if newposition(2)+0.03+0.001>=1
    d1 = newposition - preposition;
    d1(2) = -d1(2);
    newdire = atan2(d1(2),d1(1));    
    return;
end
%没有碰到任何壁面
newdire = predire;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -