[764] | 1 | #include <math.h>
|
---|
| 2 | #include <iostream.h>
|
---|
| 3 | #include "utilgeom.h"
|
---|
| 4 | //++
|
---|
| 5 | // Titre Some utilitary functions for geometry (utilgeom.h)...
|
---|
| 6 | //--
|
---|
| 7 | //++
|
---|
| 8 | int sign(double d)
|
---|
| 9 | //
|
---|
| 10 | //--
|
---|
| 11 | {
|
---|
| 12 | return (d >= 0) - (d < 0);
|
---|
| 13 | }
|
---|
| 14 | //++
|
---|
| 15 | double absd(double d)
|
---|
| 16 | //
|
---|
| 17 | //--
|
---|
| 18 | {
|
---|
| 19 | return sqrt(d*d);
|
---|
| 20 | }
|
---|
| 21 | //++
|
---|
| 22 | double mod(double d, double periode)
|
---|
| 23 | //
|
---|
| 24 | //--
|
---|
| 25 | {
|
---|
| 26 | if( d >= 0 ) return d-floor(d/periode)*periode;
|
---|
| 27 | else return d-ceil(d/periode)*periode+periode;
|
---|
| 28 | }
|
---|
| 29 | //++
|
---|
| 30 | void swap(double& d1, double& d2)
|
---|
| 31 | //
|
---|
| 32 | //--
|
---|
| 33 | {
|
---|
| 34 | double temp;
|
---|
| 35 | temp=d2;
|
---|
| 36 | d2=d1;
|
---|
| 37 | d1=temp;
|
---|
| 38 | }
|
---|
| 39 | //++
|
---|
| 40 | double min(double d1, double d2)
|
---|
| 41 | //
|
---|
| 42 | //--
|
---|
| 43 | {
|
---|
| 44 | if( d1 >= d2 ) return d2;
|
---|
| 45 | else return d1;
|
---|
| 46 | }
|
---|
| 47 | //++
|
---|
| 48 | double max(double d1, double d2)
|
---|
| 49 | //
|
---|
| 50 | //--
|
---|
| 51 | {
|
---|
| 52 | return -min(-d1,-d2);
|
---|
| 53 | }
|
---|
| 54 | //++
|
---|
| 55 | int arrondi(double d)
|
---|
| 56 | //
|
---|
| 57 | //--
|
---|
| 58 | {
|
---|
| 59 | return (int)(((d-floor(d)) >= 0.5)*ceil(d)+((d-floor(d)) < 0.5)*floor(d));
|
---|
| 60 | }
|
---|
| 61 | //++
|
---|
| 62 | long rangijd(int nc, int i, int j, int d)
|
---|
| 63 | //
|
---|
| 64 | //--
|
---|
| 65 | {
|
---|
| 66 | if( i < j ) return 2*i*(2*nc-i-1)+4*(j-i-1)+d;
|
---|
| 67 | if( i > j ) return 2*j*(2*nc-j-1)+4*(i-j-1)+d+2;
|
---|
| 68 | if( i == j )
|
---|
| 69 | {
|
---|
| 70 | cout << "pas d'intersection entre le cercle " << i
|
---|
| 71 | << " et le cercle " << j << "." << endl;
|
---|
| 72 | return -1;
|
---|
| 73 | }
|
---|
| 74 | else return -1;
|
---|
| 75 | }
|
---|
| 76 | //++
|
---|
| 77 | long rangdiff(int nc, int i, int j, int d)
|
---|
| 78 | //
|
---|
| 79 | //--
|
---|
| 80 | {
|
---|
| 81 | if( i == j )
|
---|
| 82 | {
|
---|
| 83 | cout << "diff n'est pas defini entre le cercle " << i
|
---|
| 84 | << " et le cercle " << j << "." << endl;
|
---|
| 85 | return 0;
|
---|
| 86 | }
|
---|
| 87 | int indm=(int)min(i,j);
|
---|
| 88 | int indM=(int)max(i,j);
|
---|
| 89 | return indm*(2*nc-indm-1)+2*(indM-indm-1)+d;
|
---|
| 90 | }
|
---|
| 91 | //++
|
---|
| 92 | long rangik(int NtotEch, int i, int k)
|
---|
| 93 | //
|
---|
| 94 | //--
|
---|
| 95 | {
|
---|
| 96 | return NtotEch*i+k;
|
---|
| 97 | }
|
---|
| 98 | //++
|
---|
| 99 | long ranghk(int NtotEch, int h, int k)
|
---|
| 100 | //
|
---|
| 101 | //--
|
---|
| 102 | {
|
---|
| 103 | return NtotEch*h+k;
|
---|
| 104 | }
|
---|
| 105 | //++
|
---|
| 106 | double scangle(double sinus, double cosinus)
|
---|
| 107 | //
|
---|
| 108 | //--
|
---|
| 109 | { // Modifie par D. Y. pour eviter des plantages aux petis angles
|
---|
| 110 | double eps=1.e-10;
|
---|
| 111 | if( fabs(1.-sinus*sinus-cosinus*cosinus) > eps)
|
---|
| 112 | { // On renormalise D.Y.
|
---|
| 113 | // cerr << "angle non valide: "<< "sinus = " << sinus << " cosinus = " << cosinus << '\n'; // BUGGG
|
---|
| 114 | double norm= cosinus*cosinus+sinus*sinus;
|
---|
| 115 | cosinus=cosinus/norm;
|
---|
| 116 | sinus=sinus/norm;
|
---|
| 117 | }
|
---|
| 118 | if( fabs(cosinus) >1. ) cosinus=1.*copysign(1.,cosinus);
|
---|
| 119 | if( fabs(sinus) >1. ) sinus=1.*copysign(1.,sinus);
|
---|
| 120 | /* if( fabs(sinus) == 0. ) sinus=0.;
|
---|
| 121 | if( fabs(cosinus) == 0. ) cosinus=0.;
|
---|
| 122 | */
|
---|
| 123 | return acos(cosinus)*copysign(1,sinus);
|
---|
| 124 | }
|
---|
| 125 |
|
---|