How to draw a tangent using a point and an angle?

I need to draw a tangent from (0,0) with an angle of 89.8412 as shown in the curve. How to do that?
img.PNG

 Accepted Answer

Try this:
ad = 89.8412; % Angle (Degrees)
tangline = 200*[0 cosd(ad); 0 sind(ad)];
figure
plot(tangline(1,:), tangline(2,:))
grid
axis([0 2 0 300])
It produces two vectors in the ‘tangline’ matrix, the first row being the x-coordinates and the second row the y-coordinates. It then plots them.
You can check that these are correct with:
Check = atan2d(diff(tangline(2,:)), diff(tangline(1,:)))
It returns the ‘ad’ value.

13 Comments

tangline = 200*[0 cosd(ad); 0 sind(ad)];
Why has 200 been multiplied?
Also, I wanted to plot a line with same angle but starting from (1.68,240.177). How to do that?
The multiplication is simply to correspond to the scale of the plot image you posted.
Replace the ‘0’ values with those coordinates:
LineLen = 200;
tangline = LineLen*[1.68 cosd(ad); 240.177 sind(ad)];
Make ‘LineLen’ whatever you want (other than 0).
Do this:
ad = 89.8412; % Angle (Degrees)
LineLen = 200; % Line Length
Orign = [1.68; 240.177]; % Tangent Line Origin
tangline = LineLen*[0 cosd(ad); 0 sind(ad)] + Orign;
Then plot it as previously:
plot(tangline(1,:), tangline(2,:))
I tried implementing it in Matlab R2015a. But I am getting this error.
How to rectify it?
Please post the applicable parts of your code as text (not an image) so I can run it to see what the problem is. The code I posted did not throw any errors when I ran it.
When I ran the code you posted, and with your files, it ran without error. The ‘tangline’ line was plotted at the left end of the yellow line created by this plot call:
plot([1.68,2],[240.177,240.177])
Is that what you want to do and where you want to plot it? (It seems not to have a context with the rest of your data.)
Yes, but I want the line to start from (1.68,240.177) which was not happening for me. Also some error is coming.
The code I sent is doing the above function?
tangline = 10*[0 cosd(ad);0 sind(ad)]+ origin;
The + origin part when added to tangline is showing error in Matlab R2015a?
Is it something to do with the Matlab version?
Its working in Matlab 2018
Thank you!
Yes, it has everything to do with the MATLAB version. ‘Automatic implicit expansion’ was introduced in R21016b, and that is the reason my original code will not work in R2015a. It is necessary to use the bsxfun function to get the same result.
Try this:
ad = 89.8412;
origin = [1.68;240.177];
lineLen = 10;
tangline = bsxfun(@plus, lineLen*[0 cosd(ad);0 sind(ad)], origin);
plot(tangline(1,:), tangline(2,:))
That should work. It produces the same result my original code does in R2018b.
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!