October 28, 2020, 10:55:32 PM

Author Topic: [bb] Pointing one image towards another using ATan by Klapster [ 1+ years ago ]  (Read 1247 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Pointing one image towards another using ATan
Author : Klapster
Posted : 1+ years ago

Description : This code demonstrates how ATan is used to determine the frame of a rotating image

Code :
Code: BlitzBasic
  1. ;Example Code and Function to find the angle from a point to another point
  2. ;+ Make an image point towards the second point
  3. ;By Tom Klapiscak -- 2002
  4. ;Feel free to use this code, wherever, whenever, and however...
  5.  
  6. Graphics 800,600,16,1
  7. Px=400
  8. Py=300
  9. IMG=Create_Line()      
  10. SetBuffer BackBuffer()
  11.  
  12. While KeyHit(1)=0
  13.         Mx=MouseX()
  14.         My=MouseY()
  15.         Color 0,255,0
  16.         Rect Mx-1,My-1,3,3,1   
  17.         Color 255,0,0
  18.         Rect Px-1,Py-1,3,3,1   
  19.         I=Get_Iteration(Mx,My,Px,Py,72)
  20.         If I=>0 And I<72 Then It=I     
  21.         DrawImage IMG,Px,Py,It
  22.         Flip
  23.         Cls
  24. Wend
  25. End
  26.  
  27.  
  28. ;Iterations is just how many images you have per
  29. ;360 degrees(ie how smooth the rotation is)
  30. Function Get_Iteration(X1,Y1,X2,Y2,Iterations)
  31.         Steps=(360/Iterations)
  32.         Dx#=X2-X1
  33.         Dy#=Y2-Y1      
  34.         Theta#=ATan#(Dy/Dx)
  35.         If Dx<0 And Dy=>0
  36.                 Theta#=Theta#+180
  37.         EndIf
  38.         If Dx<0 And Dy<0
  39.                 Theta#=Theta#+180
  40.         EndIf          
  41.         If Dx=>0 And Dy<0
  42.                 Theta#=Theta#+360
  43.         EndIf
  44.         Return Int(Theta/Steps)
  45. End Function
  46.  
  47. Function Create_Line()
  48.         IMG=CreateImage(200,200,73)
  49.         HandleImage IMG,100,100
  50.         For Count=0 To 360 Step 5
  51.                 SetBuffer ImageBuffer(IMG,(360-Count)/5)
  52.                 Line 100,100,Sin(Count-90)*100+100,Cos(Count-90)*100+100
  53.         Next           
  54.         Return IMG
  55. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal