Too many ElseIf's, ideas?

Dark_Neo
04-23-2004, 03:17 AM
I have a function to move the pupils in 2 eyes (it's an animation program) the problem is there are too many ElseIf statments, surely there must be a better way. Currently I have it so that I have 4 sliders that control the posistion of each pupil (2 sliders for each pupil) each slider has 3 positions, making a total of 9 posistions for the pupil, here's the code I've got:

Private Sub MoveEyes(eye As Integer, x As Integer, y As Integer)
offset = 0
If eye = 1 Then offset = 1800
If x = 0 And y = 0 Then
shpPupilsPreview(eye).Top = 960
shpPupilsPreview(eye).Left = 600 + offset
ElseIf x = 1 And y = 0 Then
shpPupilsPreview(eye).Top = 960
shpPupilsPreview(eye).Left = 720 + offset
ElseIf x = 2 And y = 0 Then
shpPupilsPreview(eye).Top = 960
shpPupilsPreview(eye).Left = 840 + offset
ElseIf x = 0 And y = 1 Then
shpPupilsPreview(eye).Top = 1080
shpPupilsPreview(eye).Left = 600 + offset
ElseIf x = 1 And y = 1 Then
shpPupilsPreview(eye).Top = 1080
shpPupilsPreview(eye).Left = 720 + offset
ElseIf x = 2 And y = 1 Then
shpPupilsPreview(eye).Top = 1080
shpPupilsPreview(eye).Left = 840 + offset
ElseIf x = 0 And y = 2 Then
shpPupilsPreview(eye).Top = 1200
shpPupilsPreview(eye).Left = 600 + offset
ElseIf x = 1 And y = 2 Then
shpPupilsPreview(eye).Top = 1200
shpPupilsPreview(eye).Left = 720 + offset
ElseIf x = 2 And y = 2 Then
shpPupilsPreview(eye).Top = 1200
shpPupilsPreview(eye).Left = 840 + offset
End If
End Sub

But I'm thinking of changing it so that there are 5 posistions for each slider. Making 25 posistions, this would require a massive amount of ElseIf's using my method. Also it creates another problem since the pupil can't go outside the eye, which would happen if both sliders where at their extremes (like if both where at 0 or 5, or one was at 0 and the other at 5) I would be gratefull for any ideas :D

Syko10-96
04-23-2004, 03:26 AM
Given the following based off of your If Then ElseIf's...

x = 0 then shpPupilsPreview(eye).Left = 600 + offset
x = 1 then shpPupilsPreview(eye).Left = 720 + offset
x = 2 then shpPupilsPreview(eye).Left = 840 + offset

AND

y = 0 then shpPupilsPreview(eye).Top = 960
y = 1 then shpPupilsPreview(eye).Top = 1080
y = 2 then shpPupilsPreview(eye).Top = 1200

The following Select Case pair should take care of your code, making it smaller and easier to read :)


Private Sub MoveEyes(eye As Integer, x As Integer, y As Integer)
offset = 0
If eye = 1 Then offset = 1800
Select Case x
Case 0
shpPupilsPreview(eye).Left = 600 + offset
Case 1
shpPupilsPreview(eye).Left = 720 + offset
Case 2
shpPupilsPreview(eye).Left = 840 + offset
End Select

Select Case y
Case 0
shpPupilsPreview(eye).Top = 960
Case 1
shpPupilsPreview(eye).Top = 1080
Case 2
shpPupilsPreview(eye).Top = 1200
End Select
End Sub


This could then easily be expanded to 5 positions apiece :)

webbone
04-23-2004, 03:26 AM
Looking at what you have posted I see that X is an index into the array {600, 720, 840} and Y is an index into the array {960, 1080, 1200}.

You can further reduce these through a little math (Assuming the value for eye is either 0 or 1):

.Left = 600 + (Eye*1800) + (X*120)
.Top = 960 + (Y*120)

Which reduces your sub to:

Private Sub MoveEyes(eye As Integer, x As Integer, y As Integer)
shpPupilsPreview(eye).Top = 960 + (Y*120)
shpPupilsPreview(eye).Left = 600 + (Eye*1800) + (X*120)
End Sub


You should be able to adjust the constants in these calculations to increase the number of steps in your sliders to just about anything - for example, instead of 3 positions, how about 30 positions?

Private Sub MoveEyes(eye As Integer, x As Integer, y As Integer)
shpPupilsPreview(eye).Top = 960 + (Y*12)
shpPupilsPreview(eye).Left = 600 + (Eye*1800) + (X*12)
End Sub


EDIT: I believe that reduces the number of ElseIf's to 0!

Syko10-96
04-23-2004, 03:28 AM
wow! guess I need more coffee ;) nice one webbone!

bk2003
04-23-2004, 03:29 AM
Private Sub MoveEyes(eye As Integer, x As Integer, y As Integer)
offset = eye * 1800


shpPupilsPreview(eye).Top = 960 + (y * 120)
shpPupilsPreview(eye).Left = 600 + (x*120) + offset
End Sub


Oops, way to late....

webbone
04-23-2004, 03:37 AM
Also it creates another problem since the pupil can't go outside the eye, which would happen if both sliders where at their extremes (like if both where at 0 or 5, or one was at 0 and the other at 5)

I'm assuming this means you are moving the pupil around inside a circle (the eye) and don't want to allow the user to move it outside of that circle? What is the size/position of the circle? It should be possible to limit the position by testing the values the equations I provided you with against whether they fall inside the circle.

EDIT: For those of you wishing to think up a solution to generate the limiting, remember that a circle is defined by the equation X^2 + Y^2 = R, for a circle of Radius R and center at (0, 0).

Dark_Neo
04-23-2004, 04:02 AM
Wow, thanks! It makes so much sense now!

The outer circle is 855 (diameter)
The pupil circle is 375.

I think it would be quite easy to check the values. I could just add the X and Y together and if it's too high, subtract from one of them.....hmmmmm......

Stroker
04-23-2004, 04:44 AM
Instead of x and y, why not rho and theta?

.top = cos(rho) * theta * stepmultipler1 + offset1
.left = sin(rho) * theta * stepmultiplier2 + offset2

Is that right?
Or did I get sin and cos mixed up?
(my trig is a bit rusty)

A polar approach would take care of the corner problem no prob.

Umm...
It's too early.

Mandelbrot
04-23-2004, 04:53 AM
I suppose the next point, then is "Why actually use sliders?". You could base the position of the eye on the position of your mouse click when close to it!


Paul. ;)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum