Advertisement
name22

Interpolation between 2 2D Points

May 2nd, 2013
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 3.02 KB | None | 0 0
  1. #include <WindowsConstants.au3>
  2. #include <GUIConstants.au3>
  3. #include <GDIPlus.au3>
  4. #include <WinAPI.au3>
  5.  
  6. ; -Author: name22 (www.autoit.de)
  7.  
  8. Opt("GUIOnEventMode", 1)
  9.  
  10. Global $vA[2], $vB[2], $vDir[2], $nLen, $nSpeed, $nPos = 0
  11.  
  12. $nSpeed = 3 ;Der blaue Punkt wandert pro Schleifendurchgang 3 Pixel entlang der Linie
  13.  
  14. ;Punkte festlegen:
  15. $vA[0] = 10 ;X-Koordinate von A
  16. $vA[1] = 20 ;Y-Koordinate von A
  17.  
  18. $vB[0] = 256 ;X-Koordinate von A
  19. $vB[1] = 386 ;Y-Koordinate von A
  20.  
  21. ;Richtungsvektor bestimmen (du musst nur den Startpunkt vom Zielpunkt abziehen, Koordinate für Koordinate):
  22. $vDir[0] = $vB[0] - $vA[0]
  23. $vDir[1] = $vB[1] - $vA[1]
  24.  
  25. ;Richtungsvektor normalisieren (auf eine Länge von 1 reduzieren, so dass er seine Richtung beibehält):
  26. ;[dazu wird jede Koordinate durch die Gesamtlänge des Vektors (Pythagoras) geteilt]
  27. $nLen = Sqrt($vDir[0] ^ 2 + $vDir[1] ^ 2) ;Länge des Vektors (Strecke von A nach B)
  28. $vDir[0] = $vDir[0] / $nLen
  29. $vDir[1] = $vDir[1] / $nLen
  30.  
  31. $hGUI = GUICreate("Example by name22", 400, 400)
  32. GUISetState()
  33.  
  34. $hDC_Window = _WinAPI_GetDC($hGUI)
  35. $hDC_Bitmap = _WinAPI_CreateCompatibleDC($hDC_Window)
  36. $hBitmap = _WinAPI_CreateCompatibleBitmap($hDC_Window, 400, 400)
  37. _WinAPI_SelectObject($hDC_Bitmap, $hBitmap)
  38.  
  39. _GDIPlus_Startup()
  40.  
  41. $hGraphics = _GDIPlus_GraphicsCreateFromHDC($hDC_Bitmap)
  42. _GDIPlus_GraphicsSetSmoothingMode($hGraphics, 2)
  43.  
  44. $hBrush_Red = _GDIPlus_BrushCreateSolid(0xFFFF0000)
  45. $hBrush_Green = _GDIPlus_BrushCreateSolid(0xFF00FF00)
  46. $hBrush_Blue = _GDIPlus_BrushCreateSolid(0xFF0000FF)
  47.  
  48. $hPen = _GDIPlus_PenCreate(0xFF000000, 3)
  49.  
  50. OnAutoItExitRegister("_Shutdown")
  51. GUISetOnEvent($GUI_EVENT_CLOSE, "_Close", $hGUI)
  52.  
  53. While Sleep(20)
  54.     ;Nur eine kleine Spielerei um verschiedene Werte für nPos zu demonstrieren:
  55.     $nPos += $nSpeed
  56.     If $nPos >= $nLen Then
  57.         $nPos = $nLen
  58.         $nSpeed *= -1
  59.     ElseIf $nPos <= 0 Then
  60.         $nPos = 0
  61.         $nSpeed *= -1
  62.     EndIf
  63.  
  64.     $nX_Pos = $vA[0] + $vDir[0] * $nPos ;X-Koordinate des Punktes auf der Linie AB ausrechnen
  65.     $nY_Pos = $vA[1] + $vDir[1] * $nPos ;Y-Koordinate des Punktes auf der Linie AB ausrechnen
  66.     ;($nPos ist die Distanz von A in Pixeln, für nPos="0" ist die Postion="A", für nPos="Volle Länge von AB" ist die Position="B")
  67.  
  68.     _GDIPlus_GraphicsClear($hGraphics, 0xFFFFFFFF)
  69.     _GDIPlus_GraphicsDrawLine($hGraphics, $vA[0], $vA[1], $vB[0], $vB[1], $hPen)
  70.     _GDIPlus_GraphicsFillEllipse($hGraphics, $vA[0] - 5, $vA[1] - 5, 10, 10, $hBrush_Green)
  71.     _GDIPlus_GraphicsFillEllipse($hGraphics, $vB[0] - 5, $vB[1] - 5, 10, 10, $hBrush_Red)
  72.     _GDIPlus_GraphicsFillEllipse($hGraphics, $nX_Pos - 8, $nY_Pos - 8, 16, 16, $hBrush_Blue)
  73.     _WinAPI_BitBlt($hDC_Window, 0, 0, 400, 400, $hDC_Bitmap, 0, 0, $SRCCOPY)
  74. WEnd
  75.  
  76. Func _Close()
  77.     Exit
  78. EndFunc
  79.  
  80. Func _Shutdown()
  81.     _WinAPI_ReleaseDC($hGUI, $hDC_Window)
  82.     _WinAPI_DeleteDC($hDC_Bitmap)
  83.     _WinAPI_DeleteObject($hBitmap)
  84.  
  85.     _GDIPlus_GraphicsDispose($hGraphics)
  86.     _GDIPlus_BrushDispose($hBrush_Red)
  87.     _GDIPlus_BrushDispose($hBrush_Green)
  88.     _GDIPlus_BrushDispose($hBrush_Blue)
  89.     _GDIPlus_PenDispose($hPen)
  90.     _GDIPlus_Shutdown()
  91. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement