Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

I'm writing a program that needs to determine a variable (opacity) based on a linear function of time (milliseconds elapsed during the program).

Basically, over a period of 1000 milliseconds (end_time - start_time), I need to linearly calculate an integer (opacity) that goes from 255 to 0 (start_opacity to end_opacity).

What is this type of function called? Is it "linear regression?" "linear programming?" Can Wolfram Alpha create the function? I tried this (making my imaginary milliseconds being from 951 to 1951), but it's not quite what I want.

I want a function F to calculate the opacity based on these parameters:

opacity = F (start_opacity, end_opacity start_time, current_time, end_time)

What search terms will help me figure out how to do this?

(winning answer may also explain how to do it)

share|improve this question
1  
Then your function would look like ((end_opacity-start_opacity)/(end_time-start_time))(current_time-start_time)+st‌​art_opacity... –  J. M. Aug 31 '11 at 6:21
3  
J.M. is correct. This is called linear interpolation. –  Rahul Aug 31 '11 at 7:10
    
Thank you both! First one to phrase their comment in the form of an answer can get a green check. :-) –  Thunder Rabbit Aug 31 '11 at 7:28
add comment

1 Answer 1

up vote 2 down vote accepted

(To settle this...)

It's easy to come up with the equation of a line joining two points: if you assume a variable third point and then impose the condition that the slope of the segment joining the two given points and the slope of the segment joining one of the two givens and the variable, you get

$$\frac{y_2-y_1}{x_2-x_1}=\frac{y-y_1}{x-x_1}$$

In your case, since you wanted the line joining two points in the (time,opacity) plane, you then obtain the expression ((end_opacity-start_opacity)/(end_time-start_time))(current_time-start_time)+start_opacity. As Rahul mentions, this is sometimes termed as linear interpolation.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.