php - SQL: search of nearest in 2d square and circle -
i have table: points
it has 2 filds: x, y
each row represents dot on 2d field
i want capable of performing search of dots in radius r
and square side
btw: use php access db.
my main point nearest center of figure point first result in quqe
how such thing in sql?
mathematically point in circle statisfies equation
(c.x-p.x)^2 + (c.y-p.y)^2 <= r^2
where c cetner of circle, p - point, r - radius
for square
max(abs(c.x-p.x),abs(c.y-p.y)) <= a/2
where c cetner of square, p - point, a - side of square
you can write theese equations in language.
left side of equations called distance various measures. finding nearest point should order resultset distance asceniding , take first result.
something this:
select top 1 p.x, p.x points p otrder ((@x - p.x)*(@x - p.x)+(@y - p.y)*(@y - p.y))
Comments
Post a Comment