Determine point position regarding a line

Hi Matlab experts,
I was wondering if anyone would have an idea on how to determine the position (right or left) of a point (in 2D) regarding a line (a segment of a line if we want to be precise) that I have extracted using 'improfile'.
Here's what I've tried:
[cx,cy,c] = improfile(label,xi,yi,j-y_cc); % extracting my line segment
m = [cx(1) cy(1)];
n = [cx(end) cy(end)];
coeff = [[1; 1] m(:)]\n(:); %find the slope and the intercept
for ii = 1:256
for jj = 256:-1:1
if label(ii,jj,k) == 11 || label(ii,jj,k) == 12
pos = sign((n(1) - m(1)) * (jj - m(2)) -...
(n(2) - m(1)) * (ii - m(1)));
%test = jj-ii*coeff(1)-coeff(2); % I have tried looking at the sign of this too
if pos <= 0
labelvst_l(ii,jj,k) = 1;
end
end
end
end
I would be very grateful if anyone could help with this! Thank you
Ismail

Answers (2)

KSSV
KSSV on 28 Oct 2016
If (x1,y1) and (x2,y2) are two points P1,P2. You can decide which point comes first by calculating difference between x coordinates i.e (x2-x1). If (x2-x1) is +ve P1 comes first and then P2. If (x2-x1) is negative P2 comes first and then P1.

2 Comments

Thank you for your answer. The thing is I'm trying to know whether a point P is above or under a line (segment) that is defined by two points Q1 and Q2 that I'm getting from the function 'improfile'

Sign in to comment.

Asked:

on 27 Oct 2016

Answered:

on 22 Apr 2022

Community Treasure Hunt

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

Start Hunting!