MATLAB Answers

Add a user define property to freehand object

3 views (last 30 days)
Nikan Fakhari
Nikan Fakhari on 2 Aug 2021
Edited: Adam Danz on 3 Aug 2021
Hi there,
I know that Freehand object does not have a rotatable property like drawrectangle.
Can anyone tell me please how can I add this property to Freehand, so I can rotate my object on the figure?
Nikan

Answers (1)

Adam Danz
Adam Danz on 3 Aug 2021
Edited: Adam Danz on 3 Aug 2021
You could apply your own rotation using a rotation matrix.
% anonymous function to center the obj at (0,0),
% rotate it, and move it back to its original
% center point.
ROIxy = @(h)[h.Position(:,1),h.Position(:,2)];
rotMat = @(h,th)([cosd(th), -sind(th); sind(th), cosd(th)]*(ROIxy(h)-mean(ROIxy(h)))')' + mean(ROIxy(h));
To use the functions above, create a freehand object and they apply the rotation angle in degrees.
Example:
% create freehand obj
z = drawfreehand();
% Plot the center point (red x)
hold on
xl = xlim(); yl = ylim();
cnt = mean(ROIxy(z)); % center point
plot(cnt(1),cnt(2),'rx')
xlim(xl); ylim(yl) % see [1]
% Continually rotate ROI by 12 degrees CCW
while true
z.Position = rotMat(z,12); % see [2]
drawnow
end
Footnotes
[1] I don't know why the axis limits change when adding the + marker but this resets the original axis limits.
[2] rotMat(h,th) rotates ROI object h by th degrees (negative is CW rotation)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by