Hey I'm developing a little GUI with App Desinger that is supposed to animate circles clockwise and anticlockwise for different starting points. The animation has to be done with given starting point, given endpoint and given i- and j-parameters. I and j is the distance from the startingpoint to the center point in x- and y-direction so you can easily calculate the radius of the circle.
I already wrote an algorithm for that purpose but as it's a bit complicated (lots of distinction of cases) I was wondering if there is an easyer way. Besides of that I'm facing an issue with my algo which I don't know how to solve.
In the following I'm going to show you the idea of my algo for the anticklockwise animation. The clockwise animation works the same, the only difference is that the angles run from zero to negative values.
My idea was to devide the circle into 4 segments. Given the center point of the circle I distinguish in which segment is my starting point and my endpoint. So for the clockwise animation I have 4 cases for the starting point and additional 4 cases for each of the starting points dealing with the endpoint. E.g. Starting point in 1st Segment: endpoint in 1st, endpoint in 2nd, endpoint in 3rd, endpoint in 4th segment.
Here is a part of my code for the starting point beeing in the 1st segment and the endpoint being in the 1st and 2nd segment. I saved the points in a struct called s in case you are wondering what s is. PlotCircle does the animation with the given points.
i = s(n).parameter(1);
j = s(n).parameter(2);
p1 = s(n-1).koordinaten;
p2 = s(n).koordinaten;
M = [p1(1) + i, p1(2) + j];
r = round(sqrt(i^2 + j^2),2);
if p1(1) > M(1) && p1(2) >= M(2) && p2(1) >= M(1) && p2(2) > M(2)
alpha1 = round (acos(abs(i)/r),2);
alpha2 = round(acos(abs(p2(2) - M(2))/r),2);
alpha = round((pi/2 - alpha2),2);
t = linspace(alpha1,alpha,50);
x = r * cos(t) + M(1);
y = r * sin(t) + M(2);
plotCircle(app,x,y);
end
if p1(1) > M(1) && p1(2) >= M(2) && p2(1) < M(1) && p2(2) >= M(2)
alpha1 = round (acos(abs(i)/r),2);
alpha2 = round(acos(abs(p2(2) - M(2))/r),2) + pi/2;
t = linspace(alpha1,alpha2,50);
x = r * cos(t) + M(1);
y = r * sin(t) + M(2);
plotCircle(app,x,y);
end
function plotCircle (app,x,y)
for m = 1:length(x)
plot2 = plot(app.UIAxes,x(m),y(m),'ro','LineWidth',1.5,'MarkerSize',5);
plot3 = plot(app.UIAxes,x(1:m),y(1:m),'r-','LineWidth',1.5,'MarkerSize',5);
drawnow
if m ~= length(x)
delete(plot2)
delete(plot3)
end
if m == length(x)
delete(plot2)
app.plot_array = [app.plot_array; plot3];
end
end
end
As you can see this is a lot of work for all cases existing. Anyway I took the time to implement all these cases for clockwise and anticlockwise animation. Still I would like to know if there is an easyer way.
The issue I was talking about earlier is kind of an optimization problem I guess. As the User of the GUI has to type in the staring point, endpoint, i and j manually there can be deviation of the radius of the starting point and endpoint. When I calculate alpha2 with acos (abs(....)/r) the radius r relates to the radius of staring point (p1) . This can cause inprecision in the calculation of the angles. For example when the User types in an endpoint that hasn't got approximately the same radius like the one calculated from i and j for the starting point.
I would be happy if I can overcome this problem with the algo I was developing myself because I spent so much time with it so far. Also I would like if my algo is a "good solution" to achieve clockwise and anticlockwise animations of a circle or if you recommend to solve it differently.
Best Tim :)