Rotate rectangle x number of degrees when using imrect or alternative function

조회 수: 3 (최근 30일)
Hi everybody,
I would like to allow a user to draw a rectangle over a plot to highlight some data. This seems entirely possible using imrect, however I sometimes need the rectangle to be rotated 60 degrees to the x axis. I can't find any mention of rotation in the help or by using get(h).
I've looked for some help on this function but strangely it doesn't seem to be widely used. Does anybody know how to rotate a rectangle when using imrect, or are there alternatives which do allow it?
Thanks,
Rod.

채택된 답변

Image Analyst
Image Analyst 2014년 4월 5일
You can get the vertices of the corners. Then use the rotation matrix (see Wikipedia) to rotate it about its center by any angle. Then redraw the rectangle with plot().
  댓글 수: 2
Right Grievous
Right Grievous 2014년 4월 5일
I think I'm going to use impoly instead and manually calculate the coordinates for the rotated rectangle. I only have to calculate this once and then I can have the same polygon created on the figure each time - the user can move it into position.
I'm still at a loss as to why imrect does not have an input for orientation. Did Mathworks not foresee someone wanting a rectangle that was maybe not parallel to the axes?
My next challenge is rotating the contents of the polygon, so I will probably use your suggestion for that purpose. It seems awfully complicated, is there a Matlab function for a rotation matrix or does it have to be coded manually?
Thanks,
Rod.
Image Analyst
Image Analyst 2014년 4월 5일
편집: Image Analyst 2014년 4월 5일
Rod, it's trivial. Just a 2 by 2 matrix [cos, -sin, sin, cos] multiplied by the "old" coordinates. See the attached demo to rotate an ellipse, and a star.
rotationArray = [cos(rotationAngle), -sin(rotationAngle); sin(rotationAngle), cos(rotationAngle)];
% Rotate the diagram
rotatedArray = vertices * rotationArray;

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 3-D Scene Control에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by