How do i make a line with a fixed length and fixed y axis move its second point freely within a closed region
4 ビュー (過去 30 日間)
古いコメントを表示
I need some help or a method to solve this.
this is my script:
clear,clc
R = randi([2 10]);
d = randi([0 R]);
th = -(pi/2)*rand(1);
q = [d,th];
disp("Radius");
disp(R);
disp("Joint Poses");
disp(q);
x = d-R*sin(th);
y = R*cos(th);
p = ([x,y]);
z = sqrt(x^2+y^2);
zz = sqrt(R^2-0^2);
disp("TCP poses");
disp(p);
disp("test")
disp(z);
disp(zz);
hold on
rectangle('Position',[-R -R 4*R 2*R],'Curvature',1,'edgecolor',[0.4,0.32,1],'facecolor',[0.38,0.3,0.75])
plot(x,y,'g*');
line([zz x], [0 y],'color',[1,0.4,0],'linewidth',1.2);
axis ([-R 3*R 0 3*R]);
hold off
which generates f.x. this

the area has the height of 10 (the circle radius on the side is also 10), which means the line should have the length of 10.
But the basepoint is fixed and not the length. I want a line with fixed length, which the x-axis of the baseline varies and fixed for the y axis.
0 件のコメント
回答 (1 件)
BhaTTa
2025 年 8 月 14 日
Hey @Zain Ahmed, Looking at your code, I understand the issue. You have a semicircular workspace where you want a line of fixed length (equal to radius R) that can rotate, with the base point varying along the x-axis but fixed at y=0.
Please refer to the below code and take it as reference and modify it accodingly:
clear, clc
R = randi([2 10]);
% Base point x-coordinate can vary from 0 to some range
x_base = randi([0 R]); % or use: x_base = R * rand(); for continuous range
th = -(pi/2) * rand(1); % Angle from vertical (negative for clockwise)
q = [x_base, th];
disp("Radius");
disp(R);
disp("Joint Poses [x_base, theta]");
disp(q);
% Calculate end point with FIXED line length = R
x = x_base + R * sin(th); % End point x
y = R * cos(th); % End point y (height from base)
p = [x, y];
line_length = sqrt((x - x_base)^2 + y^2); % Should equal R
disp("TCP poses");
disp(p);
disp("Line length (should equal R)");
disp(line_length);
disp("Radius R");
disp(R);
% Plotting
hold on
% Draw semicircular workspace
rectangle('Position',[-R -R 4*R 2*R],'Curvature',1,'edgecolor',[0.4,0.32,1],'facecolor',[0.38,0.3,0.75])
% Mark base point
plot(x_base, 0, 'ro', 'MarkerSize', 8, 'MarkerFaceColor', 'r');
% Mark end point
plot(x, y, 'g*', 'MarkerSize', 10);
% Draw line from base to end point
line([x_base x], [0 y], 'color', [1,0.4,0], 'linewidth', 2);
% Add labels
text(x_base, -0.2, sprintf('Base: (%.1f, 0)', x_base), 'HorizontalAlignment', 'center');
text(x, y+0.3, sprintf('End: (%.1f, %.1f)', x, y), 'HorizontalAlignment', 'center');
text((x_base+x)/2, y/2+0.5, sprintf('L=%.1f', line_length), 'HorizontalAlignment', 'center', 'BackgroundColor', 'yellow');
axis([-R 3*R -R 3*R]);
grid on;
title(sprintf('Fixed Length Line (L=%d) with Variable Base Position', R));
xlabel('X');
ylabel('Y');
hold off
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!