 |
 |

04-23-2004, 03:17 AM
|
|
Regular
|
|
Join Date: Mar 2003
Posts: 60
|
|
Too many ElseIf's, ideas?
|
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:
Code:
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 
|
__________________
Dark Neo - Because good is just too boring
|

04-23-2004, 03:26 AM
|
 |
Contributor
|
|
Join Date: Oct 2002
Location: Virginia, USA
Posts: 716
|
|
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
Code:
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 
|
__________________
I honestly think you ought to calm down; take a stress pill and think things over. - HAL 9000 (2001: A Space Odyssey)
|

04-23-2004, 03:26 AM
|
 |
Hydrogen Powered
Administrator * Expert *
|
|
Join Date: Jul 2003
Location: Sacramento, CA
Posts: 6,090
|
|
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:
Code:
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?
Code:
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!
|
__________________
"With the appearance of the AddressOf operator, an entire industry has developed among authors illustrating how to do previously impossible tasks using Visual Basic. Another industry is rapidly developing among consultants helping users who have gotten into trouble attempting these tasks." -Dan Appleman
|

04-23-2004, 03:28 AM
|
 |
Contributor
|
|
Join Date: Oct 2002
Location: Virginia, USA
Posts: 716
|
|
wow! guess I need more coffee  nice one webbone!
|
__________________
I honestly think you ought to calm down; take a stress pill and think things over. - HAL 9000 (2001: A Space Odyssey)
|

04-23-2004, 03:29 AM
|
 |
Contributor
|
|
Join Date: Aug 2003
Location: Gothenburg, Sweden
Posts: 648
|
|
Code:
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....
|
|

04-23-2004, 03:37 AM
|
 |
Hydrogen Powered
Administrator * Expert *
|
|
Join Date: Jul 2003
Location: Sacramento, CA
Posts: 6,090
|
|
Quote:
|
Originally Posted by Dark_Neo
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).
|
__________________
"With the appearance of the AddressOf operator, an entire industry has developed among authors illustrating how to do previously impossible tasks using Visual Basic. Another industry is rapidly developing among consultants helping users who have gotten into trouble attempting these tasks." -Dan Appleman
|

04-23-2004, 04:02 AM
|
|
Regular
|
|
Join Date: Mar 2003
Posts: 60
|
|
|
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......
|
__________________
Dark Neo - Because good is just too boring
|

04-23-2004, 04:44 AM
|
|
Newcomer
|
|
Join Date: Apr 2004
Posts: 12
|
|
|
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.
|
|

04-23-2004, 04:53 AM
|
 |
Senior Contributor
|
|
Join Date: Apr 2002
Location: glbTheWorld.Europe.UK
Posts: 1,201
|
|
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. 
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|