Tanoda
CSharpExtensions.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10namespace Leap {
16 public static class CSharpExtensions {
21 public static bool NearlyEquals(this float a, float b, float epsilon = Constants.EPSILON) {
22 float absA = Math.Abs(a);
23 float absB = Math.Abs(b);
24 float diff = Math.Abs(a - b);
25
26 if (a == b) { // shortcut, handles infinities
27 return true;
28 } else if (a == 0 || b == 0 || diff < float.MinValue) {
29 // a or b is zero or both are extremely close to it
30 // relative error is less meaningful here
31 return diff < (epsilon * float.MinValue);
32 } else { // use relative error
33 return diff / (absA + absB) < epsilon;
34 }
35 }
36
41 public static bool HasMethod(this object objectToCheck, string methodName) {
42 var type = objectToCheck.GetType();
43 return type.GetMethod(methodName) != null;
44 }
45
50 public static int indexOf(this Enum enumItem) {
51 return Array.IndexOf(Enum.GetValues(enumItem.GetType()), enumItem);
52 }
53
58 public static T itemFor<T>(this int ordinal) {
59 T[] values = (T[])Enum.GetValues(typeof(T));
60 return values[ordinal];
61 }
62
67 public static void Dispatch<T>(this EventHandler<T> handler,
68 object sender, T eventArgs) where T : EventArgs {
69 if (handler != null) handler(sender, eventArgs);
70 }
71
78 public static void DispatchOnContext<T>(this EventHandler<T> handler, object sender,
79 System.Threading.SynchronizationContext context,
80 T eventArgs) where T : EventArgs {
81 if (handler != null) {
82 if (context != null) {
83 System.Threading.SendOrPostCallback evt = (spc_args) => { handler(sender, spc_args as T); };
84 context.Post(evt, eventArgs);
85 } else
86 handler(sender, eventArgs);
87 }
88 }
89 }
90}
91
Es.InkPainter.Math Math
Definition: PaintTest.cs:7