1351 views

# Do the math?

1. 6 months ago

### Dave S

Dec 1 San Diego, California USA

I have 4 coodinates (x,y) that mark the 4 corners of a rectangle (not necessarly a square). If I click a point need the "lines" the intersect those points, I need to know which of the coordinate pairs the click was closest to. Did I click nearest the TOP edge? LEFT, BOTTOM or RIGHT ..... or was I outside of a predetermined tolerance?

The idea is to select an edge, without have to hit the "line" exactly.. just gotta be reasonable close

2. ### Greg O

Dec 1 Xojo Inc
Edited 6 months ago

Assuming the box edges are vertical/horizontal, You can use abs to help you here.

```if abs(top-y) < tolerance then
// clicked top edge
End if```
3. ### Dave S

Dec 1 San Diego, California USA

actually headed that direction.... In reality it is more than a "square" it is actually a "grid"
so I am going to take my click Point (P) and find the Horizontal line and vertical line the is closest (abs(delta)
once I have those two line... the sign of each delta will tell me what side of the line the point is on

just need to decide what do to if the distance from top and left are equal... :)

4. ### Tim H

Dec 1 Pre-Release Testers Portland, OR USA

Calculate the distance using the good old Pythagorean formula a^2 + b^2 = c^2.
c is the distance.
a is abs(x1 - x2), b is abs(y1 - y2).

5. ### Dave S

Dec 2 San Diego, California USA

@Tim H Calculate the distance using the good old Pythagorean formula a^2 + b^2 = c^2.
c is the distance.
a is abs(x1 - x2), b is abs(y1 - y2).

No need to be that expensive. What Greg mentioned (with some expansion) works just fine... Since the lines are horizontal and vertical, and I'm not looking for the distance to a specific point

6. ### Tim H

Dec 2 Pre-Release Testers Portland, OR USA

Cool. Your mention of a grid made me think of "closest point on the grid". As in snap to grid.

7. ### Dave S

Dec 2 San Diego, California USA

Nah.... when the user clicks on the grid... In needed to know which horizontal or vertical line (and what square) was clicked...
so for example ... Cell(5,3) Top Side

8. ### Tomas J

Dec 3 Pre-Release Testers, Xojo Pro Europe (Germany)

Wouldn't it be faster using matrices?

9. ### Dave S

Dec 3 San Diego, California USA

not sure how you figure how Matrix Math is easier or faster than a simple comparison of DeltaX and DeltaY

but thanks.... I have it all worked out... its fast, its accurate and does exactly what needs to be done.

Gregs origninal statement pointed me in the direction that I couldn't put my finger on originally