点线段的距离函数

点线段的距离函数

#include <iostream>
#include <cmath>

struct POINT
{
	double x;
	double y;
};
typedef POINT VECTOR;

struct SEGMENT
{
	POINT * pStart;
	POINT * pEnd;
};

class Segment
{
public:
	POINT * pStart;
	POINT * pEnd;
	Segment() : pStart(NULL), pEnd(NULL) {}
	Segment(POINT * p1, POINT * p2) : pStart(p1), pEnd(p2) {}
	double GetLength() const;
	double GetDist(const POINT * point) const;
	double GetDist(const POINT & point) const;
};
double Segment::GetLength() const
{
	if(pStart == NULL || pEnd == NULL)
		return 0.0;
	return sqrt(pow(pStart->x - pEnd->x, 2) + pow(pStart->y - pEnd->y,2));
}
double Segment::GetDist(const POINT * point) const
{
	VECTOR sp = {point->x - pStart->x, point->y - pStart->y};
	VECTOR se = {pEnd->x - pStart->x, pEnd->y - pStart->y};
	VECTOR es = {-se.x, -se.y};
	VECTOR ep = {point->x - pEnd->x, point->y - pEnd->y};
	if(sp.x * se.x + sp.y * se.y <= 0)
		return sqrt(pow(pStart->x - point->x,2) + pow(pStart->y - point->y,2));
	else if(ep.x * es.x + ep.y * es.y <= 0)
		return sqrt(pow(pEnd->x - point->x, 2) + pow(pEnd->y - point->y, 2));
	else 
	{
		double abs_sp = sqrt(sp.x * sp.x + sp.y * sp.y);
		double abs_se = sqrt(se.x * se.x + se.y * se.y);
		double cos = (sp.x * es.x + sp.y * es.y)/(abs_sp * abs_se);
		return abs_sp * sqrt(1 - cos * cos);
	}
}
double Segment::GetDist(const POINT & point) const
{
	return GetDist(&point);
}

int main()
{
	POINT p1 = {1.0, 2.0};
	POINT p2 = {2.0, 2.0};
	POINT p3 = {1.5, 4};
	Segment pn = Segment(&p1, &p2);
	std::cout<<pn.GetDist(p3)<<std::endl;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个 MATLAB 函数,用于计算一个线段的最近距离: ```matlab function [distance, closest_point] = point_to_segment_distance(point, segment_start, segment_end) % POINT_TO_SEGMENT_DISTANCE Calculates the distance between a point and a line segment % [distance, closest_point] = POINT_TO_SEGMENT_DISTANCE(point, segment_start, segment_end) % calculates the distance between a point and a line segment defined by two points, % as well as the closest point on the segment to the given point. % point: a 1x2 array representing the (x,y) coordinates of the point % segment_start: a 1x2 array representing the (x,y) coordinates of the start of the line segment % segment_end: a 1x2 array representing the (x,y) coordinates of the end of the line segment % distance: the distance between the point and the line segment % closest_point: a 1x2 array representing the (x,y) coordinates of the closest point on the line segment % Calculate the direction of the line segment segment_vector = segment_end - segment_start; % Calculate the vector from the start of the line segment to the point point_vector = point - segment_start; % Calculate the projection of the point vector onto the line segment vector projection = dot(point_vector, segment_vector) / norm(segment_vector)^2; % If the projection is outside the line segment, calculate the distance to the closest endpoint if projection < 0 closest_point = segment_start; elseif projection > 1 closest_point = segment_end; else % Calculate the closest point on the line segment closest_point = segment_start + projection * segment_vector; end % Calculate the distance between the point and the closest point on the line segment distance = norm(point - closest_point); end ``` 使用示例: ```matlab % Define the point and line segment point = [2, 3]; segment_start = [1, 1]; segment_end = [4, 5]; % Calculate the distance between the point and the line segment [distance, closest_point] = point_to_segment_distance(point, segment_start, segment_end); % Display the results disp(['Distance: ', num2str(distance)]); disp(['Closest point: (', num2str(closest_point(1)), ', ', num2str(closest_point(2)), ')']); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值