[Flash 9+]

september 2015

Smooth step for AS3

__ Math.cos
__ smoothstep2
__ smoothstep

Smoothstep is a scalar interpolation function commonly used in computer graphics and video game engines.
The function interpolates smoothly between two input values based on a third one that should be between the first two. The returned value is clamped between 0 and 1.
The slope of the smoothstep function tends toward zero at both edges. This makes it easy to create a sequence of transitions using smoothstep to interpolate each segment rather than using a more sophisticated or expensive interpolation technique.

source : http://en.wikipedia.org/wiki/Smoothstep

package
{
	import flash.display.Sprite;
	
	/**
	 * ...
	 * @author YopSolo
	 */
	public class Main extends Sprite
	{
		
		public function Main()
		{
			var step:Number = .01;
			
			graphics.lineStyle(0, 0x0000ff); // blue
			for (var x:Number = 0.0; x < 1; x += step)
			{
				var y:Number = 1 - smoothstep(0, 1, x);
				if (x == 0.0)
				{
					graphics.moveTo(x * 400, y * 400);
				}
				else
				{
					graphics.lineTo(x * 400, y * 400);
				}
			}
			
			// smoothstep2
			graphics.lineStyle(0, 0x00ff00); // green
			for (x = 0.0; x < 1; x += step)
			{
				y = 1 - smoothstep2(0, 1, x);
				if (x == 0.0)
				{
					graphics.moveTo(x * 400, y * 400);
				}
				else
				{
					graphics.lineTo(x * 400, y * 400);
				}
			}
			
			// Cos
			graphics.lineStyle(0, 0xff0000); // red
			for (x = 0; x < 1; x += step)
			{
				y = Math.cos(Math.PI * x) * .5 + .5;
				if (x == 0)
				{
					graphics.moveTo(x * 400, y * 400);
				}
				else
				{
					graphics.lineTo(x * 400, y * 400);
				}
			}
		}
		
		//http://en.wikipedia.org/wiki/Smoothstep
		private function smoothstep(e0:Number, e1:Number, x:Number):Number
		{
			x = Math.min(Math.max((x - e0) / (e1 - e0), 0.0), 1.0);
			return x * x * (3 - 2 * x);
		}
		
		private function smoothstep2(edge0:Number, edge1:Number, x:Number):Number
		{
			// Scale, bias and saturate x to 0..1 range
			x = Math.min(Math.max((x - edge0) / (edge1 - edge0), 0.0), 1.0);
			// Evaluate polynomial
			return x * x * x * (x * (x * 6 - 15) + 10);
		}
	
	}

}

Vote in HexoSearch