BeeTiau
BeeTiau

Reputation: 69

Finding an intersection of a curve in Matlab

I have a geometrical problem as shown in the picture below, enter image description here

I have prepared the sketch of the problem shown at the top right corner. Basically, from point C, or denoted as M in the sketch, I want to:

  1. Measure L2 which is the distance of point C to the associated pole (black line). The coordinate of C is known. The pole is basically from running from the origin, i.e. {0,0} moving outward every 30-degree over 360-degree. The pole will always intersect with the blue point whose coordinate is also known.
  2. Measure L1 which is the distance from the origin to a point that is obtained from the distance of an intersection between L3 with the blue curvature. It is hard to explain but I hope the sketch would help.

How is it possible to solve this problem using Matlab?

below is the digital dataset

For blue points (labeled as "actual" in the plot)

Theta(deg)      x          y
================================
       0    1.0148         0
   20.0000    0.9397    0.3420
   40.0000    0.8042    0.6748
   60.0000    0.5727    0.9919
   80.0000    0.2073    1.1757
  100.0000   -0.2073    1.1757
  120.0000   -0.5727    0.9919
  140.0000   -0.8042    0.6748
  160.0000   -0.9397    0.3420
  180.0000   -1.0148         0
  200.0000   -0.9397   -0.3420
  220.0000   -0.8042   -0.6748
  240.0000   -0.5727   -0.9919
  260.0000   -0.2073   -1.1757
  280.0000    0.2073   -1.1757
  300.0000    0.5727   -0.9919
  320.0000    0.8042   -0.6748
  340.0000    0.9397   -0.3420
  360.0000    1.0148         0

And for the one labeled as "predicted"

   x-pred     y-pred
===========================
    1.0953    0.2897
    1.0292    0.6399
    0.8390    0.9013
    0.5476    1.1899
    0.1902    1.2300
   -0.1902    1.3091
   -0.5476    1.0693
   -0.8390    0.9247
   -1.0292    0.4744
   -1.0953    0.2070
   -1.0292   -0.2885
   -0.8390   -0.5168
   -0.5476   -0.8711
   -0.1902   -0.9193
    0.1902   -1.0086
    0.5476   -0.8278
    0.8390   -0.6483
    1.0292   -0.3125
    1.0953   -0.0000

Any ideas will be appreciated.

Thanks in advance.

Upvotes: 3

Views: 140

Answers (1)

jodag
jodag

Reputation: 22174

Step 1 : Find points B, and D.

D is the point where a line segment in the blue curve intersects ray AC. To find the intersection between a ray and a line segment we can do the following. Given the points at the endpoints of the line segment which we call p1 and p2 and a ray beginning at A and passing through C, we solve the vector equation p1 + t1*(p2-p1) == A + t2*(C-A) for scalars t1 and t2. The values of t1 and t2 tell us both if there is an intersection, and where the intersection occurs.

  • There is an intersection if and only if t2 >= 0 and 0 <= t1 <= 1.
  • If there is an intersection, then it occurs at D = p1 + t1*(p2-p1).

B is just the one of the points, either p1 or p2 depending on the notation being used.

Code:

% Assuming C and A are represented as 1x2 matrices.
% Assuming blue is an Nx2 matrix containing the x-y coordinates of
%   the points in the blue curve in order of increasing theta.

% Search for intersecting line segment to find B and D
N = size(blue,1);
for bidx = 1:N
    p1 = blue(bidx,:);
    p2 = blue(mod(bidx,N)+1,:);
    % solve for t1 and t2
    t = [(p2-p1).' (A-C).']\((A-p1).');
    % if we find intersection we are done.
    if t(2) >= 0 && t(1) >= 0 && t(1) <= 1
        D = p1 + t(1)*(p2-p1);
        B = p2;
        break;
    end
end

Step 2 : Compute L1 and L2.

L2 is the component of AC orthogonal to AB.

L1 is the component of AD parallel to AB.

Using this knowledge compute L1 and L2 as follows...

AB = B-A;
AC = C-A;
AD = D-A;
% projection of vector X onto vector Y.
proj = @(X,Y) Y * dot(X,Y)/dot(Y,Y);

% get the desired distances
L2 = norm(AC - proj(AC,AB));
L1 = norm(proj(AD,AB));

Upvotes: 2

Related Questions