# Finding Closest Point RSS

## 4 replies

Last post Apr 03, 2004 05:34 PM by Jigar

Member

330 Points

66 Posts

### Finding Closest Point

Mar 24, 2004 08:22 PM|LINK

I have a list of x,y points in a DB, and I want a user to be able to click anywhere on an image button, and get back a value of the closest point. Anything built into GDI+ that may help me out with this? Thanks guys. -glog-

All-Star

18705 Points

3733 Posts

### Re: Finding Closest Point

Apr 02, 2004 08:23 AM|LINK

I don't think GDI is what you should be using here. sutely it's just a SQL issue? if you click an input type="image" /> you get the x, y passed to the next page - plug these coords into a SQL statement with some math and you can get the rows back in order of proximity. I'll see if I can work up a SQL trick for this. can't recall off the top of my head how it's done.
RTFM - straight talk for web developers. Unmoderated, uncensored, occasionally unreadable

Jason Brown - MVP, IIS

Contributor

4629 Points

935 Posts

### How to find distance.

Apr 02, 2004 01:21 PM|LINK

This function will find Distance between two points..
```public double Distance(System.Drawing.Point p1,System.Drawing.Point p2){
double dx = p1.x - p2.x;
double dy = p1.y - p2.y;
return Math.Sqrt((dx * dx) + (dy * dy));
}

And this is how its works.

* p2
/ |
/   |
/    | dy
/      |
/        |
/          |
*---------#
p1   dx

distance = SquareRoot((dx x dx) + (dy x dy))```
Jigar Desai
-----------------------
Do not forget to "Mark as Answer" on the post that helped you.

All-Star

18705 Points

3733 Posts

### Re: How to find distance.

Apr 03, 2004 12:41 PM|LINK

cheers for filling that in, but I'm still sceptical that this is a GDI issue, myself. note the 'DB' reference.
RTFM - straight talk for web developers. Unmoderated, uncensored, occasionally unreadable

Jason Brown - MVP, IIS

Contributor

4629 Points

935 Posts

### Find Closest point using DB

Apr 03, 2004 04:51 PM|LINK

Yes you can do same in DB check out this sample SQL Script. run into SQLServer Query Anlyser. I have used TempTable just to show how it works.
```Declare @Px int
Declare @Py int

Set @Px = 1
Set @Py = 1

CREATE TABLE #TemporaryTable (ItemID int ,X int,Y int)
INSERT INTO #TemporaryTable Values(1,-2,2)
INSERT INTO #TemporaryTable Values(2,2,2)
INSERT INTO #TemporaryTable Values(3,2,-2)
INSERT INTO #TemporaryTable Values(4,-2,-2)

Select * , SQRT((@Px - X) * (@Px - X) + (@Py - Y) * (@Py - Y)) AS Distance
From #TemporaryTable
DROP TABLE #TemporaryTable```
Out put Is...
```ItemID X      Y      Distance
------ ------ ------ ------------------
1        -2      2      3.1622776601683795
2         2      2      1.4142135623730951
3         2      -2     3.1622776601683795
4        -2     -2     4.2426406871192848
```
You can create StoredProcedure using this logic to return closest point .
Jigar Desai
-----------------------
Do not forget to "Mark as Answer" on the post that helped you.