Can I make my code better and faster?

groover

Member
Joined
Oct 10, 2009
Messages
23
Programming Experience
1-3
Hi all,

I am doing hit-tests on more than 50 regions. I use a moving picture with 8 test-points on the outline of the picture.
One of these test-points sometimes has to be passed to a sub or function.
Not all of the points have to be tested, it depends on the direction of movement and the shape of the region.
The regions are created at form load.
The code I have has a lot of repeating and a lot of IF,AND,OR, etc.

My code works OK, but is slow and not very compact.

Here is a part of the code I use:
VB.NET:
 Public Sub hitTest()

        Dim hitPoint As PointF 'Is passed to subs and functions.Can be any of the  folowing points:

        Dim PN As PointF = New PointF(ulcorner.X + 24, ulcorner.Y - 8)      'North
        Dim PNO As PointF = New PointF(ulcorner.X + 43, ulcorner.Y + 2)     'NothEast
        Dim PO As PointF = New PointF(ulcorner.X + 50, ulcorner.Y + 24)     'East
        Dim PZO As PointF = New PointF(ulcorner.X + 38, ulcorner.Y + 36)    'SouthEast
        Dim PZ As PointF = New PointF(ulcorner.X + 24, ulcorner.Y + 52)     'South
        Dim PZW As PointF = New PointF(ulcorner.X + 14, ulcorner.Y + 36)    'SouthWest
        Dim PW As PointF = New PointF(ulcorner.X - 2, ulcorner.Y + 24)      'West
        Dim PNW As PointF = New PointF(ulcorner.X + 5, ulcorner.Y + 8)      'NorthWest

        'Test Startgateregion 
        I****N = rgnStartgate.IsVisible(pn)
        I****NO = rgnStartgate.IsVisible(PNO)
        I****NW = rgnStartgate.IsVisible(PNW)
        I****ZO = rgnStartgate.IsVisible(PZO)
        I****Z = rgnStartgate.IsVisible(PZ)


        If ulcorner.X < 470 Then isInField = True

        If I****N Or I****NO Or I****NW = True And isInField = False Then
            gravityTimer.Restart()
            n = 0

        Else : If I****NO Or I****Z Or I****ZO And rightdir = True And isInField = True Then
                ballangle = ballangle - 180
                o = 0
                n = 0
            End If
        End If

            I****N = False
            I****NO = False
            I****NW = False
        I****ZO = False
        I****Z = False

        'Test Toparch(reg)
            I****N = reg.IsVisible(pn)
            I****NO = reg.IsVisible(PNO)
        I****NW = reg.IsVisible(PNW)

        If I****N = True Then hitPoint = PN
            If I****NO = True Then hitPoint = PNO
            If I****N Or I****NO Or I****NW = True And n > 10 Then
                gravityTimer.Restart()
            reflectToparch(PN)

        End If
        n = 0

        I****N = False
        I****NO = False
        I****NW = False

        'toplefttargethit
        I****N = rgnTopleft.IsVisible(PN)
        I****NO = rgnTopleft.IsVisible(PNO)
        I****O = rgnTopleft.IsVisible(PO)
        I****NW = rgnTopleft.IsVisible(PNW)
        I****W = rgnTopleft.IsVisible(PW)

        If I****W Or I****NW = True And leftdir = True Then

            reflectTopleftside()
            n = 0
        End If

        I****N = False
        I****NO = False
        I****O = False
        I****NW = False
        I****W = False
        I****Z = False

        ' leftnarhittest

        'leftnarImage, 140, 140)
        I****N = rgnLeftNar.IsVisible(pn)
        I****NO = rgnLeftNar.IsVisible(PNO)
        I****O = rgnLeftNar.IsVisible(PO)
        I****NW = rgnLeftNar.IsVisible(PNW)
        I****W = rgnLeftNar.IsVisible(PW)
        I****Z = rgnLeftNar.IsVisible(PZ)

        If I****Z = True And y >= 140 Then

            hitPoint = pn

            ballangle = ballangle + 90
            n = 0
            I****N = False
            I****NO = False
            I****O = False
            I****NW = False
            I****W = False
            I****Z = False
        End If
And so on for all the regions.

How can I make my code faster and better?

Thanks,

Groover

PS
For some reason when I submit this, I s H i t (without the spaces and underscore) changes to I**** , I don't know why ?
 

Latest posts

Back
Top