Which points of a set of points are closest to me/ any fixed point?
(or: Who is closest for trying to catch in the schoolyard?)
To make it easier for the kids among you the following is also attached as stack.
Code: Select all
-- script button "closest points", for explaining the approach
on mouseUp
lock screen; lock messages
put the points of grc "setOfPoints" into p0 -- any number of lines of points
put the loc of grc "redOval" into p1 -- the point of interest
put item 1 of p1 into x0; put item 2 of p1 into y0
-- now compute the distances with the help of a sum of absolute values
repeat for each line p in p0
if p is empty then next repeat
put cr & abs(x0 - item 1 of p) + abs(y0 - item 2 of p) &","& p after ds
end repeat
delete char 1 of ds
sort ds numeric by item 1 of each
put ds into fld "distances"
put item 2 to 3 of line 1 of ds into p2
set layer of grc "redOval" to top
set layer of grc "connectingLine" to top
show grc "connectingLine"
set points of grc "connectingLine" to p1 & cr & p2
unlock screen; unlock messages
end mouseUp
-- next two are the script of grc "redOval"
on mouseDown
hide grc "connectingLine"
grab me
end mouseDown
on mouseUp
send "mouseUp" to btn "closest points"
end mouseUp
Imagine a certain product, say a TV.
On the vertical line you hace the prices, from low at bottom up to high at top.
In horizontal direction you have quality, from low at left to high at right.
Now set your prefered point (redOval) determining a price and a quality level.
Then compute the closest product point to your prefered point.
Having this it is easy to have more dimensions, say you split your "quality" criterium into three different kinds.
Then your points have four dimensions and you set your preference point again to a point (x1,x2,x3,x4) and compute the distances to the other points as above, simply using adding absolute values of four differences instead of two.
Very simple. Isn't it? Difficult is only to visualize four dimensions, but we don't need this for our decision.
Edit. Changed picture and stack, text of *describing field* in that was wrong, sorry (I made this originally for euclidian distance), scripts are unchanged.