Equation of a straight line
You are viewing an old version of this entry, click here to see latest version.
public static function lEquation (T:Dynamic, x1:Float, x2:Float, y0:Float, y1:Float, y2:Float) :Dynamic { var x0 = (x2 - x1) * (y0 - y1) / (y2 - y1) + x1; return switch (T) { case Int: Math.round ( x0 ); default : x0; } }
What is this equation useful? For a lot of things, here's an example how to move a progress bar when you know the percent loaded:
progressBar.width = lEquation (Int, 0, 530, percent, 0, 100);
T is the type you want to return, Int or Float
0 - 530 means that the width of the progressBar will change from 0 to 530 when the percent will take values from 0 to 100
The math:
( x0 - x1 ) ( y0 - y1 ) ----------- = ----------- ( x2 - x1 ) ( y2 - y1 ) (x2-x1)*(y0-y1) x0 = ----------------- +x1 (y2-y1)
version #6774, modified 2009-08-18 23:11:59 by cristibaluta