Advertisement
Ham62

PointAngles.bas

Mar 26th, 2020
1,598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "fbgfx.bi"
  2.  
  3. type coord
  4.     x as integer
  5.     y as integer
  6. end type
  7.  
  8. function dotProduct(u as coord, v as coord) as double
  9.     return (u.x * v.x) + (u.y * v.y)
  10. end function
  11.  
  12. function norm(u as coord) as double
  13.     return sqr((u.x*u.x) + (u.y*u.y))
  14. end function
  15.  
  16. dim as coord a = type(260, 50)
  17. dim as coord b = type(160, 100)
  18.  
  19. screenres 640, 480, 8
  20.  
  21. dim as coord c = type(1, 0) ' C is the unit vector pointing left
  22.  
  23. ' Make b into a vector where the origin is at 'a', not (0, 0)
  24. b.x = a.x-b.x
  25. b.y = a.y-b.y
  26.  
  27. ' Find the angle between b and c
  28. dim as double angle = acos(dotProduct(c, b)/(norm(c)*norm(b)))
  29.  
  30. const PI = atn(1)/45
  31. #define RadToAng( _R ) (_R)/PI
  32. do
  33.     if multikey(fb.SC_LEFT) then
  34.         b.x -= 1
  35.     end if
  36.     if multikey(fb.SC_RIGHT) then
  37.         b.x += 1
  38.     end if
  39.     if multikey(fb.SC_UP) then
  40.         b.y -= 1
  41.     end if
  42.     if multikey(fb.SC_DOWN) then
  43.         b.y += 1
  44.     end if
  45.  
  46.     screenlock
  47.    
  48.     cls    
  49.     angle = acos(dotProduct(c, b)/(norm(c)*norm(b)))
  50.     locate(0, 0)
  51.     print "a = (";a.x;",";a.y;")"
  52.     print "b = (";b.x;",";b.y;")"
  53.     print "c = (";c.x;",";c.y;")"
  54.     print "angle =";RadToAng(angle);" degrees"
  55.    
  56.     ' Line from origin pointing to other point
  57.     if b.y > 0 then
  58.         line(a.x, a.y)-step(10*cos(angle), 10*sin(angle)), &H8
  59.     else
  60.         line(a.x, a.y)-step(10*cos(angle), -10*sin(angle)), &H8
  61.     end if
  62.    
  63.     pset(a.x, a.y), &HF         ' Origin (where we rotate from)
  64.     pset(a.x+b.x, a.y+b.y), &HE ' Point we're pointing to
  65.  
  66.     screenunlock
  67.    
  68.     sleep 10,1
  69. loop until multikey(1)
  70.  
  71.  
  72. sleep
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement