Jump to content
Sign in to follow this  
jebus495

A fun problem for you to solve

Recommended Posts

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?

I have honestly missed how this relates to what I want to do.

Where do I put the cursor position?

What other values do I need to get and where do they go?

Share this post


Link to post
Share on other sites

Well, all that math junk relates to how to calculate the coordinates of a point on the edge of a window which lies on the same line as the center point that you want and the current mouse position. In a nutshell, what you illustrated in your first post.

What I provided are the calculations and the comparisons you'd need to programmatically solve your problem.

EDIT: I forgot to mention earlier that you need to retranslate your translated coordinates from relative to actual screen coordinates after the calculations.

Edited by omikron48

Share this post


Link to post
Share on other sites

The math-idiots method...

#Include <Misc.au3>
Opt("MouseCoordMode", 2)

$GUI = GUICreate("",401,401)
$center = 201
GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    If $msg = -3 then ExitLoop
    If _IsPressed(2) Then
        $mpos = MouseGetPos()
        $mpos[0] -= $center
        $mpos[1] -= $center
        If Abs($mpos[0]) > Abs($mpos[1]) Then
            $xoffset = 1
            $yoffset = Abs($mpos[1]) / Abs($mpos[0])
        Else
            $xoffset = Abs($mpos[0]) / Abs($mpos[1])
            $yoffset = 1
        EndIf
        If $mpos[0] < 0 Then $xoffset *= -1
        If $mpos[1] < 0 Then $yoffset *= -1
        $x = 0 ; $x = $mpos[0]
        $y = 0 ; $y = $mpos[1]
        While Abs($x) < $center And Abs($y) < $center
            MouseMove($center + $x, $center + $y, 1)
            ToolTip("h = " & $x & @CRLF & "v = " & $y)
            $x += $xoffset
            $y += $yoffset
        WEnd
    EndIf
WEnd
Exit

Right-click the mouse...

Edit: Oops, got my V and H crossed in the last-minute addition of a tooltip statement.

For a square or rectangular window and minus the flashy loop:

#Include <Misc.au3>
Opt("MouseCoordMode", 2)

$GUI = GUICreate("",601,401)
$xcenter = 301
$ycenter = 201
GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    If $msg = -3 then ExitLoop
    If _IsPressed(2) Then
        $mpos = MouseGetPos()
        $mpos[0] -= $xcenter
        $mpos[1] -= $ycenter
        If (Abs($mpos[0]) / $xcenter) > (Abs($mpos[1]) / $ycenter) Then
            $xoffset = $xcenter - 1
            $yoffset = (Abs($mpos[1]) / Abs($mpos[0])) * $xcenter
        Else
            $xoffset = (Abs($mpos[0]) / Abs($mpos[1])) * $ycenter
            $yoffset = $ycenter - 1
        EndIf
        If $mpos[0] < 0 Then $xoffset *= -1
        If $mpos[1] < 0 Then $yoffset *= -1
        MouseMove($xcenter + $xoffset, $ycenter + $yoffset, 10)
        ToolTip("h = " & $xoffset & @CRLF & "v = " & $yoffset)
    EndIf
WEnd
Exit

Edit: bug fix

Edited by Spiff59

Share this post


Link to post
Share on other sites

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

I think I was confusing myself a bit last night. I haven't tested your solution, but I'll trust you. However the solution doesn't tell me which border is intersected. So if you first punch in the x coordinate to determine y, and the return coordinates put the mouse outside the window, then the guess was wrong. So you then need to use the y coordinate to determine x instead. Nice job omikron48. Oops, I see you mentioned that already. lol :idea: Edited by czardas

Share this post


Link to post
Share on other sites

Ok I had some time to work on this and had some fun with it. I reduced the formulas where I could and it is probably to complicated to read and understand but it works. The program runs on whatever window is currently active. You must have the mouse over the window for it to work, and you press F2 to run the function to move the mouse. Press Esc to exit.

#include <Math.au3>
HotKeySet("{F2}","Go")
HotKeySet("{ESC}","Terminate")
Func Terminate()
    Exit
EndFunc

While True
WEnd

