Reputation: 25194
This is embarassing, but however. My ellipse is defined by:
centerX
and centerY
;majorAxis
and minorAxis
, lengths of both axis;orientation
, angle between the horizontal X axis and the major axis of the ellipse.I started plotting the ellipse with its major axis parallel to the X axis:
theta = 0 : 0.05 : 2*pi;
orientation=orientation*pi/180;
xx = (majorAxis/2) * sin(theta) + centerX;
yy = (minorAxis/2) * cos(theta) + centerY;
%plot(xx,yy)
Now I only have to rotate this stuff by orientation
degrees or radiants. Any help? I tried a lot of stuff, e.g.:
rotate
ing the object;R = [cos(orientation), -sin(orientation); sin(orientation), cos(orientation)];
and applying it to xx-centerX
and yy-centerY
.For some reason I can't get desired results. Of course I need points to be rotated around the center, not the origin.
Side note: if this is any help, I'm trying to plot ellipses defined by regionprops
, using the Centroid
,MajorAxisLength
,MinorAxisLength
and Orientation
properties.
To @Luis Mendo, here's what I get:
and this is another try:
For the bottom feature I'm getting orientation = 8
degrees, more or less, which is totally reasonable. But using your code, which I'm posting below, I get a -8
ellipse!
theta = 0 : 0.05 : 2*pi;
orientation=orientation*pi/180;
delta_x = (majorAxis/2) * sin(theta);
delta_y = (minorAxis/2) * cos(theta);
delta_x_ok = delta_x*cos(orientation) - delta_y*sin(orientation);
delta_y_ok = delta_x*sin(orientation) + delta_y*cos(orientation);
plot(delta_x_ok + centerX, delta_y_ok + centerY, 'r', 'LineWidth', 1.3);
Upvotes: 2
Views: 8283
Reputation: 3221
The orientation
can be an offset to the theta
angle.
theta = 0 : 0.05 : 2*pi;
orientation=orientation*pi/180;
xx = (majorAxis/2) * sin(theta + orientation) + centerX;
yy = (minorAxis/2) * cos(theta + orientation) + centerY;
%plot(xx,yy)
update this will only change the starting point of the ellipse, which has no effect.
Upvotes: 0
Reputation: 112659
xx-centerX
and yy-centerY
can be interpreted as coordinates with respect to axes aligned and centered with the rotated ellipse. To obtain coordinates with respect to the actual (non-rotated, non-centered) axes, say xx2
and yy2
, you only need to apply the transformation
xx2 = (xx-centerX)*cos(orientation) - (yy-centerY)*sin(orientation) + centerX;
yy2 = (xx-centerX)*sin(orientation) + (yy-centerY)*cos(orientation) + centerY;
Then plot the rotated ellipse with
plot(xx2,yy2)
Example:
majorAxis = 2;
minorAxis = 1;
centerX = 10;
centerY = 15;
orientation = -45;
theta = 0 : 0.05 : 2*pi;
orientation=orientation*pi/180;
xx = (majorAxis/2) * sin(theta) + centerX;
yy = (minorAxis/2) * cos(theta) + centerY;
xx2 = (xx-centerX)*cos(orientation) - (yy-centerY)*sin(orientation) + centerX;
yy2 = (xx-centerX)*sin(orientation) + (yy-centerY)*cos(orientation) + centerY;
plot(xx2,yy2)
axis equal
grid
The gap in the ellipse is produced because the theta
step does not divide 2*pi
. To correct that, use for example
theta = linspace(0, 2*pi, 150);
which gives
Upvotes: 2