Borrar filtros
Borrar filtros

Line inside a circle. Hold on not working

1 visualización (últimos 30 días)
Macarena Santillan
Macarena Santillan el 16 de Feb. de 2022
Comentada: DGM el 16 de Feb. de 2022
Hello, I am trying to plot an image of a circle, with 4 points and a line that goes through one of the points but the line is not showing up on the plot.
I am using hold on to have the scatter and the circle in the image but it won't show the line. Any ideas as to how to fix this? I have the equation of the line and I just need it to appear in the same plot as everything else. Is there an easier way? Thank you!
xlocs=[0 -67.4066 -67.5774 -0.1715]; %mm
ylocs=[0 -0.3384 67.0755 67.1668];
%Tilt Line Tline4=pt4+t*o
t= linspace(0,1,4);
r4x=-0.1715+ t*(-0.1021);
r4y=67.1668 +t*(1.0773);
hold on; grid on;axis square;
radius = 55; %Making the radius bigger to make it fit %circle/projectile face diameter 101.6mm=4in
centerX = -35;
centerY = 35;
plot(r4x,r4y);
hold on
rectangle('Position',[centerX - radius, centerY - radius, radius*2, radius*2],'Curvature',[1,1])
hold on
scatter(xlocs,ylocs,'filled','r')
text(xlocs,ylocs,labels,'VerticalAlignment','bottom','HorizontalAlignment','right')
xlabel('Position (mm)')
ylabel('Position (mm)')
xlim([-90 30])
ylim([-30 90])
labels = {'Pin 1','Pin 2','Pin 3','Pin4'};
hold off

Respuesta aceptada

DGM
DGM el 16 de Feb. de 2022
Editada: DGM el 16 de Feb. de 2022
Simon is correct. The line as you defined it is 1mm long.
% pull parameters out
radius = 55; % main circle radius
centerX = -35; % main circle center x
centerY = 35; % main circle center y
linelen = 20; % length of line on pin 4
lineang = 110; % line angle
xlocs=[0 -67.4066 -67.5774 -0.1715]; %mm
ylocs=[0 -0.3384 67.0755 67.1668];
t = linspace(0,linelen,4);
r4x = xlocs(4) + t*cosd(lineang); % actually use the parameters
r4y = ylocs(4) + t*sind(lineang); % instead of repeating literals
hold on; grid on; % only need to hold on once
plot(r4x,r4y);
rectangle('Position',[centerX - radius, centerY - radius, radius*2, radius*2],'Curvature',[1,1])
scatter(xlocs,ylocs,'filled','r')
labels = {'Pin 1','Pin 2','Pin 3','Pin4'}; % assign variables before use
text(xlocs,ylocs,labels,'VerticalAlignment','bottom','HorizontalAlignment','right')
hold off
xlabel('Position (mm)')
ylabel('Position (mm)')
xlim([-90 30])
ylim([-30 90])
axis square;
It's up to you to decide what you want to draw.
I should add that I'm going to guess that this was wrong:
r4x = -0.1715 + t*(-0.1021);
r4y = 67.1668 + t*(1.0773);
There is no (real-valued) angle such that sin(angle) is greater than 1. This isn't a simple rotation by any particular angle. Maybe it's a rotation and an asymmetric scaling, but I doubt that's intentional. If you want a rotation, just pick an angle and use the modified code.
  4 comentarios
Macarena Santillan
Macarena Santillan el 16 de Feb. de 2022
DGM, why do you think r4x = -0.1715 + t*(-0.1021); and r4y = 67.1668 + t*(1.0773); are wrong?
What do you mean by assymetric scaling and how do you choose the line angle? why did you use 110?
Thank you!
DGM
DGM el 16 de Feb. de 2022
When looking at someone else's code, sometimes I can only assume what the intent was and where the numbers came from. To understand the assumptions I made, consider the hypothetical code:
r4x = -0.1715 + t*(-0.1021);
r4y = 67.1668 + t*(0.9948);
This would look to me like you're rotating line t by 95.86 degrees and then translating it by the vector [-0.1715; 67.1668], which is the same as the last point in the point list. It's not at all implausible that someone would simply hardcode the cosd(angle) and sind(angle) as literal constants.
... but that's not what the original code was. The constants used for changing both x and y don't describe a simple rotation, as I mentioned. For that to be the case, asind(1.0773) and acosd(-0.1021) would need to equal the same angle.
The original code could be a reduced form of something like this:
lineang = 95.86;
sk = [1 1.083];
% ...
r4x = xlocs(4) + t*cosd(lineang)*sk(1);
r4y = ylocs(4) + t*sind(lineang)*sk(2);
... where sk is essentially an additional scaling vector. As lineang changes, the endpoint of the line would no longer move in a circle around pin4, but in a slight ellipse, and its apparent angle would no longer be 95.86 degrees if you tried to measure it. While this would give the same constants that you used, this did not seem as likely to be an intended operation.
If you can further explain the process by which you interactively got the line parameters, I could probably generalize the example further. In particular, which tool did you use? What were the landmarks in the image you used to place/align the line? Obviously pin4 would be one point, but what feaures of the image define its length and orientation?

Iniciar sesión para comentar.

Más respuestas (2)

Simon Chan
Simon Chan el 16 de Feb. de 2022
The line is too short and you are not able to see it.
I make it to a blue dot with MarkerSize = 50,
xlocs=[0 -67.4066 -67.5774 -0.1715]; %mm
ylocs=[0 -0.3384 67.0755 67.1668];
%Tilt Line Tline4=pt4+t*o
t= linspace(0,1,4);
r4x=-0.1715+ t*(-0.1021);
r4y=67.1668 +t*(1.0773);
radius = 55; %Making the radius bigger to make it fit %circle/projectile face diameter 101.6mm=4in
centerX = -35;
centerY = 35;
plot(r4x,r4y,'b.','MarkerSize',50);
grid on;axis square;
hold on
rectangle('Position',[centerX - radius, centerY - radius, radius*2, radius*2],'Curvature',[1,1])
scatter(xlocs,ylocs,'filled','r')
labels = {'Pin 1','Pin 2','Pin 3','Pin4'};
text(xlocs,ylocs,labels,'VerticalAlignment','bottom','HorizontalAlignment','right')
xlabel('Position (mm)')
ylabel('Position (mm)')
xlim([-90 30])
ylim([-30 90])
hold off
  1 comentario
Macarena Santillan
Macarena Santillan el 16 de Feb. de 2022
I need the line to remain a line, this is a making a blue dot around it.
Is there a way to just make the line longer?

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 16 de Feb. de 2022
Try this:
xlocs=[0 -67.4066 -67.5774 -0.1715]; %mm
ylocs=[0 -0.3384 67.0755 67.1668];
%Tilt Line Tline4=pt4+t*o
t= linspace(0,1,4);
r4x=-0.1715+ t*(-0.1021);
r4y=67.1668 +t*(1.0773);
hold on; grid on;axis square;
radius = 55; %Making the radius bigger to make it fit %circle/projectile face diameter 101.6mm=4in
centerX = -35;
centerY = 35;
plot(r4x,r4y);
hold on
rectangle('Position',[centerX - radius, centerY - radius, radius*2, radius*2],'Curvature',[1,1])
hold on
scatter(xlocs,ylocs,'filled','r')
labels = {'Pin 1','Pin 2','Pin 3','Pin4'};
text(xlocs,ylocs,labels,'VerticalAlignment','bottom','HorizontalAlignment','right')
xlabel('Position (mm)')
ylabel('Position (mm)')
xlim([-90 30])
ylim([-30 90])
labels = {'Pin 1','Pin 2','Pin 3','Pin4'};
hold off
% Draw lines between all points.
% Tack last point on
for k1 = 1 : length(xlocs)
for k2 = 1 : length(xlocs)
xl = [xlocs(k1), xlocs(k2)];
yl = [ylocs(k1), ylocs(k2)];
line(xl, yl, 'Color', 'm', 'LineWidth', 2);
end
end
  2 comentarios
Macarena Santillan
Macarena Santillan el 16 de Feb. de 2022
I need an specific line that will go thorugh point 4 in the direction given by the eqn in the code
Image Analyst
Image Analyst el 16 de Feb. de 2022
Point 4? Do you mean Pin 4? OK, but what are the endpoints of the line? Is one endpoint Pin 4? What is the other endpoint? What color do you want the line?

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by