線分と点の距離

type
TGridLine = Record

private
FPositionS: TPoint;
FPositionE: TPoint;

public
procedure Assign(aGridLine: TGridLine);

property PointS: TPoint read FPositionS write FPositionS;
property PointE: TPoint read FPositionE write FPositionE;
end;

function TSessionScratchBoard.Distance(aPoint: TPointF; aGridLine: TGridLine): Single;
var
rx,ry,dx,dy,qx,qy,L2,W:double;
x0, y0, x1, y1: Single;
px, py: Single;
tmpPoint0, tmpPoint1: TPointF;
begin
tmpPoint0 := PositionToPoint(aGridLine.PointS.X, aGridLine.PointS.Y);
tmpPoint1 := PositionToPoint(aGridLine.PointE.X, aGridLine.PointE.Y);

px := aPoint.X;
py := aPoint.Y;
x0 := tmpPoint0.X;
y0 := tmpPoint0.Y;
x1 := tmpPoint1.X;
y1 := tmpPoint1.Y;

dx := x1 – x0;
dy := y1 – y0;
L2 := dx * dx + dy * dy;
rx := px – x0;
ry := py – y0;
W := rx * dx + ry * dy;
if L2 < W then
begin
rx := x1 - px;
ry := y1 - py;
W := rx * dx + ry * dy;
end;
if W < 0 then
result := sqrt(rx * rx + ry * ry)
else
result := abs(rx * dy - ry * dx)/sqrt(L2);
end;

コメントを残す

メールアドレスが公開されることはありません。


+ 四 = 8

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>