# Detect dot on line.

## Recommended Posts

Having some massive brain failure here muttley

I think that an image says more than a thousand words:

I need to detect if the line is crossing the blue dot, I only know the points at the green circles.

Ideas?

Broken link? PM me and I'll send you the file!

##### Share on other sites

as in detect if the line goes through the center of the dot, or if the line crosses any part of the dot?

##### Share on other sites

as in detect if the line goes through the center of the dot, or if the line crosses any part of the dot?

Cross it.

And to make my first post clearer, I of course know the rectangle that bounds the circle.

Broken link? PM me and I'll send you the file!

##### Share on other sites

as long as the width of the rectangle is the same as the dot's diameter and that you know the rectangle's center, you can test if the circle is within r of the line's projected path, with r being the dot's radius.

Perhaps you can give an example of the function that you wish to make?

y=mx+c

##### Share on other sites

I suggest analytics geometry. You should find the line equation and you can verify if dot coordinates is on your line.

When the words fail... music speaks

##### Share on other sites

y=mx+c

Yeah, that's what I'm trying but I really can't get my head around it. Damn computers to have this weird coordinate system.

Broken link? PM me and I'll send you the file!

##### Share on other sites

Is this theoretical? I mean do you have two sets of coordinates for the end points and coordinates for the circle?

Or is this literally a drawing?

##### Share on other sites

Is this theoretical? I mean do you have two sets of coordinates for the end points and coordinates for the circle?

Or is this literally a drawing?

It's part of a little GDI+ thingy I'm making.

The coordinates could be for example:

The coordinates for the line could be: (300,300) (20,408) and the dot could be (10,10) and has a width of 10.

Remember that this is screen coordinates and the dots coordinates is the upper left corner of it's rectangle.

Broken link? PM me and I'll send you the file!

##### Share on other sites

Yeah, that's what I'm trying but I really can't get my head around it. Damn computers to have this weird coordinate system.

First find the slope of the line between the 2 points

http://www.purplemath.com/modules/slope.htm

(Rise over Run)

This fraction is the M in Y=MX

Then plug in the (X,Y) values for the point to test. If Y = M*X then it is on the line.

Edited by Paulie

##### Share on other sites

You could do this using a sort of trick. Since we're working with a circle you could cheat, assuming you know the radius.

1. Determine the equation of the line using x1,y1 and x2,y2.

2. Find the shortest distance from the point to the line:

http://www.worsleyschool.net/science/files...nt/method5.html

3. If distance is less than radius, we have an intersect.

##### Share on other sites

I'm trying with Paulies solution, almost succeeded with it muttley

Thanks for the support guys

Broken link? PM me and I'll send you the file!

##### Share on other sites

I loaded your image up in Photoshop to grab the line start / end coordinates, circle coordinates / radius. This will allow for you to define the radius of the circle and the line doesn't have to pass through absolute middle to count as intersect.

Start point: 44,44

End point: 641,521

Circle center: 294,243

You can verify the distance formula using:

Start point: -1,0

End point: 1,0

Circle center: 0,5

Distance = 5

```\$result = PointDistanceFromLine(44,44,641,521,294,243)
ConsoleWrite(\$result & @CRLF)
MsgBox(0,"","Circle intersects line")
Else
MsgBox(0,"","Circle doesn't intersect line")
EndIf

\$result = PointDistanceFromLine(44,44,641,521,322,298)
ConsoleWrite(\$result & @CRLF)
MsgBox(0,"","Circle intersects line")
Else
MsgBox(0,"","Circle doesn't intersect line")
EndIf

;-------------------------
;FUNCTIONS
;-------------------------

;distance: Center point distance from line
;MsgBox(0,"","Circle intersects line")
Return True
Else
;MsgBox(0,"","Circle doesn't intersect line")
Return False
EndIf
EndFunc

;xa,ya: Start X,Y
;xb,yb: End X,Y
;xp,yp: Point X,Y
Func PointDistanceFromLine(\$xa,\$ya,\$xb,\$yb,\$xp,\$yp)
;Xa,Ya is point 1 on the line segment.
;Xb,Yb is point 2 on the line segment.
;Xp,Yp is the point.

\$xu = \$xp - \$xa
\$yu = \$yp - \$ya
\$xv = \$xb - \$xa
\$yv = \$yb - \$ya
If (\$xu * \$xv + \$yu * \$yv < 0) Then
Return Sqrt( (\$Xp-\$Xa)^2 + (\$Yp-\$Ya)^2)
endif

\$xu = \$xp - \$xb
\$yu = \$yp - \$yb
\$xv = -\$xv
\$yv = -\$yv
If (\$xu * \$xv + \$yu * \$yv < 0) Then
Return Sqrt( (\$Xp-\$Xb)^2 + (\$Yp-\$Yb)^2 )
endif

Return Abs( ( \$Xp * ( \$Ya - \$Yb ) + \$Yp * ( \$Xb - \$Xa ) + ( \$Xa * \$Yb - \$Xb * \$Ya ) ) / Sqrt( ( \$Xb - \$Xa )^2 + ( \$Yb - \$Ya )^2 ) )
EndFunc```
Edited by weaponx

## Create an account

Register a new account

×

• Wiki

• Back

• Git