Jump to content
Sign in to follow this  
jebus495

A fun problem for you to solve

Recommended Posts

jebus495

I would like to write a script that:

Moves the mouse to the outside edge of a window relative to the position of the mouse.

For example if my mouse was 100 pixels above the center of the window I would want the mouse to move up to the top of the window.

Here is a screenshot to better illustrate what I mean:

Posted Image

Seems like it would take some math (possibly trig?) which I'm pretty good at generally. This situation has be lost on even where to begin though. Thought it might seem like a fun thing to do.

Share this post


Link to post
Share on other sites
PsaltyDS

So, what have you written so far? What effort have you made? Did you check the help file for MouseGetPos() and MouseMove()?

This is a help forum, not a script writing service.

:idea:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
ShawnW

Working on it, at least for another 30min at work. What do you want to happen when the cursor is over the exact center of the window, and therefore there is no direction to calculate?

Share this post


Link to post
Share on other sites
jebus495

So, what have you written so far? What effort have you made? Did you check the help file for MouseGetPos() and MouseMove()?

This is a help forum, not a script writing service.

:idea:

As requested:

#Include <Misc.au3>


While 1
$winpos = WinGetPos("Untitled - Notepad")

If _IsPressed(11) Then
    $mospos = MouseGetPos()
    MouseMove($winpos[0]+($winpos[2]/2), $winpos[1]+($winpos[3]/2), 100)
    Sleep(500)
    MouseMove($mospos[0], $mospos[1], 0)
EndIf
WEnd

This is not really a working example. What this does is move your mouse from its current position to the center of the notepad screen and then puts it back.

Yes I know all about MouseGetPos() and MouseMove() but my confusion begins with calculating my final desired mouse position.

There really isn't much room to make an attempt for this particular problem. Only a few lines and I can't for the life of me figure out how I would need to work that out.

Working on it, at least for another 30min at work. What do you want to happen when the cursor is over the exact center of the window, and therefore there is no direction to calculate?

It would be fine if it did nothing in that case. Edited by jebus495

Share this post


Link to post
Share on other sites
ShawnW

Actually I'll email myself what I got and take this home from work, I'll have a solution once I'm home if one isn't posted already.

Share this post


Link to post
Share on other sites
czardas

You have to calculate the ratio of the mouse position right (or left) of centre to the height above (or below) the centre. Then you have to see which border it comes into contact with first. I would take a guess (for example the right border). Then if the mouse ends up outside the window: the guess was wrong and the line must intersect the top border. No trig involved. There are probably other ways to do it too.

Edited by czardas

Share this post


Link to post
Share on other sites
nitekram

Are you trying te reinvent the wheel? LINK

edit - fixed link

Edited by nitekram

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Share this post


Link to post
Share on other sites
darkjohn20

Sounds like fun with vectors haha...

Share this post


Link to post
Share on other sites
bogQ

He is trying to calculate the next x, y, position under the identical angle of circle with diffrent radius size

And he need formula to adapt it to script

So math forum is the right place for him ( Pre-Calculus section probably) and this isnt fun problem to solve, its 'make my brain explode' to solve :idea:

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites
jebus495

He is trying to calculate the next x, y, position under the identical angle of circle with diffrent radius size

And he need formula to adapt it to script

So math forum is the right place for him ( Pre-Calculus section probably) and this isnt fun problem to solve, its 'make my brain explode' to solve :idea:

Thank you for wording it that way! It gave me an idea!

Ironically the center point of the window I want to use is 404. xD

Edited by jebus495

Share this post


Link to post
Share on other sites
ShawnW

Well I came home to some personal issues I need to deal with.

Here is what I had so far. The math for calculating where to move doesn't quite work yet. I was fiddling with it. Right now it properly determines what quadrant the direction line needs to go, and I think properly determines weather it hits the vertical or horizontal side first, but calculating where the mouse should be on that side is buggy and I made a mistake somewhere. Only 1 quadrant is coded with space for the others in the select, but they should be pretty cookie cutter once the 1st is working.

It uses the Autoit Help window as its test. Hit f2 over the window to have it move the mouse, but as I said the only quadrant coded so far is lower left, and even that does not yet work. If no one has a solution when I get to work tomorrow I'll finish it up.

HotKeySet("{F2}","Go")
HotKeySet("{ESC}","Terminate")
Func Terminate()
    Exit
EndFunc
$WinSize = WinGetPos("AutoIt Help")
$left = $WinSize[0]
$top = $WinSize[1]
$width = $WinSize[2]
$height = $WinSize[3]
$right = $left + $width
$bottom = $top + $height

While True
WEnd

