How do I scale every line by the same amount?
Afficher commentaires plus anciens
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 commentaires
Walter Roberson
le 24 Déc 2022
Is the task only about drawing them to look larger, or do you need all of the values for computation purposes?
Mariam Shahab
le 24 Déc 2022
Réponse acceptée
Plus de réponses (1)
Sulaymon Eshkabilov
le 24 Déc 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-');
...
Catégories
En savoir plus sur Object Containers dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!