Jump to content

Applied Maths


Recommended Posts

I'm trying to get a label move to my mouse when i click in a -smooth- and orderly fashion. I also want it to not move more then 300 pixels at a time.

I have commented the code a lot so please look at this in SciTe. (Where the most comment is, is where the problems are.)

Opt("GUIOnEventMode", 1)

#include <GUIConstants.au3>

Global $AppTitle = StringTrimRight(@ScriptName,4), $Item[3]

$GUI = GUICreate($AppTitle, 800,640,-1,-1,$WS_POPUP)
GUISetBkColor(0)
GUISetOnEvent(-3, "Stop")

;Create the moving part
$Item[1] = 0;X
$Item[2] = 0;Y
$Item[0] = GUICtrlCreateLabel("Me", $Item[1], $Item[2], 20,20); ID naturallement
GUICtrlSetBkColor(-1,0xFFFFFF)

GUISetState()

While 1
    $Mouse = GUIGetCursorInfo()
    
    If $Mouse[2] Then
        #cs
        $Mouse[0];x mouse
        $Mouse[1];y mouse
        $Item[1];x moving part
        $Item[2];y moving part
        #ce
        
    ;Pythagoras makes a visit to check if there has not been traveling more then 200 pixels
        If Sqrt(($Mouse[0] - $Item[1])^2 + ($Mouse[1] - $Item[2])^2) > 200 Then
        ;Shorter down the movement
            
        ;( I think this is where it goes wrong )
            
            While Sqrt(($Mouse[0] - $Item[1])^2 + ($Mouse[1] - $Item[2])^2) > 200
            ; Thank AMD for fast PC's
                $Mouse[0] *= 0.9
                $Mouse[1] *= 0.9
            WEnd
        Else
        ;Just go on !
        EndIf
        
    ;Number of steps is only so high because it will look smooth
        $Steps = 20
    ; This is to stop the "Are we there yet" if-statement
        $CurrentSteps = 0
        
    ;Ofcourse we have to calculate where we're going in the form of 'step size' (and direction)
        $StepSizeX = (($Mouse[0] - $Item[1]) / $Steps)
        $StepSizeY = (($Mouse[1] - $Item[2]) / $Steps)
        
        While $CurrentSteps <> $Steps
        ;Visualize.
            $Item[1] += $StepSizeX
            $Item[2] += $StepSizeY
            GUICtrlSetPos($Item[0], $Item[1], $Item[2])
            Sleep(10)
            $CurrentSteps += 1
        WEnd
    EndIf
WEnd

Func Stop()
    Exit
EndFunc
Link to comment
Share on other sites

Your right about the problem area. You compare algorithm > 200 but decrease the values on each itteration. So you will never get out of the loop.

;Pythagoras makes a visit to check if there has not been traveling more then 200 pixels
        If Sqrt(($Mouse[0] - $Item[1])^2 + ($Mouse[1] - $Item[2])^2) > 200 Then
        ;Shorter down the movement
            
        ;( I think this is where it goes wrong )
            
            ;CHANGE: While Sqrt(($Mouse[0] - $Item[1])^2 + ($Mouse[1] - $Item[2])^2) > 200
            While Sqrt(($Mouse[0] - $Item[1])^2 + ($Mouse[1] - $Item[2])^2) < 200
            ; Thank AMD for fast PC's
                $Mouse[0] *= 0.9
                $Mouse[1] *= 0.9
            WEnd
        Else
        ;Just go on !
        EndIf
Edited by Uten
Link to comment
Share on other sites

[Edit] I first thought it made sence what you said. But just look at this:

If it's bigger then 50 Do:

While it's smaller then 50

make value lower and lower

end

EndIf

If you would fill in a value of 40, it skips the while and it's ok. If you get a value bigger then 50, say 60, it goes towards the while loop but it never runs it even once because the statement is false.. ??

Edited by Manadar
Link to comment
Share on other sites

[Edit] I first thought it made sence what you said. But just look at this:

If it's bigger then 50 Do:

While it's smaller then 50

make value lower and lower

end

EndIf

If you would fill in a value of 40, it skips the while and it's ok. If you get a value bigger then 50, say 60, it goes towards the while loop but it never runs it even once because the statement is false.. ??

hmm, looks like your right. Not my best day. Should have tested more, but then a gain that's not my job it's yours :D. Good catch though!

The loop should have been something like this:

Local $foo
            Do
            ; Thank AMD for fast PC's
                $Mouse[0] *= 0.9
                $Mouse[1] *= 0.9
        $foo = Round(Sqrt(($Mouse[0] - $Item[1])^2 + ($Mouse[1] - $Item[2])^2),0) + 1
        ConsoleWrite("Calculate: $foo:=" & $foo & ", $Mouse[0]:=" & $Mouse[0] & ", $Mouse[1]:=" & $Mouse[1] & @LF)
        if Round($Mouse[0],0) = 0 OR Round( $Mouse[1],0) = 0 then ExitLoop ;Escaping 
            Until  $foo < 200

As you can see the algorithm still is not perfect in the sense that it will fail if your label is fare down on the GUI and you do your click in the upper part of the GUI.

Maybe you could post the solution when you get it?

Link to comment
Share on other sites

  • Moderators

I have commented the code a lot so please look at this in SciTe. (Where the most comment is, is where the problems are.)

I don't know if you know this, but now you can use syntax coloring on the forum. Instead of [ code][ /code] you would use [ autoit][ /autoit] (without spaces of course) or if you are in full reply/post mode you could use the button that big_daddy came up with [A3] next to the code tags :D ... Just a FYI is all.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I don't know if you know this, but now you can use syntax coloring on the forum. Instead of [ code][ /code] you would use [ autoit][ /autoit] (without spaces of course) or if you are in full reply/post mode you could use the button that big_daddy came up with [A3] next to the code tags :D ... Just a FYI is all.

Wasn't there a problem when you tried to copy everything in that box into SciTe? I didn't use it for that reason.
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...