Blame mono/Assistance/Geometry.cs

cfceb0
using System;
77c1c7
using System.Collections.Generic;
a62e15
using System.Linq.Expressions;
cfceb0
cfceb0
namespace Assistance {
cfceb0
	public static class Geometry {
d13872
		public static readonly double precision = 1e-8;
a62e15
		public static readonly double precisionSqr = precision*precision;
cfceb0
		public static readonly double sqrt2Pi = Math.Sqrt(2.0*Math.PI);
cfceb0
		
a62e15
		public static bool isEqual(double a, double b)
a62e15
			{ return Math.Abs(b - a) <= precision; }
a62e15
		public static bool isLess(double a, double b)
7bbf70
			{ return a - precision < b; }
a62e15
		public static bool isGreater(double a, double b)
7bbf70
			{ return b - precision < a; }
0f2bf8
		public static bool isLessOrEqual(double a, double b)
7bbf70
			{ return a + precision <= b; }
0f2bf8
		public static bool isGreaterOrEqual(double a, double b)
7bbf70
			{ return b + precision <= a; }
a62e15
		
cfceb0
		public static double logNormalDistribuitionUnscaled(double x, double x0, double w) {
cfceb0
			return Math.Exp(-0.5*Math.Pow(Math.Log(x/x0)/w, 2.0))/x;
cfceb0
		}
cfceb0
		
cfceb0
		public static double logNormalDistribuition(double x, double x0, double w) {
cfceb0
			return logNormalDistribuition(x, x0, w)/(w*sqrt2Pi);
cfceb0
		}
cfceb0
		
cfceb0
		public static void truncateInfiniteLine(Rectangle bounds, ref Point p0, ref Point p1) {
cfceb0
			if (p0.isEqual(p1)) return;
cfceb0
			Point d = p0 - p1;
cfceb0
			if (Math.Abs(d.x)*bounds.height > bounds.width*Math.Abs(d.y)) {
cfceb0
				// horizontal
cfceb0
				double k = d.y/d.x;
cfceb0
				p1 = new Point(bounds.x1, p0.y + k*(bounds.x1 - p0.x));
cfceb0
				p0 = new Point(bounds.x0, p0.y + k*(bounds.x0 - p0.x));
cfceb0
			} else {
cfceb0
				// vertical
cfceb0
				double k = d.x/d.y;
cfceb0
				p1 = new Point(p0.x + k*(bounds.y1 - p0.y), bounds.y1);
cfceb0
				p0 = new Point(p0.x + k*(bounds.y0 - p0.y), bounds.y0);
cfceb0
			}
cfceb0
		}
77c1c7
		
a62e15
		public static class Interpolation<t> {</t>
a62e15
			public delegate T HalfFunc(T a, double b);
a62e15
			public delegate T FullFunc(T a, T b);
a62e15
			
a62e15
			public static FullFunc add;
a62e15
			public static FullFunc sub;
a62e15
			public static HalfFunc mul;
a62e15
			public static HalfFunc div;
77c1c7
		
a62e15
   			static Interpolation() {
a62e15
   				{ // add
a62e15
   					ParameterExpression a = Expression.Parameter(typeof(T));
a62e15
				    ParameterExpression b = Expression.Parameter(typeof(T));
a62e15
				    add = Expression.Lambda<fullfunc>(Expression.Add(a, b), a, b).Compile();</fullfunc>
a62e15
				}
a62e15
   				{ // sub
a62e15
   					ParameterExpression a = Expression.Parameter(typeof(T));
a62e15
				    ParameterExpression b = Expression.Parameter(typeof(T));
a62e15
				    sub = Expression.Lambda<fullfunc>(Expression.Subtract(a, b), a, b).Compile();</fullfunc>
a62e15
				}
a62e15
   				{ // mul
a62e15
   					ParameterExpression a = Expression.Parameter(typeof(T));
a62e15
				    ParameterExpression b = Expression.Parameter(typeof(double));
589f9a
				    mul = Expression.Lambda<halffunc>(Expression.Multiply(a, b), a, b).Compile();</halffunc>
a62e15
				}
a62e15
   				{ // div
a62e15
   					ParameterExpression a = Expression.Parameter(typeof(T));
a62e15
				    ParameterExpression b = Expression.Parameter(typeof(double));
589f9a
				    div = Expression.Lambda<halffunc>(Expression.Divide(a, b), a, b).Compile();</halffunc>
a62e15
				}
a62e15
   			}
a62e15
   			
a62e15
			public static T linear(T p0, T p1, double l)
7bbf70
				{ return add(mul(p0, 1.0-l), mul(p1, l)); }
a62e15
	
589f9a
			public static T spline(T p0, T p1, T t0, T t1, double l) {
a62e15
				double ll = l*l;
a62e15
				double lll = ll*l;
a62e15
				return add( add( mul(p0, ( 2.0*lll - 3.0*ll + 1.0)),
a62e15
				                 mul(p1, (-2.0*lll + 3.0*ll      )) ),
a62e15
				            add( mul(t0, (     lll - 2.0*ll + l  )),
a62e15
				                 mul(t1, (     lll - 1.0*ll      )) ));
a62e15
	        }
a62e15
	
589f9a
			public static T splineTangent(T p0, T p1, T t0, T t1, double l) {
a62e15
				double ll = l*l;
a62e15
				return add( mul(sub(p0, p1), 6.0*(ll - l)),
a62e15
				            add( mul(t0, ( 3.0*ll - 4.0*l + 1.0)),
a62e15
				                 mul(t1, ( 3.0*ll - 2.0*l      )) ));
a62e15
	        }
77c1c7
		}
a62e15
		
a62e15
		public static double interpolationLinear(double p0, double p1, double l)
7bbf70
			{ return p0*(1.0 - l) + p1*l; }
77c1c7
a62e15
		public static double interpolationSpline(double p0, double p1, double t0, double t1, double l) {
a62e15
			double ll = l*l;
a62e15
			double lll = ll*l;
a62e15
			return p0*( 2.0*lll - 3.0*ll + 1.0)
a62e15
			     + p1*(-2.0*lll + 3.0*ll      )
a62e15
			     + t0*(     lll - 2.0*ll + l  )
a62e15
			     + t1*(     lll - 1.0*ll      );
b82ef4
        }
b82ef4
a62e15
		public static double interpolationSplineTangent(double p0, double p1, double t0, double t1, double l) {
a62e15
			double ll = l*l;
a62e15
			return (p0 - p1)*6.0*(ll - l)
a62e15
			     + t0*( 3.0*ll - 4.0*l + 1.0)
a62e15
			     + t1*( 3.0*ll - 2.0*l      );
77c1c7
        }
0f2bf8
702257
		public static Point interpolationLinear(Point p0, Point p1, double l)
702257
			{ return p0*(1.0 - l) + p1*l; }
702257
702257
		public static Point interpolationSpline(Point p0, Point p1, Point t0, Point t1, double l) {
0f2bf8
			double ll = l*l;
0f2bf8
			double lll = ll*l;
0f2bf8
			return p0*( 2.0*lll - 3.0*ll + 1.0)
0f2bf8
			     + p1*(-2.0*lll + 3.0*ll      )
0f2bf8
			     + t0*(     lll - 2.0*ll + l  )
0f2bf8
			     + t1*(     lll - 1.0*ll      );
0f2bf8
        }
0f2bf8
702257
		public static Point interpolationSplineTangent(Point p0, Point p1, Point t0, Point t1, double l) {
0f2bf8
			double ll = l*l;
0f2bf8
			return (p0 - p1)*6.0*(ll - l)
0f2bf8
			     + t0*( 3.0*ll - 4.0*l + 1.0)
0f2bf8
			     + t1*( 3.0*ll - 2.0*l      );
0f2bf8
        }
cfceb0
	}
cfceb0
}