四连杆运动仿真的关键是求两个圆的交点,
两个圆有两个交点,哪个才是俺们想要的呢?
简单的说,向量BD叉乘BC,会出现正负号,一次仿真中要不取全正值,要不全取负值
/* 求两个圆的交点
* 0 : error
* 1 : one point
* 2 : two points
* 3 : the same circle
*/
template<typename _EvalType_=Number>
inline int circle_circle_intersection(
_EvalType_ x0,_EvalType_ y0,_EvalType_ r0,
_EvalType_ x1,_EvalType_ y1,_EvalType_ r1,
_EvalType_ *xi,_EvalType_ *yi,
_EvalType_ *xi_prime,_EvalType_ *yi_prime) {
/*http://paulbourke.net/geometry/circlesphere/*/
/* dx and dy are the vertical and horizontal distances between
* the circle centers.
*/
auto dx=x1-x0;
auto dy=y1-y0;
/* Determine the straight-line distance between the centers. */
//d = sqrt((dy*dy) + (dx*dx));
auto d=std::hypot(dx,dy); // Suggested by Kei