create and rotate a line up to a convex polygon vertex

Hello,
how can i create and rotate a line up to a convex polygon vertex ?
line is tangent to vertex and rotate the line by the smallest angle between the line and the segment following the vertex it passes through (in clockwise order.)
step1: create a convex polygon step2: find the leftmost point of the polygon step3:create a line that is tangent to the leftmost point of the polygon. step4:rotate the line by the smallest angle between the line and the segment following the vertex it passes through (in clockwise order.)
step 1,2 and 3 ok, but i have problems with step 4.
i have already tried
*angle = atan2(norm(cross(v1,v2)),dot(v1,v2));*
that method but when i find the direction of vector it equal to 0 0 0 .
please help me.
thanks

8 Comments

Can you provide a small set of sample data and maybe steps for creating a plot with a description of what you want?
Dear Sean,
thanks for your attention,
step1: create a convex polygon
step2: find the leftmost point of the polygon
step3:create a line that is tangent to the leftmost point of the polygon.
step4:rotate the line by the smallest angle between the line and the segment following the vertex it passes through (in clockwise order.)
i have already tried
angle = atan2(norm(cross(v1,v2)),dot(v1,v2));
that method but when i find the direction of vector it equal to 0 0 0 . what can i do ?
But won't the left most point be a vertex and thus no tangent defined? Please clarify. Step 4 is not at all clear either. A picture would be worth a 1000 words. Could you draw a little sample image and post it to a free image hosting website?
i hope it mekes the problem clear
http://www.freeimagehosting.net/9f329
Those lines are not tangents to the vertex (undefined); they're vertical lines at the vertex. So you want to calculate the angle between that vertical line and the two edges of the polygon touching that vertex?
My interpretation of this procedure is that, after step 4, you will have a line that extends one of the edges of the polygon. Is that correct?
not two edges. i use the angle to rotate (in clockwise) that vertical lines by the smaller angle. so it is enough to calculate the angle between the vertical line and the above edge. maybe a another picture necessary here :)
http://www.freeimagehosting.net/0f9f9
thanks for your interest.
this is not correct. i will use this algorithm to draw the convex hull of two convex polygon.

Sign in to comment.

 Accepted Answer

So to calculate you're angle, v1 will always be a unit vector vertical:
v1 = [0 1 0];
v2 needs to be turned into a unit vector:
v2 = [dx dy 0]; %dx,dy are the differences in the vertex we care about and the next one over
v2 = v2./norm(v2); %make unit vector
Then calculate the angle
ang = atan2(norm(cross(v1,v2)),dot(v1,v2))
To rotate it you can use hgtransform:
x = [0 1 1 0];
y = [0 0 1 1];
h = fill(x,y,'c');
t = hgtransform('Parent',gca);
set(h,'Parent',t);
rot = makehgtform('zrotate',ang);
set(t,'matrix',rot)
More:
So something along the lines of:
P(1,:) = [1.5 2.0 0];
P(2,:) = [0.7 3.2 0];
P(3,:) = [0.5 4.5 0];
P(4,:) = [0.7 5.2 0];
P(5,:) = [1.7 5.3 0];
P(6,:) = [2.5 5.0 0];
P(7,:) = [3.0 4.5 0];
fill(P(:,1),P(:,2),'c');
v1 = [0 1 0];
v2 = P(4,:);
v2 = v2./norm(v2);
ang = atan2(norm(cross(v1,v2)),dot(v1,v2));
lineH = line([0 2],[0 10]);
pause(2) %demo
t = hgtransform('Parent',gca);
set(lineH,'Parent',t);
rot = makehgtform('zrotate',ang);
set(t,'matrix',rot)

14 Comments

Dear Sean,
thank you a lot. it works.i calculated the angle value. but now i have problems about how to rotate.Could you help me about the code below ?
P(1,:) = [1.5 2.0 0];
P(2,:) = [0.7 3.2 0];
P(3,:) = [0.5 4.5 0];
P(4,:) = [0.7 5.2 0];
P(5,:) = [1.7 5.3 0];
P(6,:) = [2.5 5.0 0];
P(7,:) = [3.0 4.5 0];
Q(1,:) = [2.2 2.6 0 0];
Q(2,:) = [2.0 3.3 0 0];
Q(3,:) = [2.4 4.7 0 0];
Q(4,:) = [4.0 5.9 0 0];
Q(5,:) = [4.5 5.3 0 0];
Q(6,:) = [4.7 3.7 0 0];
Q(7,:) = [4.3 2.5 0 0];
i draw polygon with this vertex values. and draw the vertical.
how can i rotate the vertical by the angle value?
you show the method but i could not implemet it to the verticals
it will be enough for one vertex to another.i have the code which calculates the min vertex and it's number.
Have you looked at the documentation for makehgtform?
How are you plotting Q? Do you want to rotate about the chosen vertex?
that is, the rotation is only about calculated angle. this rotation contines until the side of verticals change. and then we find the pocked lid. it is in the picture too.
http://www.freeimagehosting.net/d26f4
What you have labelled as the pocked lid IS the convex hull of the two polygons.
Reposting dedilyi's earlier comment but with the link corrected:
i have read it. i plot P and Q with vertex numbers and values which are shown below. I want to rotate verticals not about vertex by calculated angle values.like in the picture http://www.freeimagehosting.net/82c80
P(1,:) = [1.5 2.0 0];
P(2,:) = [0.7 3.2 0];
P(3,:) = [0.5 4.5 0];
P(4,:) = [0.7 5.2 0];
P(5,:) = [1.7 5.3 0];
P(6,:) = [2.5 5.0 0];
P(7,:) = [3.0 4.5 0];
Q(1,:) = [2.2 2.6 0 0];
Q(2,:) = [2.0 3.3 0 0];
Q(3,:) = [2.4 4.7 0 0];
Q(4,:) = [4.0 5.9 0 0];
Q(5,:) = [4.5 5.3 0 0];
Q(6,:) = [4.7 3.7 0 0];
Q(7,:) = [4.3 2.5 0 0];
pocked lids means a edge of convex hull which is not belong P or Q convex polygon as shown on the picture http://www.freeimagehosting.net/d26f4
Exactly. So calculate your convexhull of each small polygon and the two together. The edges that aren't in either polygon but are edges of the convex hull of the two polygons together are your pocked lids.
i am greatfull to you Mr.Sean,Now i can rotate verticals by angle. But still i have a problem.i want to rotate these verticals in clockwise.(that is to right).thank you very much for your help.
also i want to draw vertical of Q polygon adjacent to the Q polygon and the same for P polygon.
as shown in the picture.
http://www.freeimagehosting.net/5a504
maybe it is possible to use cart2sph and then rotate.

Sign in to comment.

More Answers (1)

If a convex hull is your goal, you could use convhull.

Categories

Community Treasure Hunt

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

Start Hunting!