How to rotate a line on a center point?
Show older comments
Hi, given the center point is (1,1). How can I rotate a line with a radius of 0.8? And the line follows a right click of a mouse.The graph needs to be in a 2dimension. Thank you.
Answers (1)
Geoff Hayes
on 17 Nov 2015
Nik - if each right mouse-click corresponds to a single rotation (given a fixed rotation angle), then you can use the below code to rotate your line of radius r around the centre point (1,1).
function rotatingLine
close all;
xo = 1;
yo = 1;
r = 0.8;
n = 100;
theta = pi/18; % 10 degree rotation angle
coords = zeros(2,n);
coords(1,:) = linspace(0,0,n);
coords(2,:) = linspace(0,r,n);
hPlot = plot(coords(1,:),coords(2,:));
axis([-2*r 2*r -2*r 2*r]);
axis('equal');
axis('manual');
set(gcf,'WindowButtonDownFcn',@onButtonDown);
function onButtonDown(hObject,~)
selectionType = get(hObject,'SelectionType');
if strcmpi(selectionType,'alt')
% right button click, so rotate line
R = [cos(theta) -sin(theta) ; sin(theta) cos(theta)];
coords = R*coords;
set(hPlot,'XData',coords(1,:),'YData',coords(2,:));
end
end
end
In the above example, we assume that the rotation angle is 10 degrees. An array of x and y coordinates is created so that the line is plotted pointing north from the origin of (1,1). We then use assign a callback to the current figure (gcf) WindowButtonDownFcn event. This callback checks to see if the selection type is alt (for a right-button click) and then we apply the rotation to the coordinates, rotating the line 10 degrees counter-clockwise about the origin. Each subsequent right-button click does the same.
Categories
Find more on Elementary Math in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!