Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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|cite|improve this question
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
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
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


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|cite|improve this answer

Your Answer


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.