카테고리 없음
알고리즘/ 한 점과 직선 사이의 거리 계산
cleitia
2020. 3. 2. 10:28
Distance from a point to a line/segment
// 일반 함수
/* 점과 직선 사이의 거리 계산 */
static std::array<int, 3> cross(const std::array<int, 3> &a,
const std::array<int, 3> &b)
{
std::array<int, 3> result;
result[0] = a[1] * b[2] - a[2] * b[1];
result[1] = a[2] * b[0] - a[0] * b[2];
result[2] = a[0] * b[1] - a[1] * b[0];
return result;
}
static double point_to_line_distance(const cv::Point &p, const cv::Vec4i &line)
{
std::array<int, 3> pa{ { line[0], line[1], 1 } };
std::array<int, 3> pb{ { line[2], line[3], 1 } };
std::array<int, 3> l = cross(pa, pb);
return std::abs((p.x * l[0] + p.y * l[1] + l[2])) * 1.0 /
std::sqrt(double(l[0] * l[0] + l[1] * l[1]));
}
// 또는
// 템플릿 함수화
template<typename point3_t>
point3_t cross(const point3_t vec1_pos, const point3_t vec2_pos)
{
return point3_t(
vec1_pos.y * vec2_pos.z - vec1_pos.z * vec2_pos.y,
vec1_pos.z * vec2_pos.x - vec1_pos.x * vec2_pos.z,
vec1_pos.x * vec2_pos.y - vec1_pos.y * vec2_pos.x);
}
/* 점과 직선 사이의 거리 계산 */
template <typename point2_t, typename vec4_t>
double point_to_line_distance(const point2_t &p, const vec4_t &line)
{
struct point3_d { double x, y, z; point3_d() {}; point3_d(double a, double b, double c) { x = a; y = b; z = c; }; };
point3_d line_s(line[0], line[1], 1);
point3_d line_e(line[2], line[3], 1);
point3_d line_parameters = cross(line_s, line_e); // line_parameters[3] mean {a, b, c} of line equation(ax + by + c = 0).
return std::abs((line_parameters.x * p.x + line_parameters.y * p.y + line_parameters.z)) * 1.0 / std::sqrt(double(line_parameters.x * line_parameters.x + line_parameters.y * line_parameters.y));
}
https://stackoverflow.com/questions/12132352/distance-from-a-point-to-a-line-segment
Distance from a point to a line/segment
I have to compute the distance from a point to a line (check if it is line or a line segment). I am not sure that the bool function IsSegment is working properly. Can i have some suggestions? Thank...
stackoverflow.com