How to draw a figure..

48 views (last 30 days)
Ark
Ark on 22 Mar 2014
Edited: Ark on 25 Mar 2014
Hi everyone
I was wondering how to draw a star's outline using 23 points, so that 13 of them are closer to begining of coordinate system and other 13 further
To start with, my idea is to set 2 points - one closer and second further and then mark the rest using that 2 points from begining but moving an angle.
So i declared matrix full of "0" and then i would like to place all the matrix points on the graph by using for ex. the rotation matrix y = A * X where A = [cos (theta),-sin (theta) sin (theta) cos (theta )], and connect the dots as vectors using plot command. But im quite beginner in Matlab and i have no idea how to put my ideas on code.
I deduced something like that:
M=zeros(2,26)
...
for K=2:2:13
M(:,k)=0,5*A*M(:,k-1) %Point 2
M(:,k+1)=2*A(M(:,k) %Point 3 using Point 2 etc.
plot(...)
...
end
How to obtain the desired effect? Is that even possible? :/
I would be very grateful for solution. Thanks in advance

Accepted Answer

Star Strider
Star Strider on 22 Mar 2014
Edited: Star Strider on 22 Mar 2014
This seems to do what you want. Change (pi/13) to (pi/12) to get a symmetrical version the figure you drew, since it’s not symmetrical and there are more points with (y<0) than (y>0). This isn’t exactly what you drew, but it’s close:
th = 0:(pi/13):(2*pi);
rv = [1.0; 0.5];
for k1 = 1:length(th)
r = rv(rem(k1,2)+1); % Choose ‘rv(2)’ for even ‘k1’, ‘rv(1)’ for odd ‘k1’
[x(k1),y(k1)] = pol2cart(th(k1),r);
end
figure(1)
plot(x,y)
axis equal
Change the values in the rv vector and the (pi/13) interval to change the appearance of the star. See the pol2cart. documentation for details.
This was fun! I’d never considered that possibility.

More Answers (3)

Ark
Ark on 22 Mar 2014
Thank you very much! I really appreciate your reply. It looks briliant and works indeed :)
But i wonder if it's possible to do the same using matrix and moreover recive for ex. coordinates of all 26 points from graph? How to implement such idea?
Kindest regards.
  1 Comment
Star Strider
Star Strider on 22 Mar 2014
Edited: Star Strider on 22 Mar 2014
My pleasure!
The coordinates of all 26 points are in the [x,y] vectors. To see the [x,y] vectors, add this line after the loop:
xy = [x' y']
Since x and y are originally row vectors, the transpose operator (') converts them to column vectors so they will display vertically. Putting them inside the square brackets [] converts them into a matrix so they will display side-by-side.
I don’t know what you mean by ‘using matrix’. I used the pol2cart function, but you can do the same thing by defining x and y this way in the loop:
x(k1) = r*cos(th(k1));
y(k1) = r*sin(th(k1));

Sign in to comment.


Image Analyst
Image Analyst on 22 Mar 2014
Attached is a demo that I made from Roger Stafford's star formula.

Ark
Ark on 22 Mar 2014
Edited: Ark on 22 Mar 2014
Thank you both for reply but I have got a additional question if it's possible to do the same using zeros matrix with plot command and moreover recive for ex. coordinates of all 26 points from graph?
  1 Comment
Star Strider
Star Strider on 22 Mar 2014
To get data from the figure itself, you need to use the figure’s handles. (I encourage you to explore the general details of ‘object handles’ on your own. Knowing how they work will help you with plots and other objects in MATLAB.)
This is how to get the x and y data from the plot:
h1l = findobj('type','line');
x1d = get(h1l,'XData');
y1d = get(h1l,'YData');
xy2 = [x1d' y1d']
If you display them and the ‘xy’ matrix in my original code, you will see that they are the same. (Since figure(1) is the only figure in the workspace, MATLAB assumes every object reference refers to it.)

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!