43 #include <ilqgames/geometry/line_segment2.h> 44 #include <ilqgames/utils/types.h> 48 bool LineSegment2::Side(
const Point2& query)
const {
49 const Point2 relative_query = query - p1_;
50 const float cross_product = relative_query.x() * unit_direction_.y() -
51 unit_direction_.x() * relative_query.y();
53 return cross_product > 0.0;
56 Point2 LineSegment2::ClosestPoint(
const Point2& query,
bool* is_endpoint,
57 float* signed_squared_distance)
const {
59 const Point2 relative_query = query - p1_;
62 const float dot_product = relative_query.dot(unit_direction_);
63 const float cross_product = relative_query.x() * unit_direction_.y() -
64 unit_direction_.x() * relative_query.y();
66 const float cross_product_sign = sgn(cross_product);
70 if (dot_product < 0.0) {
72 if (is_endpoint) *is_endpoint =
true;
74 if (signed_squared_distance) {
75 *signed_squared_distance =
76 cross_product_sign * relative_query.squaredNorm();
80 }
else if (dot_product > length_) {
82 if (is_endpoint) *is_endpoint =
true;
84 if (signed_squared_distance) {
85 *signed_squared_distance =
86 cross_product_sign * (query - p2_).squaredNorm();
93 if (is_endpoint) *is_endpoint =
false;
95 if (signed_squared_distance)
96 *signed_squared_distance =
97 cross_product_sign * cross_product * cross_product;
99 return p1_ + dot_product * unit_direction_;