create and rotate a line up to a convex polygon vertex
2 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Sean de Wolski
2011 年 12 月 29 日
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 件のコメント
その他の回答 (1 件)
参考
カテゴリ
Help Center および File Exchange で Computational Geometry についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!