MOCKSTACKS
EN
Questions And Answers

More Tutorials




How to lerp in PHP?


The lerp() function is used to find a number between two numbers. The amt parameter can be used to specify the amount to interpolate between the two values. An amount nearer to 0.1 would mean that the final value is nearer to the first value, and nearer to 0.9 means that the value is nearer to the second value. If the value is less or more than these, then the final value would be calculated on the basis of the ratio of the two numbers.

It can be used for drawing dotted lines or creating motion along a path by finding all the intermediate points in a line.

Syntax:

lerp( start, stop, amt )

Example


function setup() {
createCanvas(600, 200);
textSize(20);

inputElemA = createInput(10);
inputElemA.position(30, 40);

inputElemB = createInput(100);
inputElemB.position(30, 60);

sliderElem = createSlider(0, 1, 0.5, 0.1);
sliderElem.position(30, 120);
}

function draw() {
clear();
text("Enter two values between which new "
		+ "number would be lerped", 20, 20);
text("Move the slider to observe the amount"
		+ " of lerping", 20, 100);

// Convert the string value to a number
// value for lerping
inputValA = Number(inputElemA.value());
inputValB = Number(inputElemB.value());
sliderVal = sliderElem.value();

text("The amount of lerping is: "
		+ sliderVal, 20, 160);
		
text("The lerped value is: "
		+ lerp(inputValA, inputValB,
		sliderVal), 20, 180);
}

Output


Example 2


function setup() {
createCanvas(600, 300);
textSize(20);

sliderElem = createSlider(0, 1, 0.5, 0.1);
sliderElem.position(30, 180);

circleApos = 50;
circleBpos = 500;
}

function draw() {
clear();
text("Move the slider to observe the x position "
		+ "of the middle circle", 20, 160);

circle(circleApos, 50, 80);
circle(circleBpos, 50, 80);

sliderVal = sliderElem.value();
lerpedVal = lerp(circleApos, circleBpos, sliderVal);

// Draw the circle at the lerped x coordinate
circle(lerpedVal, 50, 80);

text("The amount of lerping is: " + sliderVal, 20, 220);
}

Output



Conclusion

In this page (written and validated by ) you learned about . What's Next? If you are interested in completing PHP tutorial, we encourage you simply to start here: PHP Tutorial.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.