Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Locating Mouse co-ords.

CitizenOlekCitizenOlek Posts: 53Member
Hi All,

Do you remember an old board game called 4000 A.D.?
I'm writing a translation in VB.Net! I've got a Form with a PictureBox that has the Game Board Graphic in it. It has a space background with 48 Main Stars. I've written a routine that finds which Star the Mouse is hovering over and then Highlights that Star, or unhighlights a Star if the Mouse has stopped hovering over it, using the following code:

[Start code]
For Apos = 0 To 47
' If we're touching a Star
If (e.X > (StarX(Apos) - 10) And e.X < (StarX(Apos) + 10)) And (e.Y > (StarY(Apos) - 10) And e.Y < (StarY(Apos) + 10)) Then
If StHi Then
Apos = 47
Else
starClicked = Apos
StarInfo(Apos)
HighlightStar(tmpStr1, StarX(Apos), StarY(Apos))
saveStX = StarX(Apos)
saveStY = StarY(Apos)
StHi = True
Apos = 47
End If
Else
' We;re not touching a Star
' Erase Highlight
If StHi = True Then
HighlightStar("", saveStX, saveStY)
TextBox1.Text = GameMessage
StHi = False
End If
End If
Next Apos
[End code]

This routine works, but it is slow.
Is there another way I could accomplish this faster?
Keep in mind I am a complete newcomer to VB.Net and there is much I do not understand.

Thanks and have a great day.

Comments

  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    [code]
    : [Start code]
    : For Apos = 0 To 47
    : ' If we're touching a Star
    : If (e.X > (StarX(Apos) - 10) And e.X < (StarX(Apos) + 10)) And
    : (e.Y > (StarY(Apos) - 10) And e.Y < (StarY(Apos) + 10)) Then
    : If StHi Then
    : Apos = 47
    : Else
    : starClicked = Apos
    : StarInfo(Apos)
    : HighlightStar(tmpStr1, StarX(Apos), StarY(Apos))
    : saveStX = StarX(Apos)
    : saveStY = StarY(Apos)
    : StHi = True
    : Apos = 47
    : End If
    : Else
    : ' We;re not touching a Star
    : ' Erase Highlight
    : If StHi = True Then
    : HighlightStar("", saveStX, saveStY)
    : TextBox1.Text = GameMessage
    : StHi = False
    : End If
    : End If
    : Next Apos
    : [End code]
    [/code]


    Well, I would love to help you optimize this program to run faster, but I would have to see more code than just this.

    I don't know what routine you have this code under, but I get the feeling like you have it under "PictureBox1.MouseMove" or something like that. Basically something you want to keep in mind when you have a loop with If statements in it, it is going to start to slow down, especially since your if statements have several cases.

    So there are a few things you could try...

    [b]Circut Logic[/b]: I have no awesome way of explaining this... but basically you use AndAlso and OrElse instead of And and Or parts of your if statement. So your top if statement would look like this...

    [code]
    If (e.X > (StarX(Apos) - 10) AndAlso e.X < (StarX(Apos) + 10)) AndAlso (e.Y > (StarY(Apos) - 10) AndAlso e.Y < (StarY(Apos) + 10)) Then
    [/code]
    What this does is checks each case individually and as soon as one case is fasle it immediately goes to the ElseIf's or Else... Normally, it does all 4 passes on this if statement at once (which is very very slightly faster then the circut logic) however, overall you will save time by using Circut logic on a If statement inbedded in a Loop or a function that gets called alot.

    I noticed that you are setting Apos = 47 (the number that ends the for loop) Instead of that, try using "Exit Sub" or "Exit For" depending on what you want to do. If you are finished at that point with all the work that the function is going to do, do an Exit Sub, if you just want to drop out of the For Loop, do an Exit For.

    If you want something to run fast, basically try to stay away from If Statements (which is hard in some cases, I wrote code for a panel that I can resize / move with my mouse and the cursor changes in the proper areas and you can resize from corners or sides like a regular windows form works, this panel also 'snaps' or 'docks' into the side of a form if you drag it off... anyways, several revisions later I learned that Select cases are faster than If statements and Circut logic becomes your freind when you have code on the MouseMove function...)

    Hope this helps! Feel free to reply to this post with as many questions as you need to ask, I will answer them whenever I see them.
    Sean Campbell - Firesickle.com
  • CitizenOlekCitizenOlek Posts: 53Member
    Yes! You guessed right that this code was in the Mouse_Move event. I quickly typed in your revisions and was pleased to see an iimediate improvement in speed. Those simple changes made the routine 3 or 4 times faster.

    Thank you so much.
    Happy coding.
  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    : Yes! You guessed right that this code was in the Mouse_Move event. I
    : quickly typed in your revisions and was pleased to see an iimediate
    : improvement in speed. Those simple changes made the routine 3 or 4
    : times faster.
    :
    : Thank you so much.
    : Happy coding.
    :
    If you still feel your code is running too slow, I am sure there are other things we can try and optomize for the project to run quicker.
Sign In or Register to comment.