Distance from point to box

From: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Distance from point to box
Date: 2014-07-30 11:42:15
Message-ID: CAPpHfduML3BPFA5wib=gakmGJzzai0FTXLakJHny8prFW+jpoA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hackers,

while reading code written by my GSoC student for knn-spgist I faced many
questions about calculation distance from point to box.

1) dist_pb calls close_pb which calls on_pb, dist_ps_internal, close_ps and
so on. So, it's very complex way to calculate very simple value. I see this
way has some mathematical beauty, but is it acceptable in practice?

2) computeDistance in knn-gist uses it's own quite simplified
implementation of this calculation. But why has it own implementation
instead on simplifying dist_pb?

3) computeDistance implementation looks still very complex for me. Right
way (simple and fast) to calculate point to box distance seems for me to be
so:

double dx = 0.0, dy = 0.0;

if (point->x < box->low.x)
dx = box->low.x - point->x;
if (point->x > box->high.x)
dx = point->x - box->high.x;
if (point->y < box->low.y)
dy = box->low.y - point->y;
if (point->y > box->high.y)
dy = point->y - box->high.y;
return HYPOT(dx, dy);

I feel myself quite tangled.
Could anybody clarify it for me? Did I miss something? Thanks.

------
With best regards,
Alexander Korotkov.

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Fabien COELHO 2014-07-30 12:06:02 Re: Distance from point to box
Previous Message Pavel Stehule 2014-07-30 08:30:21 Re: Optimization for updating foreign tables in Postgres FDW