Azzera filtri
Azzera filtri

How do I scale every line by the same amount?

8 visualizzazioni (ultimi 30 giorni)
Hi all,
I have a code that allows the user to pick 5 vertices of a polygon. An example of what the 5 vertices may look like, I have attached an image.
This is the code:
xlim([-50 50]);
ylim([-50 50]);
hold on
% code for selecting 5 vertices
n=5;
a=zeros(n,1);
b=zeros(n,1);
for j=1:5
[x,y] = ginput(1);
h1 = text(x,y,int2str(j), ...
'HorizontalAlignment','center', ...
'Color', [1 0 0], ...
'FontSize',8);
a(j)=x;
b(j)=y;
end
%plotting a line between all 5 vertices
plot([a ; a(1)],[b ; b(1)], 'b-');
Q1= [a(1),b(1)];
Q2= [a(2),b(2)];
Q3= [a(3),b(3)];
Q4= [a(4),b(4)];
Q5= [a(5),b(5)];
%% Center of polygon
polyin = polyshape({a},{b});
[x,y] = centroid(polyin);
plot(polyin)
hold on
plot(x,y,'r*')
P=[x,y];
% extention 1
sf=2;
K1= Q1-P;
J1= P + K1*sf;
plot([P(1),Q1(1)],[P(2),Q1(2)],'bo',[P(1),J1(1)],[P(2),J1(2)],'r-');
plot(J1(1), J1(2),'r+');
% extension 2
K2= Q2-P;
J2= P + K2*sf;
plot([P(1),Q2(1)],[P(2),Q2(2)],'bo',[P(1),J2(1)],[P(2),J2(2)],'r-');
plot(J2(1), J2(2),'r+');
% extension 3
K3= Q3-P;
J3= P + K3*sf;
plot([P(1),Q3(1)],[P(2),Q3(2)],'bo',[P(1),J3(1)],[P(2),J3(2)],'r-');
plot(J3(1), J3(2),'r+');
% extension 4
K4= Q4-P;
J4= P + K4*sf;
plot([P(1),Q4(1)],[P(2),Q4(2)],'bo',[P(1),J4(1)],[P(2),J4(2)],'r-');
plot(J4(1), J4(2),'r+');
% extension 5
K5= Q5-P;
J5= P + K5*sf;
plot([P(1),Q5(1)],[P(2),Q5(2)],'bo',[P(1),J5(1)],[P(2),J5(2)],'r-');
plot(J5(1), J5(2),'r+');
The problem i am encountering is that not all the extensions (i.e. lines on the plot) are increased in length by the same size. I want all five lines to be scaled up by the same amount. I will apprecaite any advice on this.
This is an example of what my final plot looks like. As you can see, every red line is enlarged by a diffrent amount.
  2 Commenti
Walter Roberson
Walter Roberson il 24 Dic 2022
Is the task only about drawing them to look larger, or do you need all of the values for computation purposes?
Mariam Shahab
Mariam Shahab il 24 Dic 2022
I need to draw them larger. I am not sure what you mean by needing the values for compuatational purposes. Could you kindly elaborate on what you meant?

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 25 Dic 2022
If the task is to draw them larger, then what you do is create a hgtransform object, and parent the drawing of the unscaled shape to the hgtransform object. Then you set the Matrix property of the hgtransform to do any scaling / translation / rotation you would like. MATLAB will process the transformation and draw according to the transformed coordinates.
Creating the proper Matrix is made much easier by using makehgtform . You can pass multiple transforms in one call. Just remember that the rightmost transform is the one that is processed first.
If you were doing a rotation, the usual sequence would be to use a transform matrix to subtract the center of rotation from the coordinates, then do the rotation, and then add back the center of rotation. That would be like
makehgtform('translate', xc, yc, zc, 'xrotate', RADIANS, 'translate', -xc, -yc, -zc)
Remember processing is right-most first so that is first subtract off centers, then rotate by RANDIANS, then add back centers (leftmost)
In your case it might be more like
makehgtform('translate', xc, yc, zc, 'scale', SCALE, 'translate', -xc, -yc, -zc)
to scale distances relative to (xc, yc, zc)
  3 Commenti
Walter Roberson
Walter Roberson il 27 Dic 2022
Yes. Many of the 2d objects are implemented as 3d objects with z coordinate 0. The major exception is that image() objects do not have a third dimension. You can still apply a hgtransform to image objects, but if you happen to tilt it out of plane then because it is infinitely thin you will only get to see a line.
Walter Roberson
Walter Roberson il 27 Dic 2022
In particular plot() and plot3() both create line() objects, which are 3d objects.

Accedi per commentare.

Più risposte (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov il 24 Dic 2022
The scaling up what you are creating can be done at the start of your code by introducing a scaling factor:
clearvars; close all
SF = 1.5; % Scaling Factor
xlim([-50*SF 50*SF]);
ylim([-50*SF 50*SF]);
hold on
% code for selecting 5 vertices
n=5;
a=zeros(n,1);
b=zeros(n,1);
for j=1:5
[x,y] = ginput(1);
x=x*SF; y=y*SF;
h1 = text(x,y,int2str(j), ...
'HorizontalAlignment','center', ...
'Color', [1 0 0], ...
'FontSize',8);
a(j)=x;
b(j)=y;
end
%plotting a line between all 5 vertices
plot([a ; a(1)],[b ; b(1)], 'b-');
...

Categorie

Scopri di più su Object Containers in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by