Advertisement
Guest User

e2ir.c on line 40

a guest
Jul 26th, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.42 KB | None | 0 0
  1. module geometry;
  2. import std.stdio,std.algorithm,std.math;
  3.  
  4. double dist(double[] p1, double[] p2){
  5.     if((p1.length!=p2.length)||(p1.length!=3)){
  6.         throw new Exception("Distance meant for 3d points only.");
  7.     }
  8.     return sqrt((p2[0]-p1[0])^^2+(p2[1]-p1[1])^^2+(p2[2]-p1[2])^^2);
  9. }
  10. double[] cross(double[] v1, double[] v2){
  11.     return [v1[1]*v2[2]-v1[2]*v2[1],v1[2]*v2[0]-v1[0]*v2[2],v1[0]*v2[1]-v1[1]*v2[0]];
  12. }
  13. double[] unit(double[] v){
  14.     auto output = v.dup;
  15.     output[] /= len(output);
  16.     return output;
  17. }
  18. double len(double[] v){
  19.     return sqrt(v[0]^^2+v[1]^^2+v[2]^^2);
  20. }
  21.  
  22. double[] makeNormal(double[][] ps){
  23.     //Return a vector normal to the plane containing the 3 points.
  24.     double[] s1;
  25.     double[] s2;
  26.     s1[] += ps[0][]-ps[1][];
  27.     s2[] += ps[1][]-ps[2][];
  28.     return unit(cross(s1,s2));
  29. }
  30.  
  31. class tri{
  32.     double[][] ps;
  33.     double[][] es;
  34.     double[] center;
  35.     double[] normal;
  36.     double radius;
  37.     double zRadius;
  38.     this(double[][] points,double[] normal){
  39.         this.ps = points;
  40.         this.es = [this.ps[1][]-this.ps[0][],this.ps[2][]-this.ps[1][],this.ps[0][]-this.ps[2][]];
  41.         this.normal = normal;
  42.         this.center[] += this.ps[0][]+this.ps[1][]+this.ps[2][];
  43.         this.center[] /= 3;
  44.         this.zRadius = max(abs(this.center[2]-ps[0][2]),abs(this.center[2]-ps[1][2]),abs(this.center[2]-ps[2][2]));
  45.         this.radius = max(dist(center,ps[0]),dist(center,ps[1]),dist(center,ps[2]));
  46.     }
  47.     this(double[][] points){
  48.         this(points,makeNormal(points));
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement