GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Download
  • News
  • Community
  • Resources
  • Language Reference
  • SmallBASIC Manual

This view

  • Original page
  • New address

GeezerVison is showing smallbasic.github.io — SmallBASIC | PTDISTSEG

Edit History

PTDISTSEG

d = PTDISTSEG (Bx, By, Cx, Cy, Ax, Ay)

Home > Reference > Math

Distance of point A from line segment B-C. Point A is given by the coordinates (Ax, Ay), B by (Bx, By) and C by (Cx, Cy).

Example 1

' Define line segment
Bx = 100
By = 100
Cx = 200
Cy = 300

' Define point
Ax = 150
Ay = 50

Distance = PTDISTSEG (Bx,By,Cx,Cy,Ax,Ay) 
print "Distant between line and point is "; Distance  ;" pixel"

Example 2: Interactive example to illustrate the geometry of PTDISTSEG

' Interactive example to illustrate the geometry of PTDISTSEG

Bx = 200
By = 300
Ax = 300
Ay = 100

pen on
while(!pen(12))

  cls
  print "Move Mouse. Press Left Mouse Button to exit"
  
  Cx = pen(10)
  Cy = pen(11)
  
  Distance = PTDISTSEG (Bx,By,Cx,Cy,Ax,Ay) 
  print "Distant between line and point is "; Distance  ;" pixel"
  
  Lx = Cx - Bx
  Ly = Cy - By
  
  if(Lx != 0 AND Ly != 0) then
    ' Calculate projection vector V using dot-product
    Px = Ax - Bx
    Py = Ay - By
    Vx = Bx + (Lx * Px + Ly * Py) / (Lx^2 + Ly^2) * Lx
    Vy = By + (Lx * Px + Ly * Py) / (Lx^2 + Ly^2) * Ly

    ' Test if V is on the line segment
    t = (Vx-Bx)/Lx
    if(t < 0) then
      Vx = Bx
      Vy = By
    elseif(t > 1) then
      Vx = Cx
      Vy = Cy
    endif

  endif

  ' Draw
  line Bx, By, Cx, Cy color 10   ' Line
  line Ax, Ay, Vx, Vy color 2    ' Distant between line and point
  circle Ax, Ay, 5 color 11      ' Cirlce at position of the point
  showpage
  delay(50)
  
wend
Math
ABS   ABSMAX   ABSMIN   ACOS   ACOSH   ACOT   ACOTH   ACSC   ACSCH   ASEC   ASECH   ASIN   ASINH   ATAN   ATAN2   ATANH   ATN   CEIL   COS   COSH   COT   COTH   CSC   CSCH   DEG   DERIV   DETERM   DIFFEQN   EXP   EXPRSEQ   FIX   FLOOR   FRAC   INT   INTERSECT   INVERSE   LINEQN   LOG   LOG10   M3APPLY   M3IDENT   M3ROTATE   M3SCALE   M3TRANS   MAX   MIN   POLYAREA   POLYCENT   POLYEXT   POW   PTDISTLN   PTDISTSEG   PTSIGN   RAD   RND   ROOT   ROUND   SEC   SECH   SEGCOS   SEGLEN   SEGSIN   SEQ   SGN   SIN   SINH   SQR   STATMEAN   STATMEANDEV   STATMEDIAN   STATSPREADP   STATSPREADS   STATSTD   SUM   SUMSQ   TAN   TANH   TRANSPOSE  
This page was last edited on Tue, 20 Jun 2023 15:47:13 +0200 | Markdown processed with pandoc 3.1.12.1
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.