Func Go()
    ; Calculate Center and mouse positions
    $Xcenter = $left + $width / 2
    $Ycenter = $top + $height / 2
    $Xmouse = MouseGetPos(0)
    $Ymouse = MouseGetPos(1)
    ; Check if mouse in area
    If $Ymouse < $top Or  $Ymouse > $bottom Or $Xmouse < $left Or $Xmouse > $right Then Return

    ; More Simple calculations
    $rise = $Ymouse - $Ycenter
    $run = $Xmouse - $Xcenter
    $Slope = $rise / $run

    ; GENERAL NOTE: Since we make the center of the window (0,0) then the line we want to move along is Y = slope * X, this is used later.

    ; Determine quadrant line is directed in.
    Dim $quad[4]
    $quad[0] = True
    $quad[1] = True
    $quad[2] = True
    $quad[3] = True
    If $rise > 0 Then
        $quad[2] = False
        $quad[3] = False
    ElseIf $rise < 0 Then
        $quad[0] = False
        $quad[1] = False
    EndIf
    If $run > 0 Then
        $quad[1] = False
        $quad[2] = False
    ElseIf $run < 0 Then
        $quad[0] = False
        $quad[3] = False
    EndIf

    ; Note that since the screen coords start (0,0) at the top left, the qudrants will be different than you may remember from math class.
    ; III | IV       $quad[2] | $quad[3]
    ; ----+----  or  ---------+---------
    ;  II | I        $quad[1] | $quad[0]

    ; Move mouse
    Select
        Case $quad[0] And $quad[1] And $quad[2] And $quad[3]
            ; Cursor is in exact center of window. For now do nothing.
        Case $quad[0] And $quad[1]
            ; Cursor is above x-axis of center but along the y-axis
            MouseMove($Xcenter, $top)
        Case $quad[2] And $quad[3]
            ; Cursor is below x-axis of center but along the y-axis
            MouseMove($Xcenter, $bottom)
        Case $quad[1] And $quad[2]
            ; Cursor is left of the y-axis of center but along the x-axis
            MouseMove($left, $Ycenter)
        Case $quad[0] And $quad[3]
            ; Cursor is right of the y-axis of center but along the x-axis
            MouseMove($right, $Ycenter)
        Case $quad[0]
            If $Slope * $right > $bottom Then
                ; The line reaches the the bottom before the right of the window
                MouseMove($bottom / $Slope, $bottom)
            Else
                ; The line reaches the the right before the bottom of the window
                MouseMove($right, $Slope * $right)
            EndIf
        Case $quad[1]

        Case $quad[2]

        Case $quad[3]

    EndSelect
EndFunc

Share this post


Link to post
Share on other sites
omikron48

This is the equation for a line using two points:

where (x0,y0) being the origin, (x1,y1) being a point somewhere in the plane

y = (y1 - y0) * (x - x0) / (x1 - x0) + y0
x = (y - y0) * (x1 - x0) / (y1 - y0) + x0

Given that equation, all you need to do is plug in the values of your points. To solve for your third point, you just plug in the known x-value or y-value of the border where the point will fall on, then just get the other coordinate.

Checking which quadrant your second point will lie is a simple check of the sign and value of (x1 - x0) and (y1 - y0). After knowing which quadrant it lies, you just plug in the x-value or y-value of your two borders for that quadrant then check which of the two yield a value within their limits, that will give you the correct coordinates of your third point.

EDIT: You also need to put in a check for cases where the line produced is horizontal or vertical, since that can lead to a divide by zero error.

EDIT2: Corrected slope for equations.

Edited by omikron48

Share this post


Link to post
Share on other sites
darkjohn20

I like how he used notepad to show this drawing? :idea:

Share this post


Link to post
Share on other sites
omikron48

I should because my thesis involved point and line calculations in some part I can't quite remember.

I'm not 100% sure I typed the equation correctly, and since I currently don't have access to my thesis code at home, I can't get the working code for the calculation.

The equation is just your basic point and slope formula for a line, a.k.a. y = m(x - x0) + y0, with the slope derived using two known points (deltaY / deltaX).

Damn, I got it wrong. Got the Ys and Xs reversed. Gonna have to correct that...

EDIT: Now I recalled what it was, it was for slope and point calculations for equation values for interpolation methods.

Edited by omikron48

Share this post


Link to post
Share on other sites
czardas

Adding to the complication, is the fact that screen coordinates will not be the same as the vector coordinates. So that will also need some translation.

Share this post


Link to post
Share on other sites
omikron48

Not really, a little adjustment in the mental picture, but the equations themselves should be fine.

As I said before, they are equations using two points. There is nothing stopping you from using an arbitrary point as your origin. To get your translated points, you just subtract your origin from it before you plug it into the equation.

where (x0, y0) is the "new" origin, (x1, y1) is a point on the plane

translatedX = x1 - x0
translatedY = y1 - y0

Things, aren't too complicated yet since we're only dealing with lines and two dimensions. I wouldn't bother subjecting myself to brain cramps if the situation involved complex polynomials or parabolas and such...

Share this post


Link to post
Share on other sites
jebus495

Not really, a little adjustment in the mental picture, but the equations themselves should be fine.

As I said before, they are equations using two points. There is nothing stopping you from using an arbitrary point as your origin. To get your translated points, you just subtract your origin from it before you plug it into the equation.

where (x0, y0) is the "new" origin, (x1, y1) is a point on the plane

translatedX = x1 - x0
translatedY = y1 - y0

Things, aren't too complicated yet since we're only dealing with lines and two dimensions. I wouldn't bother subjecting myself to brain cramps if the situation involved complex polynomials or parabolas and such...

This isn't fun anymore. :idea:

Share this post


Link to post
Share on other sites
omikron48

Well, it depends. I had fun dealing with it, but that's just me. I'm fascinated by math, up to a point where it's still clear to me or I have access to someone who can explain it to me.

Thing is, your problem is very math related; geometry related to be precise. The way to solve it would be to use math in code, which is why the basic foundation for programmers is math and logic. Everything else, is just a matter of aesthetics and practicality (or efficiency to be specific).

Share this post


Link to post
Share on other sites
jebus495

Well, it depends. I had fun dealing with it, but that's just me. I'm fascinated by math, up to a point where it's still clear to me or I have access to someone who can explain it to me.

Thing is, your problem is very math related; geometry related to be precise. The way to solve it would be to use math in code, which is why the basic foundation for programmers is math and logic. Everything else, is just a matter of aesthetics and practicality (or efficiency to be specific).

So how do I even use this formula? lol. Got me a bit confused. What numbers do I actually need to put where? O.o

Share this post


Link to post
Share on other sites
omikron48

The basic explanation is all there on my two posts.

So, the question would be: which bits do you need clarification on? OR which parts can't you follow?

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.