SitePoint Sponsor

# Thread: Calculating yardage

1. ## Calculating yardage

Question, how would you go about calculating the number of yards of a football play? Example say the offense starts at the 10 yard line and run a play that gets them to the 40 yard line on the opposition side. It wouldnt be 30 since it is on the other side of the field, so how would I go about taking 2 entries, the starting yardline and the ending yard line, and get the correct yardage? Weither it is negative or positive. Perhaps I shouldnt look at it as 50 yards one side and 50 the other, rather look at it as the full 100 yards?

Thanks
Silly

2. You would definitely need additional info like what/who's side of the field the ending yard marker is on. You wouldnt be able to calculate accurately otherwise right? Normally you'd say ball on team2's 40 yard line and the ball started on Team1's 10 yard line.

3. Originally Posted by Mandibal
You would definitely need additional info like what/who's side of the field the ending yard marker is on. You wouldnt be able to calculate accurately otherwise right? Normally you'd say ball on team2's 40 yard line and the ball started on Team1's 10 yard line.
right so I have them state which side of the field the yardline starts and the yardline, then what side of the field it ended at and the yardline. The next step is how would I calculate that? Is there a mathetical calculation that just multiples/add/subtracts it to get the correct yardage? For instance if they where on their own 10 yard line, then they went to the opponents 40 yard line, perhaps I calc the first 50 yard line then the next 50 if they are on opp side, (50-10) + (50 - 40) = 50

Silly

4. something like if else

if \$team1
for(\$i = 0; \$1 < 50 \$i++

else \$team2
for(\$i = 0; \$1 < 50 \$i--

Just a thought....

5. For instance if they where on their own 10 yard line, then they went to the opponents 40 yard line, perhaps I calc the first 50 yard line then the next 50 if they are on opp side, (50-10) + (50 - 40) = 50
This seems good for when they cross the 50 yrd line. If they dont you could then do:
own 10 to own 40 = \$ending - \$starting = yrds gained.
opp 40 to opp 20 = \$starting - \$ending = yrds gained.
This of course is what you've done for each halfs calc when crossing the 50.

6. Originally Posted by Mandibal
This seems good for when they cross the 50 yrd line. If they dont you could then do:
own 10 to own 40 = \$ending - \$starting = yrds gained.
opp 40 to opp 20 = \$starting - \$ending = yrds gained.
This of course is what you've done for each halfs calc when crossing the 50.
So your saying if they dont pass the 50 yard line then simply subtrack the smaller number from the largest number and that gives you the total yards. If they do pass the 50, meaning if they select opposing side when they started from their own side, then calc 50 minus the yard line they ended up at on the other side. IE if on own side with the starting yard line 10 and they get to the 40 then 40 - 10 = 30. but if the starting yard line is 10 and they get to opp 40 then you will say (50 - 10) + (50 - 40) = 50. And if they stay on the opp side it would be, starting at the 40 ending at the 30, 40 - 30 = 10.

The key is them choosing own or opponent when entering the 2 numbers. Then through a if/else statement it will tell you to use 2 subtractions to get total yards, if they pass the 50, or just a single subtraction and if you take ending first or starting. But in this case either side the largest number is always first right?

Silly

7. I think you've got. You need 2 pieces of data each for starting and ending position. own/opp and yard marker. Then you can do an if else to decide which calculation to use. Heres a function that should work:
PHP Code:
``` /*  * Calculate yardage gained on a play  *  * @param string \$start_half own or opp  * @param int \$start_yard starting yard marker  * @param string \$end_half own or opp  * @param int \$end_yard ending yard marker  * @return int \$yards_gained  */ function calculateYards(\$start_half, \$start_yard, \$end_half, \$end_yard) {     if (\$start_half != \$end_half)     {         \$yards_gained = (50-\$start_yard) + (50-\$end_yard);     }else{         //save us some more if/else just take the absolute value ;)         \$yards_gained = abs(\$end_yard - \$start_yard);     }          return \$yards_gained; }  ```

8. You could also think about assigning internal numbers (0-100) based on the drive. IOW, if they start on their own 20 you'd calculate the starting point as:
PHP Code:
``` \$scrimmage = 20; \$start = 100 - \$scrimmage; echo \$start; // returns 80 // The "field of play" is 80 yards now  ```
Start on the opposition 45:
PHP Code:
```  \$scrimmage = 55; \$start = 100 - \$scrimmage; echo \$start; // returns 45  ```
Follow me?

9. Originally Posted by Sketch
You could also think about assigning internal numbers (0-100) based on the drive. IOW, if they start on their own 20 you'd calculate the starting point as:
PHP Code:
``` \$scrimmage = 20;\$start = 100 - \$scrimmage;echo \$start;// returns 80// The "field of play" is 80 yards now  ```
Start on the opposition 45:
PHP Code:
``` \$scrimmage = 55;\$start = 100 - \$scrimmage;echo \$start;// returns 45  ```
Follow me?
Yes I follow you. I will try both and see which one works out the best. Thanks again for all the input. Love this forum!!

Silly

#### Posting Permissions

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