Download Checking the correspondence of a point to a line segment

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Duality (projective geometry) wikipedia , lookup

Line (geometry) wikipedia , lookup

Transcript
Checking the correspondence of a point to
a line segment
The problem of correspondence of a point to a line segment is one of the
fundamental problems of the computational geometry. The problem is not as banal
as it might seem as the algorithms that check if a point belongs to a segment are not
that easy.
Given are three points of coordinates:
To check if the point C belongs to the segment |AB| one needs to check if the three
points are collinear.
Collinearity
Definition
The three points are collinear if they are on one single line.
The collinearity of the three points A, B and C can be easily established by the use of
determinant det(A,B,C) of two-dimensional matrices:
If the value of the determinant det(A,B,C)>0, point C is on the left hand side of the AB
vector. This situation can be observed below.
If the value of the determinant det(A,B,C)=0, point C is on the vector AB; in other
words, points A, B and C are collinear.
If the value of the determinant det(A,B,C)<0, point C is on the right hand side of the
vector AB.
Example 5.1
Given are three points: A=(-1,2), B=(2,1) and C=(1,3). We would like to establish the
location of the point C in relation to vector AB.
Solution
Creation of a determinant det(A,B,C) from the three points and then calculating its
value. (Sarrus rule).
Value of determinant det(A,B,C) is positive. Thus, point C is on the left hand side of
the AB vector.
A program pointvec.cpp which task is to establish the location of the three points can
be seen below.
// The program checks the collinearity of point C
// and vector AB
// pointvec.cpp
#include <iostream>
using namespace std;
struct coord
{
int x,y;
};
//points coordinates
int determine(int a11, int a12, int a21, int a22, int a31, int a32)
{
return a11*a22+a21*a32+a31*a12-a12*a21-a22*a31-a11*a32;
}
int main()
{
int i, det; //determinant of matrices
coord points[3]; //array of points
cout << "Input coordinates of points A, B, C: " << endl;
for (i=0;i<3;i++)
{
cin >> points[i].x;
cin >> points[i].y;
}
// Calculating determinants of matrices
det=determine(points[0].x,points[0].y,points[1].x,points[1]. y,
points[2].x,points[2].y);
// Checking the location of point C in elation to vector AB:
if (det==0)
cout << "Points A, B, C are collinear" << endl;
else
if (det>0)
cout << "Point C is in the left hand side of the vector AB" <<
endl;
else
cout << "Point C is on the right hand side of the vector AB" <<
endl;
return 0;
}
Outcome of the program:
pointvec.cpp, pointvec, input the coordinates of points, points A, B, C are collinear
Exercise 5.1
Establish collinearity of points A, B, C.
a) A=(-1,1), B=(1,3) and C=(2,4)
b) A=(0,-1), B=(-1,2) and C=(1,4)
b) A=(2,-1), B=(-2,2) and C=(-1,-4)
We can already check if points A, B and C are on one single line but we need to
examine a situation when the point C is on a line segment |AB|.
Thus, apart from collinearity, there needs to be one more condition for the point C to
belong to the segment |AB|:
The above condition notated in the C++ language code may look as follows:
// condition: point C is on the segment AB
if ((min(points[0].x, points [1].x) <= points [2].x) &&
(points [2].x <= max(points [0].x, points [1].x)) &&
(min(points [0].y, points [1].y) <= points [2].y) &&
(points [2].y <= max(points [0].y, points [1].y)))
cout << "Point C corresponds to the segment AB" << endl;
What’s left is to connect the two conditions:
- collinearity of three points A, B i C
- point C is on the segment AB.
Example
Example of a program checking the correspondence of a point to a line segment.
// Example of a program checking the correspondence of a point to a line segment.
// corresp.cpp
#include <iostream>
using namespace std;
struct wsp
{
int x, y;
};
//points coordinates
int determine(int a11, int a12, int a21, int a22, int a31, int a32)
{
return a11*a22+a21*a32+a31*a12-a12*a21-a22*a31-a11*a32;
}
int main()
{
int i, det;
coord points[3]; //array of points
cout << "Input coordinates of points A, B, C: " << endl;
for (i=0;i<3;i++)
{
cin >> points[i].x;
cin >> points[i].y;
}
// Calculating the determinant of matrices
det=determine(points[0].x, points [0].y, points [1].x, points [1]. y,
points [2].x, points [2].y);
if (det==0)
// condition: point C is on the segment AB
if ((min(points [0].x, points [1].x) <= points [2].x) &&
(points [2].x <= max(points [0].x, points [1].x)) &&
(min(points [0].y, points [1].y) <= points [2].y) &&
(points [2].y <= max(points [0].y, points [1].y)))
cout << "Point C corresponds to the segment AB" << endl;
else
cout << "Point C does not correspond to the segment AB" << endl;
return 0;
}
Outcome of the program:
Input coordinates of points, Point C corresponds to the segment AB