Jump to content

Detect dot on line.


Recommended Posts

Having some massive brain failure here muttley

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

Posted Image

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!

Link to comment
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!

Link to comment
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?

Link to comment
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!

Link to comment
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
Link to comment
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.

Link to comment
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)
$Radius = 11
If CircleIntersection($result, $radius) Then
    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)
$Radius = 53
If CircleIntersection($result, $radius) Then
    MsgBox(0,"","Circle intersects line")
Else
    MsgBox(0,"","Circle doesn't intersect line")
EndIf

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

;distance: Center point distance from line
;radius: circle radius
Func CircleIntersection($distance, $radius)
    If $result < $radius Then
        ;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
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...