CGAL Learning Road (1): Basic operations of two-dimensional points and lines: finding midpoints, square distances, and distances from points to straight lines

1 point coordinates are integer type

Description: The following code performs correlation operations on three points (1, 1), (10, 10), (5, 9) whose coordinates are all integers

Code:

#include <iostream>
#include <CGAL/Simple_cartesian.h>		//笛卡尔坐标相关头文件

using namespace std;

typedef CGAL::Simple_cartesian<double> Kernel;
typedef Kernel::Point_2 Point_2;
typedef Kernel::Segment_2 Segment_2;

int main()
{
    
    
	//定义两个二维点
	Point_2 p(1, 1), q(10, 10);
	cout << "p = " << p << endl;
	cout << "q = " << q.x() << " " << q.y() << endl;
	
	//两点间的平方距离
	double sqDist_pq;				
	sqDist_pq = CGAL::squared_distance(p, q);
	cout << "->两点间的平方距离:"<< CGAL::squared_distance(p, q) << endl;

	//两点的中点
	cout << "->两点间的中点:" << CGAL::midpoint(p, q) << endl;

	//两点确定一条直线
	Segment_2 s(p, q);			
	Point_2 m(5, 9);
	cout << "m = " << m << endl;

	//点到直线的距离
	double sqDist_sm;			
	sqDist_sm = CGAL::squared_distance(s, m);
	cout << "->点到直线的距离" << sqDist_sm << endl;

	//判断三点的关系
	cout << "->p 到 m 再到 q 三点的关系为(与先后顺序有关):" ;
	switch (CGAL::orientation(p, m, q))
	{
    
    
	case CGAL::COLLINEAR:
		cout << "三点共线\n";
		break;
	case CGAL::LEFT_TURN:
		cout << "三点构成左转\n";
		break;
	case CGAL::RIGHT_TURN:
		cout << "三点构成右转\n";
		break;
	}
	cout << "->p 到 q 再到 m 三点的关系为(与先后顺序有关):";

	switch (CGAL::orientation(p, q, m))
	{
    
    
	case CGAL::COLLINEAR:
		cout << "三点共线\n";
		break;
	case CGAL::LEFT_TURN:
		cout << "三点构成左转\n";
		break;
	case CGAL::RIGHT_TURN:
		cout << "三点构成右转\n";
		break;
	}

	return 0;
}

Output result:

p = 1 1
q = 10 10
->两点间的平方距离:162
->两点间的中点:5.5 5.5
m = 5 9
->点到直线的距离8
->p 到 m 再到 q 三点的关系为(与先后顺序有关):三点构成右转
->p 到 q 再到 m 三点的关系为(与先后顺序有关):三点构成左转

2 point coordinates are floating point numbers

Geometric operations using floating point numbers may have unexpected results due to numerical precision issues.

Code:

#include <iostream>
#include <CGAL/Simple_cartesian.h>

using namespace std;

typedef CGAL::Simple_cartesian<double> Kernel;
typedef Kernel::Point_2 Point_2;

int main()
{
    
    
	{
    
    
		Point_2 p(0, 0.3), q(1, 0.6), r(2, 0.9);
		cout << (CGAL::collinear(p, q, r) ? "共线\n" : "不共线\n");
	}
	
	{
    
    
		Point_2 p(0, 1.0 / 3.0), q(1, 2.0 / 3.0), r(2, 1);
		cout << (CGAL::collinear(p, q, r) ? "共线\n" : "不共线\n");
	}
	
	{
    
    
		Point_2 p(0, 0), q(1, 1), r(2, 2);
		cout << (CGAL::collinear(p, q, r) ? "共线\n" : "不共线\n");
	}
	
	return 0;
}

Output result:

不共线
不共线
共线

By observing the coordinates of the three sets of points, we know that each set of points is collinear. But the result of the operation only outputs a set of collinearity.

This is because these fractions cannot be represented as doubles, and the collinearity test will internally compute the determinant of the 3x3 matrix, which is close to but not equal to zero, so the first two sets of points are not collinear.

A similar situation may also occur for points that are close to collinearity but have a left-turn relationship . Due to the rounding error in the determinant calculation process, it may be misjudged as collinear or right-turn.

Guess you like

Origin blog.csdn.net/weixin_46098577/article/details/122620710