Clear Filters
Clear Filters

create and rotate a line up to a convex polygon vertex

4 views (last 30 days)
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
dediydi
dediydi on 29 Dec 2011
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.
dediydi
dediydi on 29 Dec 2011
this is not correct. i will use this algorithm to draw the convex hull of two convex polygon.

Sign in to comment.

Accepted Answer

Sean de Wolski
Sean de Wolski on 29 Dec 2011
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
dediydi
dediydi on 30 Dec 2011
as shown in the picture.
http://www.freeimagehosting.net/5a504
dediydi
dediydi on 30 Dec 2011
maybe it is possible to use cart2sph and then rotate.

Sign in to comment.

More Answers (1)

Andrew Newell
Andrew Newell on 29 Dec 2011
If a convex hull is your goal, you could use convhull.

Categories

Find more on Computational Geometry 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!