How do I merge lines and curves into one function?

4 views (last 30 days)
I want to create a trajectory function. The trajectory consists of a straight part (with varying length), curve (with varying degree and radius) and another straight part (with varying length).
For example:
Straight part: (0,-10000) -> (0 ,0)
Curve: Start point: (0,0), Radius: 3000, Angle: 90 degrees, End point: (3000,3000)
Straight part: (3000,3000) -> (13000,3000)
Here is a code writing the trajectory:
clear all; close all;
%% Variables
R = 3000;
Angle = 90;
Straight = 10000;
%% Plot Trajectory
n = 100000;
plot([0 0], [-Straight 0],'--','Color','k'); hold on; % Plot straight
P1_c = ([0;0]); % First point of the centerline curve
P2_c = ([R-cos(deg2rad(Angle))*R;sin(deg2rad(Angle))*R]); % Last point of the centerline curve
P0_c = ([R;0]); % Center of the centerline curve
v1_c = P1_c-P0_c; % Difference between first point and center
v2_c = P2_c-P0_c; % Difference between second point and center
cp_c = det([v1_c,v2_c]); % Cross product of v1 and v2
a_c = linspace(0,atan2(abs(cp_c),dot(v1_c,v2_c)),n); % Angle range
v3_c = [0,-cp_c;cp_c,0]*v1_c; % v3 lies in plane of v1 and v2 and is orthog. to v1
v_c = v1_c*cos(a_c)+((norm(v1_c)/norm(v3_c))*v3_c)*sin(a_c); % Calculating arc
plot(v_c(1,:)+P0_c(1),v_c(2,:)+P0_c(2),'--','Color','k');hold on; % Plot curve
Mcf = (P2_c(2)-P0_c(2))/(P2_c(1)-P0_c(1)); % Calculate slope of line from center to end point curve
m = -1/Mcf; % Calculate slope of track IN slope (tangent to end point curve)
Dx = sqrt(Straight^2/(m^2+1)); % Calculate x difference between first and last point track IN
Dy = Dx*m; % Calculate y difference between first and last point track IN
plot([P2_c(1) P2_c(1)+Dx], [P2_c(2) P2_c(2)+Dy],'--','Color','k'); hold on; % Plot straight
I want to simulate a car going through the corner. The center of the wheels will follow the trajectory (see figure). At the moment, I need some if-statements to know if the next wheel is at the vertical straight part, in the curve or at the horizontal straight part. I want to make my code a lot easier by merging all three lines so I do not need all the if-statements. How can I do this?
I already tried some fitting functions with the curve fitting toolbox, but while I have a vertical line in the plot, none of the fitting functions really work. Is there a way to do this with spline? Or can I just write a function which is conditioned in x and y to get the result I want?
Hope you can help me. Thanks in advance!
  2 Comments
John D'Errico
John D'Errico on 1 Jul 2019
You want some general trajectory, that may or may not have a single-valued functional form. That is, for any x, you would have a single line, possibly a vertical line, or possibly a circular arc. How many degrees in that arc?
As such, tools like the curve fitting toolbox do NOT apply, because the result is not a usable function. For any x, you need to get ONE value for y our for that to happen. The same applies to splines - give it an x, you get a y.
Worse, you have some point in (x,y), and it sounds like you want to find where it lies on that general path through space. But what if the (x,y) pair does not lie on that path, but is off of it, even by an infinitesimal amount?
Sorry, but there are not tools out there that do what you want right out of the box. You can use my distance2curve function (found on the file exchange) to find the closest point on a general curve, but it cannot use specific totally arbitrary curve shapes that you may provide. It just works with a sequence of points that define a path in space.
Jannes Horen van
Jannes Horen van on 1 Jul 2019
Thanks for your answer!
I already thought spline and fitting tools were indeed not possible, but it's good you confirmed it. I will try to use your function. Hopefully it gives a good result for my model.

Sign in to comment.

Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!