Ooops
October 28, 2020, 05:14:00 AM

Author Topic: [bb] Point distance to a Line by Danny [ 1+ years ago ]  (Read 1405 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Point distance to a Line by Danny [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : Point distance to a Line
Author : Danny
Posted : 1+ years ago

Description : Given 2 3d points A and B (that make up for a line-segment) and a point P; this function will return the shortest distance between that point P and the line.
I use this for my vertex lighting system where I have a 'linear light' (a light defined by 2 3d coordinates) to check if a vertex (point P) is 'within range' of that light, and it's distance determines it's fall-off.

You could also use this to determin if the player is 'too close' to a laser beam for example (or some other linear-object) like: IF 'player-distance-to-line' <= 'heath-range-of-beam' THEN 'add damage to player' :)

Danny


Code :
Code: BlitzBasic
  1. Function PointDistanceToLine#( ax#,ay#,az#, bx#,by#,bz#, px#,py#,pz# )
  2. ;| Calculates the shortest distance between a point P(xyz) and a line segment defined by A(xyz) and B(xyz) - danny.
  3.  
  4.         ;get the length of each side of the triangle ABP
  5.         ab# = Sqr( (bx-ax)*(bx-ax) + (by-ay)*(by-ay) + (bz-az)*(bz-az) )
  6.         bp# = Sqr( (px-bx)*(px-bx) + (py-by)*(py-by) + (pz-bz)*(pz-bz) )
  7.         pa# = Sqr( (ax-px)*(ax-px) + (ay-py)*(ay-py) + (az-pz)*(az-pz) )
  8.  
  9.         ;get the triangle's semiperimeter
  10.         semi# = (ab+bp+pa) / 2.0
  11.        
  12.         ;get the triangle's area
  13.         area# = Sqr( semi * (semi-ab) * (semi-bp) * (semi-pa) )
  14.        
  15.         ;return closest distance P to AB
  16.         Return (2.0 * (area/ab))
  17.        
  18. End Function


Comments :


Craig H. Nisbet(Posted 1+ years ago)

 That works great!  Thanks for posting!


puki(Posted 1+ years ago)

 <div class="quote"> I use this for my vertex lighting system  </div>Clear off "Nisbet" - this is clearly my code."Danny" - old buddy/pal.You forgot to post the vertex lighting system - no need to worry - just email it to me - I'll check it over.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal