Moving a push button in a circular orbit
Mostra commenti meno recenti
Hi,
I am trying to make a solar system gui, where in my planet (Earth) needs to rotate in orbit. I have used a background image for the orbit and I have the image of Earth loaded into a push- button. I need to move the push button in a circular orbit around my sun. I currently have code that looks like this:
function trial3_OpeningFcn(hObject, eventdata, handles, varargin)
%load the background image into Matlab
backgroundImage = importdata('planet_orbit.jpg');
%select the axes
axes(handles.axes1);
%place image onto the axes
image(backgroundImage);
%remove the axis tick marks
axis off
% Get Position of Earth
pos = get(handles.pushbutton1,'Position');
handles.x0 = pos(1); % initial x position
handles.y0 = pos(2);% initial y position
handles.width = pos(3); % width
handles.height = pos(4); % height
% Load image of Earth
a = imread('earth 2.jpg');
% Overlay image onto pushbutton1
set(handles.pushbutton1,'CData',a)
function earth_Callback(hObject, eventdata, handles)
dx = 0:0.05:10; % x trajectory
dy = asin(sqrt(100-(cos(dx)).^2)); % y trajectory;
for i = 1:length(dx)
% new_position = [x y width height];
new_position = [5*dx(i)+handles.x0 5*dy(i)+handles.y0 handles.width handles.height];
% move the button to the new position
set(handles.pushbutton1,'Position',new_position);
% slow down the movement
pause(0.0001);
end
My code doesn't give a path that I want. Can somebody help me asap? Thanks!
2 Commenti
Walter Roberson
il 28 Nov 2011
is there a particular reason you are putting the image on a pushbutton instead of just creating an image object and changing its XData and YData properties to move it? http://www.mathworks.com/help/techdoc/ref/image_props.html
Indira Iyer
il 28 Nov 2011
Risposte (2)
Jan
il 28 Nov 2011
This is an unusual function to define a cicular orbit:
dx = 0:0.05:10; % x trajectory
dy = asin(sqrt(100-(cos(dx)).^2)); % y trajectory;
dy has Pi/2 as real part and a sin wave between -2.9932 and -2.9882 as imaginary part. I can imagine, that this differs from the path you want. But as long, as I only see this formula and you do not explain which path you are wanting, I do not see a chance for any advice.
In other words: Please explain, which path you are wanting.
2 Commenti
Indira Iyer
il 28 Nov 2011
Walter Roberson
il 28 Nov 2011
numcirclepoints = 500; %adjust to change smoothness
orbitradius = 15; %adjust to fit background
theta = linspace(0,2*pi,numcirclepoints);
%but skip theta = 0 because the image is already there
theta = theta(2:end);
%now,
dx = orbitradius .* sin(theta);
dy = orbitradius .* cos(theta);
Walter Roberson
il 28 Nov 2011
a = imread('earth 2.jpg');
%set handles.x0, handles.y0, handles.width, handles.height to specific constants at this point in the code. The pushbutton will not exist so there will not be any initial coordinates to fetch
%then,
imagex = [handles.x0, handles.x0 + handles.width];
imagey = [handles.y0, handles.y0 + handles.height];
handles.earth = image(imagex, imagey, a);
%set dx and dy as appropriate at this point. But first you have to define whether dx and dy are relative to the original coordinates, or relative to the immediately previous coordinates
%while true %if orbit is continuous
for K = 1 : length(dx)
%assuming dx and dy are relative to original coordinates
set(handles.earth, 'XData', imagex + dx(K), 'YData', imagey + dy(K));
pause(0.0001);
end
%end %loop back for next full orbit
Categorie
Scopri di più su Earth and Planetary Science in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!