Jump to content

circle move towards mouse - GDIplus


Recommended Posts

Hi

I have a problem in making  circle2 moving toward the mouse, and is adjacent to circle1

this example , but this not move toward  mouse

#include <GDIplus.au3>
#include <GUIConstantsEx.au3>

AutoItSetOption("MouseCoordMode", 0)
AutoItSetOption("GUIOnEventMode", 1)

Global Const $iW = 400, $iH = 400, $iBgColor = 0xF0F0F0
Global Const $hGUI = GUICreate("", $iW, $iH)
GUISetBkColor($iBgColor, $hGUI)
GUISetState()

_GDIPlus_Startup()
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
$hPen = _GDIPlus_PenCreate()
$hPen2 = _GDIPlus_PenCreate(0xFFFF0000, 2)
$hBitmap = _GDIPlus_BitmapCreateFromScan0($iW, $iH)
$hCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsSetSmoothingMode($hCtxt, 4)
_GDIPlus_GraphicsSetPixelOffsetMode($hCtxt, 4)

Global Const $iPosX = 50, $iPosY = 50, $iWidth = 300, $iHeight = 300
Global $iMPosX, $iMPosY
Local Const $fDeg = ACos(-1) / 180, $iRadius = 30.6, $iRadius2 = $iWidth / 2 , $iWh1 = $iW / 2, $iHh1 = $iH / 2
Local $fAngle = 0 , $fAngle2 = 155 , $fAngle3 =333, $i = 0

GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

Do
    _GDIPlus_GraphicsClear($hCtxt, 0xFF000000 + $iBgColor)
    _GDIPlus_GraphicsDrawRect($hCtxt, $iPosX, $iPosY, $iWidth, $iHeight, $hPen)
    $hPos = GUIGetCursorInfo($hGUI)

    ; circle1
    _GDIPlus_GraphicsDrawEllipse($hCtxt , $iWh1 - 15, $iHh1 - 15, 30 , 30 , $hPen)

    ; circle2
    $X1 = $iWh1 - 15 + Cos($fAngle * $fDeg) * $iRadius
    $X2 = $iHh1 - 15 + Sin($fAngle * $fDeg) * $iRadius
    _GDIPlus_GraphicsDrawEllipse($hCtxt , $X1 , $X2 , 30 , 30 , $hPen)

    $fAngle += 1
    if $fAngle = 360 Then
        $fAngle = 0
    EndIf

    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, $iW, $iH)
Until Not Sleep(10)

Func _Exit()
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_GraphicsDispose($hCtxt)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_PenDispose($hPen2)
    _GDIPlus_Shutdown()
    GUIDelete()
    Exit
EndFunc

 

Edited by Alex1986
Link to comment
Share on other sites

Like this?

$hPos = GUIGetCursorInfo($hGUI)
    If (Not @Error) Then
        If ($hPos[0] > $iWidth + $iPosX - 15) Then $hPos[0] = $iWidth + $iPosX - 15
        If ($hPos[0] < $iPosX + 15) Then $hPos[0] = $iPosX + 15
        If ($hPos[1] > $iHeight + $iPosY - 15) Then $hPos[1] = $iHeight + $iPosY - 15
        If ($hPos[1] < $iPosY + 15) Then $hPos[1] = $iPosY + 15
        $iWh1 = $hPos[0]
        $iHh1 = $hPos[1]
    EndIf

 

Link to comment
Share on other sites

2 hours ago, InunoTaishou said:

Like this?

$hPos = GUIGetCursorInfo($hGUI)
    If (Not @Error) Then
        If ($hPos[0] > $iWidth + $iPosX - 15) Then $hPos[0] = $iWidth + $iPosX - 15
        If ($hPos[0] < $iPosX + 15) Then $hPos[0] = $iPosX + 15
        If ($hPos[1] > $iHeight + $iPosY - 15) Then $hPos[1] = $iHeight + $iPosY - 15
        If ($hPos[1] < $iPosY + 15) Then $hPos[1] = $iPosY + 15
        $iWh1 = $hPos[0]
        $iHh1 = $hPos[1]
    EndIf

 

not like that , thank you
* I need to be moving circle2 which is adjacent to Circle1 and  heading towards mouse cursor

Gr2Rqt9.jpg

Link to comment
Share on other sites

Try this:

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>

AutoItSetOption("MouseCoordMode", 2)
AutoItSetOption("GUIOnEventMode", 1)

Global Const $iW = 400, $iH = 400, $iBgColor = 0xF0F0F0
Global Const $hGUI = GUICreate("", $iW, $iH)
GUISetBkColor($iBgColor, $hGUI)
GUISetState()

_GDIPlus_Startup()
Global Const $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
Global Const $hPen1 = _GDIPlus_PenCreate(0xFF0000FF)
Global Const $hPen2 = _GDIPlus_PenCreate(0xFFFF0000)
Global Const $hBitmap = _GDIPlus_BitmapCreateFromScan0($iW, $iH)
Global Const $hCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsSetSmoothingMode($hCtxt, 5)
_GDIPlus_GraphicsSetPixelOffsetMode($hCtxt, 4)

Global Const $iPosX = 50, $iPosY = 50, $iWidth = 300, $iHeight = 300
Global Const $fDeg = ACos(-1) / 180, $fRad = 180 / ACos(-1), $iWh = $iW / 2, $iHh = $iH / 2
Global $fDiameter1 = 30, $fRadius1 = $fDiameter1 / 2, _ ;circle 1
       $fDiameter2 = 135, $fRadius2 = $fDiameter2 / 2, _ ;circle 2
       $mpos, $mwx, $mwy, $pd

GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

Do
    _GDIPlus_GraphicsClear($hCtxt, 0xFF000000 + $iBgColor)
    _GDIPlus_GraphicsDrawRect($hCtxt, $iPosX, $iPosY, $iWidth, $iHeight, $hPen1)

    ; circle1
    _GDIPlus_GraphicsDrawEllipse($hCtxt , $iWh - $fRadius1, $iHh - $fRadius1, $fDiameter1 , $fDiameter1 , $hPen1)

    ; circle2
    $mpos = MouseGetPos() ;get the mouse position
    $mwx = $mpos[0] - $iWh ;get the x coordinate
    $mwy = $iHh - $mpos[1] ;get the y coordinate
    $pd = Sqrt($iWh * $iHh + $mpos[0] * $mpos[1]) ;calculate the distance between mouse position and center
    $fAngle = 2 * -ATan($mwy / ($mwx + Sqrt($mwx * $mwx + $mwy * $mwy))) * $fRad ; calculate the angle between center and mouse position -> trigonometry ;-)

    $fDistance = $fRadius2 + $fRadius1 ;calculate the distance between both circles so that they always touch each other

    ;calculate x/y position of 2nd circle with a little adjustment
    $X1 = $iWh - $fRadius2 + Cos($fAngle * $fDeg) * ($fDistance + 0.5)
    $Y1 = $iHh - $fRadius2 + Sin($fAngle * $fDeg) * ($fDistance + 0.5)

    _GDIPlus_GraphicsDrawEllipse($hCtxt , $X1 + $fRadius2, $Y1 + $fRadius2, 1, 1, $hPen2) ;center of circle 2

    _GDIPlus_GraphicsDrawEllipse($hCtxt , $X1 , $Y1 , $fDiameter2 , $fDiameter2 , $hPen2) ;circle 2

    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, $iW, $iH)
Until Not Sleep(30)

Func _Exit()
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_GraphicsDispose($hCtxt)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_PenDispose($hPen1)
    _GDIPlus_PenDispose($hPen2)
    _GDIPlus_Shutdown()
    GUIDelete()
    Exit
EndFunc

 

Edited by UEZ
added some comments and shortened code

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

4 hours ago, Alex1986 said:

thank you UEZ , this is Great

Q : How to get the center of the circle2 , It moves with the mouse cursor ?

 

 

Check out the code from above. I added some comments. ;)

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...