Line inside a circle. Hold on not working
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
DGM
2022-2-16
编辑:DGM
2022-2-16
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 个评论
DGM
2022-2-16
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?
更多回答(2 个)
Simon Chan
2022-2-16
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
Image Analyst
2022-2-16
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 个评论
Image Analyst
2022-2-16
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?
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!