![How_can_i_draw_a_line_at_an_angle_of_90_degrees_from_another_line_having_a_starting_point_different_than_0_0.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/257434/How_can_i_draw_a_line_at_an_angle_of_90_degrees_from_another_line_having_a_starting_point_different_than_0_0.jpeg)
Hello! How can i draw a line at an angle of 90 degrees from another line, having a starting point different than (0,0)?
9 ビュー (過去 30 日間)
古いコメントを表示
Andreea Stanescu
2019 年 12 月 19 日
コメント済み: Andreea Stanescu
2019 年 12 月 28 日
Hello! I have a problem with drawing a square, only knowing the starting point and another random one. How can i draw another line at an angle of 90 degrees from the one made by the known point and the random one? this is what i have so far:
Thank you!
format long g;
format compact;
fontSize = 15;
x = [5.5, 6, 4.5, 3.5, 5, 5, 3, 2, 2.5, 3, 2, 2.5, 4, 5, 4.5]
y = [3, 2, 2.5, 1.5, 1.5, 1, 0.5, 1, 2, 3, 4, 4.5, 5, 4, 3.5]
% Append first point to last to close the curve
x = [x, x(1)];
y = [y, y(1)];
plot(x, y, 'r*');
grid on;
knots = [x; y];
areaOfPolygon = polyarea(x,y);
numberOfPoints = length(x);
% Interpolate with a spline curve and finer spacing.
originalSpacing = 1 : numberOfPoints;
% Make 9 points in between our original points that the user clicked on.
finerSpacing = 1 : 0.1 : numberOfPoints;
% Do the spline interpolation.
splineXY = spline(originalSpacing, knots, finerSpacing);
% Plot the interpolated curve.
hold off;
plot(knots(1, :), knots(2, :), 'ro', 'LineWidth', 2, 'MarkerSize', 16);
hold on;
plot(splineXY(1, :), splineXY(2, :), 'b+-', 'LineWidth', 2, 'MarkerSize', 8);
title('Blue Spline Between Red Knots', 'FontSize', fontSize);
legend('Knots', 'Spline');
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
grid on;
hold on;
% Get a known index. "known" because it's one of the training points.
knownIndex = randperm(length(x), 1)
% Get a unknown index. "unknown" because it's one of the interpolated points.
unknownIndex = randperm(length(finerSpacing), 1)
% Get the x,y coordinates for these indexes.
xKnown = knots(1, knownIndex)
yKnown = knots(2, knownIndex)
xUnknown = splineXY(1, unknownIndex)
yUnknown = splineXY(2, unknownIndex)
A=[xKnown, yKnown]
B=[xUnknown, yUnknown]
% Now draw a line between them in dark green.
darkGreen = [0, 0.5, 0];
plot([xKnown, xUnknown], [yKnown, yUnknown], 'o-', ...
'Color', darkGreen, 'MarkerSize', 24, 'LineWidth', 3);
hold on
0 件のコメント
採用された回答
Chien-Han Su
2019 年 12 月 28 日
Hope I understand your question correctly.
If you want to add a line segment (denoted as newL) which is perpendicular to a randomly generated line segment (denoted as oldL hereafter) according to your data, you can record the slope of oldL, then calulate the slope of newL. With this new slope, you can add line segment to the end points of oldL to get what you want. That is, add the code below after yours,
% slope of oldL
slope = (yKnown - yUnknown)./(xKnown - xUnknown);
% slope and tilt angle of newL
perSlope = -1/slope;
perTheta = atan(perSlope);
% set the lenght of line segment you want to add to the figure
lineLength = 4;
halfLineLength = lineLength/2;
% calculate the end points of a newL based on the Known points of oldL
xPerKnown = [xKnown + halfLineLength*cos(perTheta), ...
xKnown - halfLineLength*cos(perTheta)];
yPerKnown = [yKnown + halfLineLength*sin(perTheta), ...
yKnown - halfLineLength*sin(perTheta)];
% calculate the ends points of another newL based on the Unknown points of oldL
xPerUnknown = [xUnknown + halfLineLength*cos(perTheta), ...
xUnknown - halfLineLength*cos(perTheta)];
yPerUnknown = [yUnknown + halfLineLength*sin(perTheta), ...
yUnknown - halfLineLength*sin(perTheta)];
% draw these two newL
plot([xPerKnown(1), xPerKnown(2)], [yPerKnown(1), yPerKnown(2)], 'o-', ...
'Color', darkGreen, 'MarkerSize', 24, 'LineWidth', 3);
plot([xPerUnknown(1), xPerUnknown(2)], [yPerUnknown(1), yPerUnknown(2)], 'o-', ...
'Color', darkGreen, 'MarkerSize', 24, 'LineWidth', 3);
% set the unit length of two axis to be equal, to get a clear visualization of 90 degree
axis equal
and you can get
![How_can_i_draw_a_line_at_an_angle_of_90_degrees_from_another_line_having_a_starting_point_different_than_0_0.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/257434/How_can_i_draw_a_line_at_an_angle_of_90_degrees_from_another_line_having_a_starting_point_different_than_0_0.jpeg)
3 件のコメント
Chien-Han Su
2019 年 12 月 28 日
Replace
% set the lenght of line segment you want to add to the figure
lineLength = 4;
halfLineLength = lineLength/2;
with
% calculate the length of oldL and assign it to halfLineLength
oldLineLength = sqrt((xKnown - xUnknown)^2 + (yKnown - yUnknown)^2);
halfLineLength = oldLineLength;
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Discrete Data Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!