UnityMol  0.9.6-875
UnityMol viewer / In developement
ToolBox.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections;
3 using System.IO;
4 using System.Text;
5 //using System.Drawing;
6 
7 namespace BenTools.Mathematics
8 {
9  public struct Point
10  {
11  public int X;
12  public int Y;
13  }
14 
15  public struct PointF
16  {
17  public float X;
18  public float Y;
19 
20  public PointF(float x, float y)
21  {
22  this.X = x;
23  this.Y = y;
24  }
25  }
26 
27  public abstract class MathTools
28  {
32  public static readonly Random R = new Random((int)DateTime.Now.Ticks);
33  public static double Dist(double x1, double y1, double x2, double y2)
34  {
35  return Math.Sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
36  }
37  public static IList Shuffle(IList S, Random R, bool Copy)
38  {
39 // if(S.Rank>1)
40 // throw new Exception("Shuffle only defined on one-dimensional arrays!");
41  IList E;
42  E = S;
43  if(Copy)
44  {
45  if(S is ICloneable)
46  E = ((ICloneable)S).Clone() as IList;
47  else
48  throw new Exception("You want it copied, but it can't!");
49  }
50  int i,r;
51  object Temp;
52  for(i=0;i<E.Count-1;i++)
53  {
54  r = i+R.Next(E.Count-i);
55  if(r==i)
56  continue;
57  Temp = E[i];
58  E[i] = E[r];
59  E[r] = Temp;
60  }
61  return E;
62  }
63  public static void ShuffleIList(IList A, Random R)
64  {
65  Shuffle(A,R,false);
66  }
67  public static void ShuffleIList(IList A)
68  {
69  Shuffle(A,new Random((int)DateTime.Now.Ticks),false);
70  }
71  public static IList Shuffle(IList A, bool Copy)
72  {
73  return Shuffle(A,new Random((int)DateTime.Now.Ticks),Copy);
74  }
75  public static IList Shuffle(IList A)
76  {
77  return Shuffle(A,new Random((int)DateTime.Now.Ticks),true);
78  }
79 
80  public static int[] GetIntArrayRange(int A, int B)
81  {
82  int[] E = new int[B-A+1];
83  int i;
84  for(i=A;i<=B;i++)
85  E[i-A] = i;
86  return E;
87  }
88 
89  public static int[] GetIntArrayConst(int A, int n)
90  {
91  int[] E = new int[n];
92  int i;
93  for(i=0;i<n;i++)
94  E[i] = A;
95  return E;
96  }
97 
98 
99  public static int[] GetIntArray(params int[] P)
100  {
101  return P;
102  }
103 
104  public static object[] GetArray(params object[] P)
105  {
106  return P;
107  }
108  public static Array CopyToArray(ICollection L, Type T)
109  {
110  Array Erg = Array.CreateInstance(T,L.Count);
111  L.CopyTo(Erg,0);
112  return Erg;
113  }
114  public static string[] HighLevelSplit(string S, params char[] C)
115  {
116  ArrayList Erg = new ArrayList();
117  Stack CurrentBracket = new Stack();
118  int Pos = 0;
119  int i,c;
120 
121  for(i=0;i<S.Length;i++)
122  {
123  if(S[i]=='(')
124  {
125  CurrentBracket.Push(0);
126  continue;
127  }
128  if(S[i]=='[')
129  {
130  CurrentBracket.Push(1);
131  continue;
132  }
133  if(S[i]=='{')
134  {
135  CurrentBracket.Push(2);
136  continue;
137  }
138  if(S[i]==')')
139  {
140  if((int)CurrentBracket.Pop()!=0)
141  throw new Exception("Formatfehler!");
142  continue;
143  }
144  if(S[i]==']')
145  {
146  if((int)CurrentBracket.Pop()!=1)
147  throw new Exception("Formatfehler!");
148  continue;
149  }
150  if(S[i]=='}')
151  {
152  if((int)CurrentBracket.Pop()!=2)
153  throw new Exception("Formatfehler!");
154  continue;
155  }
156  if(CurrentBracket.Count>0)
157  continue;
158  c = Array.IndexOf(C,S[i]);
159  if(c!=-1)
160  {
161  if(C[c]=='\n')
162  {
163  if(i-2>=Pos)
164  Erg.Add(S.Substring(Pos,i-Pos-1));
165  Pos = i+1;
166  }
167  else
168  {
169  if(i-1>=Pos)
170  Erg.Add(S.Substring(Pos,i-Pos));
171  Pos = i+1;
172  }
173  }
174  }
175  if(CurrentBracket.Count>0)
176  throw new Exception("Formatfehler!");
177  if(i-1>=Pos)
178  Erg.Add(S.Substring(Pos,i-Pos));
179  return (string[])CopyToArray(Erg,typeof(string));
180  }
181 
182 // public static RectangleF MaxRectangleFit(RectangleF Target, SizeF Source)
183 // {
184 // float W,H;
185 // // 1. Auf höhe probieren
186 // H = Target.Height;
187 // W = Target.Height/Source.Height*Source.Width;
188 // if(W<=Target.Width)
189 // {
190 // return new RectangleF(Target.X+Target.Width/2-W/2,Target.Y,W,H);
191 // }
192 // // 2. Auf weite probieren
193 // W = Target.Width;
194 // H = Target.Width/Source.Width*Source.Height;
195 // return new RectangleF(Target.X,Target.Y+Target.Height/2-H/2,W,H);
196 //
197 // }
198  public static double DASkalar(double[] A, double[] B)
199  {
200  if(A.Length!=B.Length)
201  throw new Exception("Error in Skalar!");
202  double E=0;
203  int i;
204  for(i=0;i<A.Length;i++)
205  {
206  E+=A[i]*B[i];
207  }
208  return E;
209  }
210  public static double[] DAMult(double[] A, double r)
211  {
212  double[] E = new double[A.Length];
213  int i;
214  for(i=0;i<E.Length;i++)
215  {
216  E[i] = A[i]*r;
217  }
218  return E;
219  }
220 
221  public static double[] DAAdd(double[] A, double[] B)
222  {
223  if(A.Length!=B.Length)
224  throw new Exception("Error in Skalar!");
225  double[] E=new double[A.Length];
226  int i;
227  for(i=0;i<A.Length;i++)
228  {
229  E[i]+=A[i]+B[i];
230  }
231  return E;
232  }
233 
234  public static double DADist(double[] A, double[] B)
235  {
236  if(A.Length!=B.Length)
237  throw new Exception("Unterschiedliche Längen!");
238  int i;
239  double E = 0;
240  for(i=0;i<A.Length;i++)
241  E+=(A[i]-B[i])*(A[i]-B[i]);
242  return E;
243  }
244 
245  public static double DASum(double[] A)
246  {
247  double Erg=0;
248  foreach(double D in A)
249  {
250  Erg+=D;
251  }
252  return Erg;
253  }
254 
255  public static double DAMean(double[] A)
256  {
257  return DASum(A)/(double)A.Length;
258  }
259 
260  public static double DAStdv(double[] A, double M)
261  {
262  double Erg = 0;
263  foreach(double D in A)
264  Erg += (M-D)*(M-D);
265  return Erg/(double)A.Length;
266  }
267  private static int doubleToInt(double f)
268  {
269  if (f >= 2.147484E+09f)
270  {
271  return 2147483647;
272  }
273  if (f <= -2.147484E+09f)
274  {
275  return -2147483648;
276  }
277  return ((int) f);
278  }
279 
280  /* 0: minimum, +: rising, -: falling, 1: maximum. */
281  private static char[][] HSB_map = new char[6][]{new char[]{'1', '+', '0'},
282  new char[]{'-', '1', '0'},
283  new char[]{'0', '1', '+'},
284  new char[]{'0', '-', '1'},
285  new char[]{'+', '0', '1'},
286  new char[]{'1', '0', '-'}};
287 
288  public static double[] HSBtoRGB(int hue, int saturation, int brightness, double[] OldCol)
289  {
290  /* Clip hue at 360: */
291  if(hue<0)
292  hue = 360 - (-hue % 360);
293  hue = hue % 360;
294 
295  int i = (int)Math.Floor(hue/60.0),j;
296  double[] C;
297  if(OldCol==null || OldCol.Length!=3)
298  C = new double[3];
299  else
300  C = OldCol;
301 
302  double min = 127.0 * (240.0 - saturation)/240.0;
303  double max = 255.0 - 127.0 * (240.0 - saturation)/240.0;
304  if(brightness>120)
305  {
306  min = min + (255.0-min)*(brightness-120)/120.0;
307  max = max + (255.0-max)*(brightness-120)/120.0;
308  }
309  if(brightness<120)
310  {
311  min = min * brightness / 120.0;
312  max = max * brightness / 120.0;
313  }
314 
315  for (j = 0; j < 3; j++)
316  {
317  switch(HSB_map[i][j])
318  {
319  case '0':
320  C[j] = min;
321  break;
322  case '1':
323  C[j] = max;
324  break;
325  case '+':
326  C[j] = (min + (hue % 60)/60.0 * (max - min));
327  break;
328  case '-':
329  C[j] = (max - (hue % 60)/60.0 * (max - min));
330  break;
331  }
332  }
333  return C;
334  }
335 // public static Color HSBtoRGB(int hue, int saturation, int brightness)
336 // {
337 // double[] C = HSBtoRGB(hue,saturation,brightness,null);
338 // return Color.FromArgb((int)C[0],(int)C[1],(int)C[2]);
339 // }
340  public static double GetAngle(double x, double y)
341  {
342  if(x==0)
343  {
344  if(y>0)
345  return Math.PI/2.0;
346  if(y==0)
347  return 0;
348  if(y<0)
349  return Math.PI*3.0/2.0;
350  }
351  double atan = Math.Atan(y/x);
352  if(x>0 && y>=0)
353  return atan;
354  if(x>0 && y<0)
355  return 2*Math.PI+atan;
356  return Math.PI+atan;
357  }
358  public static double GetAngleTheta(double x, double y)
359  {
360  double dx, dy, ax, ay;
361  double t;
362  dx = x; ax = Math.Abs(dx);
363  dy = y; ay = Math.Abs(dy);
364  t = (ax+ay == 0) ? 0 : dy/(ax+ay);
365  if (dx < 0) t = 2-t; else if (dy < 0) t = 4+t;
366  return t*90.0;
367  }
368  public static int ccw(Point P0, Point P1, Point P2, bool PlusOneOnZeroDegrees)
369  {
370  int dx1, dx2, dy1, dy2;
371  dx1 = P1.X - P0.X; dy1 = P1.Y - P0.Y;
372  dx2 = P2.X - P0.X; dy2 = P2.Y - P0.Y;
373  if (dx1*dy2 > dy1*dx2) return +1;
374  if (dx1*dy2 < dy1*dx2) return -1;
375  if ((dx1*dx2 < 0) || (dy1*dy2 < 0)) return -1;
376  if ((dx1*dx1+dy1*dy1) < (dx2*dx2+dy2*dy2) && PlusOneOnZeroDegrees)
377  return +1;
378  return 0;
379  }
380  public static int ccw(double P0x, double P0y, double P1x, double P1y, double P2x, double P2y, bool PlusOneOnZeroDegrees)
381  {
382  double dx1, dx2, dy1, dy2;
383  dx1 = P1x - P0x; dy1 = P1y - P0y;
384  dx2 = P2x - P0x; dy2 = P2y - P0y;
385  if (dx1*dy2 > dy1*dx2) return +1;
386  if (dx1*dy2 < dy1*dx2) return -1;
387  if ((dx1*dx2 < 0) || (dy1*dy2 < 0)) return -1;
388  if ((dx1*dx1+dy1*dy1) < (dx2*dx2+dy2*dy2) && PlusOneOnZeroDegrees)
389  return +1;
390  return 0;
391  }
392 
393  public static bool intersect(Point P11, Point P12, Point P21, Point P22)
394  {
395  return ccw(P11, P12, P21, true)*ccw(P11, P12, P22, true) <= 0
396  && ccw(P21, P22, P11, true)*ccw(P21, P22, P12, true) <= 0;
397  }
398 
399  public static PointF IntersectionPoint(Point P11, Point P12, Point P21, Point P22)
400  {
401  double Kx = P11.X, Ky = P11.Y, Mx = P21.X, My = P21.Y;
402  double Lx = (P12.X-P11.X), Ly = (P12.Y-P11.Y), Nx = (P22.X-P21.X), Ny = (P22.Y-P21.Y);
403  double a=double.NaN,b=double.NaN;
404  if(Lx==0)
405  {
406  if(Nx==0)
407  throw new Exception("No intersect!");
408  b = (Kx-Mx)/Nx;
409  }
410  else if(Ly==0)
411  {
412  if(Ny==0)
413  throw new Exception("No intersect!");
414  b = (Ky-My)/Ny;
415  }
416  else if(Nx==0)
417  {
418  if(Lx==0)
419  throw new Exception("No intersect!");
420  a = (Mx-Kx)/Lx;
421  }
422  else if(Ny==0)
423  {
424  if(Ly==0)
425  throw new Exception("No intersect!");
426  a = (My-Ky)/Ly;
427  }
428  else
429  {
430  b = (Ky + Mx*Ly/Lx - Kx*Ly/Lx - My) / (Ny - Nx*Ly/Lx);
431  }
432  if(!double.IsNaN(a))
433  {
434  return new PointF((float)(Kx+a*Lx),(float)(Ky+a*Ly));
435  }
436  if(!double.IsNaN(b))
437  {
438  return new PointF((float)(Mx+b*Nx),(float)(My+b*Ny));
439  }
440  throw new Exception("Error in IntersectionPoint");
441  }
442  }
443 }
static double DASkalar(double[] A, double[] B)
Definition: ToolBox.cs:198
static string[] HighLevelSplit(string S, params char[] C)
Definition: ToolBox.cs:114
static PointF IntersectionPoint(Point P11, Point P12, Point P21, Point P22)
Definition: ToolBox.cs:399
static int ccw(Point P0, Point P1, Point P2, bool PlusOneOnZeroDegrees)
Definition: ToolBox.cs:368
static double DAStdv(double[] A, double M)
Definition: ToolBox.cs:260
static double GetAngleTheta(double x, double y)
Definition: ToolBox.cs:358
static object[] GetArray(params object[] P)
Definition: ToolBox.cs:104
static double[] DAAdd(double[] A, double[] B)
Definition: ToolBox.cs:221
static double[] HSBtoRGB(int hue, int saturation, int brightness, double[] OldCol)
Definition: ToolBox.cs:288
static int doubleToInt(double f)
Definition: ToolBox.cs:267
static int[] GetIntArrayRange(int A, int B)
Definition: ToolBox.cs:80
static Array CopyToArray(ICollection L, Type T)
Definition: ToolBox.cs:108
PointF(float x, float y)
Definition: ToolBox.cs:20
static double DAMean(double[] A)
Definition: ToolBox.cs:255
static void ShuffleIList(IList A)
Definition: ToolBox.cs:67
UnityEngine.Random Random
Definition: NoiseAndGrain.cs:3
static int[] GetIntArray(params int[] P)
Definition: ToolBox.cs:99
static double DASum(double[] A)
Definition: ToolBox.cs:245
static double Dist(double x1, double y1, double x2, double y2)
Definition: ToolBox.cs:33
static bool intersect(Point P11, Point P12, Point P21, Point P22)
Definition: ToolBox.cs:393
static int[] GetIntArrayConst(int A, int n)
Definition: ToolBox.cs:89
static int ccw(double P0x, double P0y, double P1x, double P1y, double P2x, double P2y, bool PlusOneOnZeroDegrees)
Definition: ToolBox.cs:380
static void ShuffleIList(IList A, Random R)
Definition: ToolBox.cs:63
static double DADist(double[] A, double[] B)
Definition: ToolBox.cs:234
static IList Shuffle(IList A, bool Copy)
Definition: ToolBox.cs:71
static IList Shuffle(IList A)
Definition: ToolBox.cs:75
static double GetAngle(double x, double y)
Definition: ToolBox.cs:340
static double[] DAMult(double[] A, double r)
Definition: ToolBox.cs:210
static IList Shuffle(IList S, Random R, bool Copy)
Definition: ToolBox.cs:37