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
Best Answer