Clear Filters
Clear Filters

How to draw tangent line at specified points in a curve

18 views (last 30 days)
I am having a an array of points x,y and respective angles at those specified points. I want to plot a tangent line at those points, i am unable to figure out how to proceed.
As shown in command window 1st column contains x points , 2nd column contains y points and 3rd column the respective tangent angle. Figure 1 is plot between x and y points. Thanking you.
Regards,
Mrinal
  1 Comment
Ngo Nguyen
Ngo Nguyen on 18 Sep 2013
Edited: Ngo Nguyen on 18 Sep 2013
If you have the coordinates M(x,y) , so you can find the slope m:
m= -(x - x0) / (y - y0) , (x0,y0) is the center of the circle
After that, you can plot the tangent line at M y = m*(x - x0) + y0 plot(x,y)

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 18 Sep 2013
Edited: Image Analyst on 18 Sep 2013
You have to figure out the slope at each point, then decide where the endpoints of the tangent line should be, then call line([x1, x2], [y1, y2], 'Color', 'b'). As a rough guess you can probably just say the slope at xn,yn is
slope = (y(n+1)-y(n-1)) / (x(n+1) - x(n-1));
or just get the slope from the tangent angle that you already have. Then the formula for the line is (from high school algebra)
y = slope * (x - x(n)) + y(n);
You just need to figure out how far away from x(n) do you want your endpoints of your tangent line segment. I believe you can just use xLeft = min(x) and xRight = max(x) if you just want the line to go everywhere and not see the endpoints lie inside the plot.
  1 Comment
Mrinal
Mrinal on 18 Sep 2013
Edited: Mrinal on 18 Sep 2013
Thank you. It seems good but i am unable to implement it. As you told i need to use the slope at every point from tangent angle but unable to understand how to use it to draw tangent at points. Here is my code as i am using various functions, so it is not possible for me to upload the whole code but the function in which i am using this is below. Hope you can guide.
% Fill in parr a list of points and tangents to be used for display.
% @param parr (x,y) Array of points.
% @param tan Array of tangents.
% @param lengthStep Distance between points.
function [x y tan] = GetPointListForDisplay(m_Length,r1,r2,count)
global nn ca ce length;
GetLength = m_Length;
length = GetLength;
ca = GetCurvatureAtDeltaLength(0.0);
ce = GetCurvatureAtDeltaLength(length);
radius = 1.0 ./ max(abs(ca), abs(ce));
nn = 3 + (180.0 * length/(2*pi*radius)); % Using modified formula of arc here
lengthStep = length/nn;
currLen = -lengthStep;
while (1)
currLen = currLen + lengthStep;
if (currLen > m_Length)
currLen = m_Length;
end
[x,y] = GetPointAtDeltaLength(currLen);
[tan] = GetTangentGridBearingAtDeltaLength(currLen);
z(count,1) = x;
z(count,2)= y;
z(count,3)= tan;
figure(1);
%plot(z(count,1).*sin(z(count,3)),z(count,2).*cos(z(count,3)),'b*');
%plot(z(1,count).*cos(z(3,count)),z(2,count).*sin(z(3,count)),'b*');
plot(z(count,1),z(count,2),'b*');
%plot(z(1,count),z(2,count),'b*');
hold on;
%pause(0.1);
count=count+1;
axis equal;
if (currLen >= m_Length)
z(count,1)= tan
break;
end
end
end

Sign in to comment.

Categories

Find more on Embedded Coder in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!