Results 1 to 12 of 12

Thread: How do i make this NOT start from 0,0 but my current position?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    245

    How do i make this NOT start from 0,0 but my current position?

    Private Sub Button7_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    Dim mp As Point = MousePosition
    myX = mp.X
    myY = mp.Y
    Dim cursx As Integer = 700
    Dim cursy As Integer = 700
    Dim drawx As Integer = mp.X
    Dim drawy As Integer = mp.Y
    Dim i As Integer = 0
    Do
    If i <= cursx Then
    drawx = i
    End If
    If i <= cursy Then
    drawy = i
    End If
    System.Threading.Thread.Sleep(4)
    Cursor.Position = New Point(drawx, drawy)
    i += 1
    Loop Until drawx = cursx And drawy = cursy
    End Sub
    oook so this when button7 clicked it will move my mouse to the X:700 and Y:700, but will start from the X:0 and Y:0 but i tried making start from my current mouse position but idk how not working:/ Someone help me rlly quick plz?:/
    Last edited by therehere3; Mar 30th, 2012 at 12:47 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How do i make this NOT start from 0,0 but my current position?

    As I told you in your other thread:
    the Cursor.Position property allows you to determine or specify where the cursor is located
    You're setting that property towards the end. Now you just need to get it towards the beginning.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    245

    Re: How do i make this NOT start from 0,0 but my current position?

    I am using the MousePosition, is that not good to use so i should rather use Cursor.Position instead? And i have it at the beginning.

    Dim mp As Point = MousePosition
    myX = mp.X
    myY = mp.Y

    Dim cursx As Integer = 700
    Dim cursy As Integer = 700
    Dim drawx As Integer = mp.X
    Dim drawy As Integer = mp.Y
    Dim i As Integer = 0
    Do
    If i <= cursx Then
    drawx = i
    End If
    If i <= cursy Then
    drawy = i
    End If
    System.Threading.Thread.Sleep(4)
    Cursor.Position = New Point(drawx, drawy)
    i += 1
    Loop Until drawx = cursx And drawy = cursy

    Im just pretty sure my usage isnt right on the mp.X and mp.Y

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    245

    Re: How do i make this NOT start from 0,0 but my current position?

    I did even use Cursor.Position. It must be my usage:

    Dim mp As Point = Cursor.Position
    myX = mp.X
    myY = mp.Y
    Dim cursx As Integer = 700
    Dim cursy As Integer = 700
    Dim drawx As Integer = mp.X
    Dim drawy As Integer = mp.Y
    Dim i As Integer = 0
    Do
    If i <= cursx Then
    drawx = i
    End If
    If i <= cursy Then
    drawy = i
    End If
    System.Threading.Thread.Sleep(4)
    Cursor.Position = New Point(drawx, drawy)
    i += 1
    Loop Until drawx = cursx And drawy = cursy

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How do i make this NOT start from 0,0 but my current position?

    Hmmm... the doco says that MousePosition is identical to Cursor.Position so that should be right. You might try using [code] tags when you're posting code instead of [quote] tags, which are for posting quotes. Without indenting it is much harder to read code and therefore it is much harder to identify where the issue(s) might be. Let's take a closer look at your code:
    Code:
    Dim mp As Point = MousePosition
    myX = mp.X
    '...
    Dim cursx As Integer = 700
    '...
    Dim drawx As Integer = mp.X
    '...
    Dim i As Integer = 0
    
    Do
        If i <= cursx Then
            drawx = i
    So, you get the actual X position of the cursor and assign it to drawx and then you pretty much immediately assign the value of i, which is zero, to drawx and therefore discard the value you retrieved. If you don't want to start at zero then don't start at zero. What purpose does i serve in that code? If you want to start at the current cursor position and end at (700,700) then why do you have i at all? Surely you just want to start at the initial drawx and drawy and increment from there in the appropriate direction.

    This is a classic example of not starting with an algorithm. You're flailing around writing code in the dark because you don't actually know what the code is supposed to do. You have a general overview but you don't have the specifics. You should know EXACTLY what the code is supposed to do before you write it.

    As further proof of that, you are incrementing the X and Y component by 1 each time. What if the cursor needs to move 100 in one direction and 500 in the other? You're going to move at 45 degrees for 100 pixels in each direction and then at 90 degrees for the rest. You should be using trigonometry to calculate a straight line between the initial and final cursor positions and moving along that line. You'll obvious have to round the values along the way but that is what you really want isn't it, for the cursor to move in essentially a straight line from one point to the other?

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    245

    Re: How do i make this NOT start from 0,0 but my current position?

    Ok so i changed it from 0 but i think it will bug your mouse if you mouse is above the coordinates you tell it to move, do you know how i could fix that?:/

    This what i have so far:

    vb Code:
    1. Dim mp As Point = MousePosition
    2.         myX = mp.X
    3.         myY = mp.Y
    4.         Dim cursx As Integer = RR(188, 239)
    5.         Dim cursy As Integer = RR(231, 269)
    6.         Dim drawx As Integer = mp.X
    7.         Dim drawy As Integer = mp.Y
    8.         Dim i As Point = Cursor.Position
    9.         Do
    10.             If i.X <= cursx Then
    11.                 drawx = i.X
    12.             End If
    13.             If i.Y <= cursy Then
    14.                 drawy = i.Y
    15.             End If
    16.             System.Threading.Thread.Sleep(4) ' sleep thread so cursor looks smooth.
    17.             Cursor.Position = New Point(drawx, drawy)
    18.             i.X += 1 : i.Y += 1
    19.         Loop Until drawx = cursx And drawy = cursy

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How do i make this NOT start from 0,0 but my current position?

    That i variable serves no useful purpose at all. It needs to be got rid of altogether. Can you tell us what your algorithm is?

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    245

    Re: How do i make this NOT start from 0,0 but my current position?

    What is a algorithm? And i tried this, (takin out i altogether) and still nothing:

    vb Code:
    1. Private Sub Button7_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    2.         Dim cursx As Integer = RR(188, 239)
    3.         Dim cursy As Integer = RR(231, 269)
    4.         Dim drawx As Integer = Cursor.Position.X
    5.         Dim drawy As Integer = Cursor.Position.Y
    6.         Do
    7.             If Cursor.Position.X <= cursx Then
    8.                 drawx = Cursor.Position.X
    9.             End If
    10.             If Cursor.Position.Y <= cursy Then
    11.                 drawy = Cursor.Position.Y
    12.             End If
    13.             System.Threading.Thread.Sleep(4) ' sleep thread so cursor looks smooth.
    14.             Cursor.Position = New Point(drawx + 1, drawy + 1)
    15.         Loop Until drawx = cursx And drawy = cursy
    16.     End Sub
    Last edited by therehere3; Mar 30th, 2012 at 05:52 PM.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    245

    Re: How do i make this NOT start from 0,0 but my current position?

    OK soo played with it some more and got it to move to the position i wanted but then just stays there and will not let me move my mouse ever again... Sooo i dnt understand:/

    Code im using:

    vb Code:
    1. Dim cursx As Integer = 507
    2.         Dim cursy As Integer = 594
    3.         Dim drawx As Integer = Cursor.Position.X
    4.         Dim drawy As Integer = Cursor.Position.Y
    5.         Do
    6.             If Cursor.Position.X <= cursx Then
    7.                 drawx = Cursor.Position.X + 1
    8.             End If
    9.             If Cursor.Position.Y <= cursy Then
    10.                 drawy = Cursor.Position.Y + 1
    11.             End If
    12.             System.Threading.Thread.Sleep(4) ' sleep thread so cursor looks smooth.
    13.             Cursor.Position = New Point(drawx, drawy)
    14.         Loop Until drawx = cursx And drawy = cursy

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How do i make this NOT start from 0,0 but my current position?

    Quote Originally Posted by therehere3 View Post
    What is a algorithm?
    I'm not sure whether you're a native English speaker or not but it concerns me that anyone who wants to program, and especially if you want to be a programmer, doesn't know what an algorithm is. It concerns me more that it apparently didn't even occur to you to simply Google the word to see what it means.

    An algorithm is basically a recipe. It is a set of steps that you can perform to complete a process. It has nothing specifically to do with programming. In most cases you should be developing and testing your algorithm without even turning your computer on. If you don't have a working algorithm then you don't actually know what your code is supposed to do, which is why so many beginners have so much trouble writing code. This is a perfect example.

    So, let's examine and define the problem here. Let's ignore the fact that you have to write code and examine the real-world problem that you're trying to solve. You have an object that currently resides at a particular point and you want it move it gradually to another point, right? Presumably you want that movement to be as smooth as possible in a basically straight line between the two points, right? Can you see how this is really not specifically a programming problem? What if I gave you a big sheet of graph paper and placed a small coin on it and I said that I wanted you to gradually move that coin from point A, where is it currently is to point B and that you had to move it in basically a straight line while sticking to the whole unit intersections on the paper? That is basically a real-world equivalent of what you're trying to do with the cursor. How can you hope to write VB code to do it if you couldn't do it in the physical world and how could you hope to do it in the physical world if you didn't have a set of steps, i.e. an algorithm, to follow?

    So, you should consider the physical problem and develop an algorithm to solve it. You can test that algorithm with pen and paper to make sure it works in all possible scenarios, e.g. point B is above, below, to the left and to the right of point A. When you have an algorithm that is confirmed to work, then you can turn on your computer and write code to implement that algorithm, safe in the knowledge that, as long as the code truly implements the algorithm, the code will work.

    So, let's consider what you're already trying to do. You are moving the object 1 unit in the X direction and 1 unit in the Y direction as long as those components of its current location are not equal to the same component of the end point. If you were to do as I suggest and try that in the real world then you'd see how wrong it is. Let's consider where the object starts at (0, 0) and has to move to (100, 200). Using your approach, the object will move to (1, 1), (2, 2), (3, 3) and so on until it reaches (100, 100). Up to that point the object will have been moving diagonally, at 45 degrees. From there it will move to (100, 101), (100, 102) and so on until it reaches (100, 200). For that second stage the object will be moving vertically. Is that really what you want to happen? For the object to move diagonally until it is either horizontally or vertically in line with the end point and then for it to move horizontally or vertically? I certainly hope it isn't, but that is the result that your approach will produce. That's because you have started to write code with only a vague idea of what it's actually supposed to do and no real-world consideration or testing beforehand.

    Now here's what you should be doing. First, you need to determine the distance between the two points in a straight line. If you've done any match at school ten you should be immediately thinking Pythagoras. From the X and Y components of the two points you can determine the length of the line between. If you want to move approximately one unit at a time then that length tells you how many steps you need to take between point A and point B.

    Next, you need the equation of that line between the two points. Again, the maths you learned at school tells you that it takes the form 'y = mx + c'. I know that I calculated that equation from two points many times in school so I assume that everyone else did to. Again, from the X and Y components of the two points you can calculate the gradient of the line 'm' and then you can plug either of the two points into the general equation along with 'm' to find 'c'.

    So, you now have the equation of a line, a start point on that line, an end point on that line and the number of steps you need to take along that line between the start point and the end point. If that number of steps is N, you can then calculate the location of those N intermediate points along the line. For that you need to employ some trigonometry, again maths learned at school. You already know the distance between the two points in the X direction, in the Y direction and in a straight line, so you can use any of sine, cosine and tangent to calculate the appropriate angle. For each of the intermediate points, the angle will be the same and you know the length of the hypotenuse, so you can use sine or cosine to calculate the X and Y distance. In actual fact, you only need to do that once because each subsequent step will simply be multiple of those.

    You don't actually have to calculate all the intermediate points up front. You can simply calculate the first one and, as I said, each subsequent one will just be a multiple of that, so you can calculate them using a counter as you go. Now, we know that we have to stick to whole units so, at each step, we will have to round off both the X and Y coordinates to the nearest whole number. For instance, if we calculate that the next point is (2.3, 6.7) then we will actually move to (2, 7). The calculation for the next point is based on the pre-rounded value though, so that we ensure that we move as close to a single whole unit in the direction of the line between the start point and end point as possible.

    So, you just keep doing that over and over and you will eventually land on or go past the end point. If you go past you simply truncate the values to land on it and you're done.

    That might all sound incredibly complex but that's only because there are a lot of steps. It's that fact that makes it hard to write code without an algorithm. If you developing an algorithm using pen and paper though, it's very easy. Each step in developing the algorithm is elementary. All the maths is simple stuff learned at school. If you're using a pen and paper then it's easy to see the triangles so it's easy to know that you need to use Pythagoras and trigonometry. You'll note that there has been no programming at all up to now, so no programming knowledge or experience is required to get this far. You just need to have listened in maths class at school. At this point, if you have written down a concise set of steps that you need to follow to get an object from point A to point B in unit along a straight line then you have an algorithm. You could give that algorithm to any programmer who uses any programming language at all and they could write you code to implement it. That's why an algorithm has nothing specifically to do with programming. It's just a set of steps and you can implement those steps in any programming language you want, or even not in a programming language but get a real person to do it in the real world.

    This is the work that you should have done before even considering writing any code. Once you've turned my instructions above into an algorithm you should test that algorithm using pen and paper. Once you know it works, then you should start writing VB code to implement the algorithm. At each point you know exactly what the code has to do and , if you don't know how to do that in code, then you can ask a specific question, e.g. "how do I calculate a trig tangent" or "how do I round a number with decimal places". Those are questions that we can answer simply. Questions like "what's wrong with this code that I can't actually describe accurately the purpose of" are kinda hard to answer. When there's lots of things wrong it's hard for us to say "do this" and "do that" because the only way to really solve the problem is to scrap large chunks of code altogether.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    245

    Re: How do i make this NOT start from 0,0 but my current position?

    So, let's consider what you're already trying to do. You are moving the object 1 unit in the X direction and 1 unit in the Y direction as long as those components of its current location are not equal to the same component of the end point. If you were to do as I suggest and try that in the real world then you'd see how wrong it is. Let's consider where the object starts at (0, 0) and has to move to (100, 200). Using your approach, the object will move to (1, 1), (2, 2), (3, 3) and so on until it reaches (100, 100). Up to that point the object will have been moving diagonally, at 45 degrees. From there it will move to (100, 101), (100, 102) and so on until it reaches (100, 200). For that second stage the object will be moving vertically. Is that really what you want to happen? For the object to move diagonally until it is either horizontally or vertically in line with the end point and then for it to move horizontally or vertically? I certainly hope it isn't, but that is the result that your approach will produce. That's because you have started to write code with only a vague idea of what it's actually supposed to do and no real-world consideration or testing beforehand
    I actually wanted it to do exactly what you said, but if you know how i can make the cursor move to a (x & y) point humanly, without making a exact perfect line to get to the point, i would be very very appreciative

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How do i make this NOT start from 0,0 but my current position?

    So are you saying that you do want the cursor to move diagonally until it is either vertically or horizontally in line with the end point and then move either vertically or horizontally from there? Or are you saying that you want the cursor to move in a single line directly to the end point?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width