I'm writing a function in MATLAB that takes in the coordinates x and y of 3 billiard balls, and wants to output which ball has moved first, second and last, and how many balls have moved in total. I came up with a solution, but it involves a significant amount of if
-else
statements, which seems a bit complicated for the task at hand.
I'm not asking for a solution I can copy-paste, but rather, if you could help guide an inexperienced MATLAB user in a certain direction I would be very grateful.
Here is what my code looks like with only one of the three if else blocks used:
function [FirstBall,SecondBall,LastBall, NbBallsMoved] = GetBallMoveOrder(Xr, Yr, Xy, Yy, Xw, Yw, MoveDistPx)
[Idx_r,len_r] = GetFirstMoveIdx(Xr,Yr,MoveDistPx);
[Idx_y,len_y] = GetFirstMoveIdx(Xy,Yy,MoveDistPx);
[Idx_w,len_w] = GetFirstMoveIdx(Xw,Yw,MoveDistPx);
if Idx_r < Idx_y && Idx_r < Idx_w % Cas où le rouge est premier
FirstBall = 1;
if Idx_y < Idx_w
SecondBall = 2;
LastBall = 3;
elseif Idx_y > Idx_w
SecondBall = 3;
Lastball = 2;
elseif len_y > len_w
SecondBall = 2;
LastBall = 3;
elseif len_y < len_w
SecondBall = 3;
LastBall = 2;
else
SecondBall = 2;
LastBall = 3;
end
end
end
Here Xr
and Yr
are arrays of coordinates, and the function GetFirstMoveIdx
returns the index of the first move, along with the length in pixels of the first move. I tried to use the sort
function but didn't get very far, since the sort
function mixes up the values where I don't know which value corresponds to which ball.