Func Go()
    ; Get active window position
    $WinSize = WinGetPos(WinGetTitle("[active]"))

    ; Calculate Center and mouse positions
    $Xcenter = $WinSize[0] + $WinSize[2] / 2
    $Ycenter = $WinSize[1] + $WinSize[3] / 2
    $Xmouse = MouseGetPos(0)
    $Ymouse = MouseGetPos(1)

    ; Checks if the mouse is over the window
    If $Xmouse > $WinSize[0] + $WinSize[2] Or $Xmouse < $WinSize[0] Or $Ymouse < $WinSize[1] Or $Ymouse > $WinSize[1] + $WinSize[3] Then Return

    ; More Simple calculations
    $rise = $Ymouse - $Ycenter
    $run = $Xmouse - $Xcenter
    If $run = 0 Then
        $ResultX = $Xcenter
        $ResultY = $WinSize[1] + ($rise > 0)*$WinSize[3]
    Else
        $slope = $rise / $run
        Select
            Case $run > 0
                $ResultX = _Min($WinSize[3] / ((($rise < 0) * -4) + 2) / $slope + $Xcenter, $WinSize[0] + $WinSize[2])
            Case Else
                $ResultX = _Max($WinSize[3] / ((($rise < 0) * -4) + 2) / $slope + $Xcenter, $WinSize[0])
        EndSelect
        Select
            Case $rise = 0
                $ResultY = $Ycenter
            Case $rise > 0
                $ResultY =  _Min($slope * $WinSize[2] / ((($run < 0) * -4) + 2) + $Ycenter, $WinSize[1] + $WinSize[3])
            Case Else
                $ResultY =  _Max($slope * $WinSize[2] / ((($run < 0) * -4) + 2) + $Ycenter, $WinSize[1])
        EndSelect
    EndIf
    MouseMove($ResultX, $ResultY, 5)
EndFunc

If you want a different version that draws the lines from the center to the mouse, and mouse to edge on screen, then use this version. It makes it easier to make sure the calculation is correct.

#include <GuiConstantsEx.au3>
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Math.au3>
HotKeySet("{F2}","Go")
HotKeySet("{ESC}","Terminate")
Func Terminate()
    Exit
EndFunc

Global $hWin=""

While True
WEnd

Func Go()
    If $hWin <> "" Then GUIDelete($hWin)
    ; Get active window position
    $WinSize = WinGetPos(WinGetTitle("[active]"))

    ; Calculate Center and mouse positions
    $Xcenter = $WinSize[0] + $WinSize[2] / 2
    $Ycenter = $WinSize[1] + $WinSize[3] / 2
    $Xmouse = MouseGetPos(0)
    $Ymouse = MouseGetPos(1)

    ; Checks if the mouse is over the window
    If $Xmouse > $WinSize[0] + $WinSize[2] Or $Xmouse < $WinSize[0] Or $Ymouse < $WinSize[1] Or $Ymouse > $WinSize[1] + $WinSize[3] Then Return

    $hWin = GUICreate("", $WinSize[2], $WinSize[3], $WinSize[0], $WinSize[1], $WS_POPUP, BitOR($WS_EX_TOPMOST, $WS_EX_TOOLWINDOW, $WS_EX_LAYERED))
    GUISetBkColor(0xABCDEF)
    _WinAPI_SetLayeredWindowAttributes($hWin, 0xABCDEF, 255)
    GUISetState()
    _GDIPlus_Startup ()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hWin)
    $hPen = _GDIPlus_PenCreate (0xFFFF0000,3,2)
    $hPen2 = _GDIPlus_PenCreate (0xFF0000FF,3,2)
    _GDIPlus_GraphicsDrawLine ($hGraphic, $Xcenter - $WinSize[0], $Ycenter - $WinSize[1], $Xmouse - $WinSize[0], $Ymouse - $WinSize[1], $hPen)

    ; More Simple calculations
    $rise = $Ymouse - $Ycenter
    $run = $Xmouse - $Xcenter
    If $run = 0 Then
        $ResultX = $Xcenter
        $ResultY = $WinSize[1] + ($rise > 0)*$WinSize[3]
    Else
        $slope = $rise / $run
        Select
            Case $run > 0
                $ResultX = _Min($WinSize[3] / ((($rise < 0) * -4) + 2) / $slope + $Xcenter, $WinSize[0] + $WinSize[2])
            Case Else
                $ResultX = _Max($WinSize[3] / ((($rise < 0) * -4) + 2) / $slope + $Xcenter, $WinSize[0])
        EndSelect
        Select
            Case $rise = 0
                $ResultY = $Ycenter
            Case $rise > 0
                $ResultY =  _Min($slope * $WinSize[2] / ((($run < 0) * -4) + 2) + $Ycenter, $WinSize[1] + $WinSize[3])
            Case Else
                $ResultY =  _Max($slope * $WinSize[2] / ((($run < 0) * -4) + 2) + $Ycenter, $WinSize[1])
        EndSelect
    EndIf
    _GDIPlus_GraphicsDrawLine ($hGraphic, $Xmouse - $WinSize[0], $Ymouse - $WinSize[1], $ResultX - $WinSize[0], $ResultY - $WinSize[1], $hPen2)
    _GDIPlus_Shutdown ()
    MouseMove($ResultX, $ResultY, 5)
EndFunc

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...