root/InOutExpositor/Utils.pde

Revision e4e203671406cf05793d8bdd3f90000f29767307, 7.5 kB (checked in by Robin Gareus <robin@…>, 2 years ago)

fixed monitoring.. (load previews)

  • Property mode set to 100644
Line 
1////////////////////////////////////////////////////////////////
2// Various / useful
3////////////////////////////////////////////////////////////////
4String rCharriots(String inStr) {
5  String res = "" ;
6  int maxLine = 32 ;
7  int strLen = inStr.length() ;
8  if(strLen>maxLine) {
9    int i = 0 ;
10    int ptt = strLen ;
11    res = inStr.substring(0,maxLine) ;
12    while(ptt>maxLine) {
13      i++ ;
14      int lastPos = (i+1)*maxLine ;
15      if(lastPos>strLen) lastPos = strLen ;
16      res += "...\n   ..."+inStr.substring(i*maxLine, lastPos) ;
17      ptt = strLen - lastPos ;
18      if(i>2) {
19        res +=  " (...)" ;
20        ptt = 0 ;
21      }
22    }
23  }
24  else res = inStr ;
25  return res ;
26}
27////////////////////////////////////////////////////////////////
28String getTypeString(int t) {
29  String res = "" ;
30  if(t==0) return res = "data" ;
31  if(t==1) return res = "audio" ;
32  if(t==2) return res = "video" ;
33  return res ;
34}
35
36String getTimeStamp() {
37  Date D = new Date() ;
38  long tt = D.getTime() ;
39  long ttt = tt/(long)1000 ;
40  String res = String.valueOf(ttt) ;
41  return res ;
42}
43String formatTimeStamp(long tmpstmp) {
44   Date thedate = new Date((long)tmpstmp*1000);
45  DateFormat formatter = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
46  String dateStr = formatter.format(thedate);
47 return dateStr;
48}
49//////////////////////////////////////////////////////////////////////
50void addTagInVector( Vector vect, String inMot, int inVal ) {
51  boolean dejaLa = false ;
52  Tag mt ;
53  for(int i=0;i<vect.size();i++) {
54    mt = ((Tag)vect.elementAt(i)) ;
55    if( mt.tag.equals(inMot) ) {
56      mt.val = mt.val+inVal ;
57      dejaLa = true ;
58    }
59  }
60  if(!dejaLa) vect.add( new Tag(inMot,inVal) ) ;
61}
62/// TAGS ///////////////////////////////////////////////////////////////////////////
63Vector makeTagsFromString( String inStr ) /////// get {A B C} TAG Vector from "A.12/B.13/C.QS..." string
64{
65  Vector res = new Vector() ;
66  String[] ls = split(inStr,aVirg) ;
67  for(int i=0;i<ls.length;i++) {
68    res.addElement( new Tag(ls[i], 0) ) ;
69  }
70  return res ;
71}
72String makeStringFromTags( Vector vect ) // get String from Tags Vector
73{
74  String stri="" ;
75  for(int u=0;u<vect.size();u++) {
76    stri += ((Tag)vect.elementAt(u)).getString() ;
77    if(u!=vect.size()-1) stri += aVirg ;
78  }
79  return stri ;
80}
81///////////////////////////////////////////////////////////////////////////
82///////////////////////////////////////////////////////////////////////////
83///////////////////////////////////////////////////////////////////////////
84Client getSelected()
85{
86  if(selected!=null) return selected ;
87  else return getClient(0) ;
88}
89//////////////////////////////////////////////////////////////////
90Client getClient( int id ) { // return Client associated with id
91  return (Client)myClients.elementAt(id) ;
92}
93//////////////////////////////////////////////////////////////////
94int getIdOfLogin( String login ) { // return the id of login (-1 if doesnt exist)
95  int res = -1 ;
96  for(int i=0;i<nClients;i++) {
97    String loginHad = ((Client)myClients.elementAt(i)).login ;
98    if(loginHad.equals(login))
99      res = i ;
100  }
101  return res ;
102}
103//////////////////////////////////////////////////////////////////
104Vector getOutLinkedClients( Client A )
105{
106  Vector res = new Vector() ;
107  for(int i=0;i<nClients;i++) {
108    if( drawnState.isAnyLink(A.id,i) )
109      res.add(getClient(i)) ;
110  }
111  return res ;
112}
113Vector getInLinkedClients( Client A )
114{
115  Vector res = new Vector() ;
116  for(int i=0;i<nClients;i++) {
117    if( drawnState.isAnyLink(i,A.id) )
118      res.add(getClient(i)) ;
119  }
120  return res ;
121}
122Vector getOutLinkedClients( Client A, int type )
123{
124  Vector res = new Vector() ;
125  for(int i=0;i<nClients;i++) {
126    if( drawnState.isLink(type,A.id,i) )
127      res.add(getClient(i)) ;
128  }
129  return res ;
130}
131Vector getInLinkedClients( Client A, int type )
132{
133  Vector res = new Vector() ;
134  for(int i=0;i<nClients;i++) {
135    if( drawnState.isLink(type,i,A.id) )
136      res.add(getClient(i)) ;
137  }
138  return res ;
139}
140//////////////////////////////////////////////////////////////////
141//////////////////////////////////////////////////////////////////
142//////////////////////////////////////////////////////////////////
143int randomButMe(int id)
144{
145  int a = int(random(0,nClients)) ;
146  if(nClients>1) {
147    while(a==id)
148      a = int(random(0,nClients)) ;
149  }
150  else a = 0 ;
151  return a ;
152}
153//////////////////////////////////////////////////////////////////
154//////////////////////////////////////////////////////////////////
155//////////////////////////////////////////////////////////////////
156//////////////////////////////////////////////////////////////////
157int getNombreConnex( int i, State inState, int type, boolean isOut ) // retourne le nombre reel de connexion d'une cellule
158{
159  int res = 0 ;
160  if(isOut) {
161    for(int j=0;j<nClients;j++) {
162      if( inState.isLink(type,i,j) ) res+=1 ; // i ---> j
163    }
164  }
165  else {
166    for(int j=0;j<nClients;j++) {
167      if( inState.isLink(type,j,i) ) res+=1 ; // j ---> i
168    } 
169  }
170  return res ;
171}
172//////////////////////////////////////////////////////////////////
173boolean isLinkPossible(int type, int i, int j) // i ----> j
174{
175  boolean res = true ;
176
177  if( !getClient(i).isLinkOn(type,true) ) res = false ;
178  if( !getClient(j).isLinkOn(type,false) ) res = false ;
179  if( !(getClient(i).connected && getClient(j).connected) ) res = false ;
180
181  return res ;
182}
183//////////////////////////////////////////////////////////////////
184//////////////////////////////////////////////////////////////////
185//////////////////////////////////////////////////////////////////
186//////////////////////////////////////////////////////////////////
187float getLinkAngle( Client A, Client B ) // A -------> B
188{
189  float x1 = A.x() ;
190  float y1 = A.y() ;
191  float x2 = B.x() ;
192  float y2 = B.y() ;
193
194  return (float)getPointsAngle(x1,y1,x2,y2) ;
195}
196float getPointsAngle(float x1, float y1, float x2, float y2) {
197  float ang = PI/2 ;
198  if(x2!=x1) ang = atan( (y2-y1)/(x2-x1) ) ;
199  if(x2<x1) ang = ang + PI ;
200
201  float res = modAngle(ang) ;
202  return res ;
203}
204//////////////////////////////////////////////////////////////////
205void reajustLinkedAngles( Client A, Client B ) // A -------> B
206{
207  A.angle = adjustToAngle(A.angle, getLinkAngle(A,B)) ;
208  B.angle = adjustToAngle(B.angle, getLinkAngle(A,B)) ;
209}
210float adjustToAngle(float a, float to)
211{
212  if(to<a) to=to+2*PI ;
213  float pDiff = to-a ;
214  float minDiff = min(pDiff, 2*PI-pDiff) ;
215  float newdA = minDiff/20 ;
216  if(pDiff<PI) a+= newdA ;
217  else a-= newdA ;
218  return  modAngle(a) ;
219}
220float modAngle( float a )
221{
222  return a%(2*PI) ;
223}
224void refreshLinkedAngles()
225{
226  Client c,l ;
227  for(int i=0;i<nClients;i++) {
228    c = getClient(i) ;
229    Vector outL = getOutLinkedClients(c) ;
230    for(int j=0;j<outL.size();j++) {
231      l = (Client)outL.elementAt(j) ;
232      l.angle = adjustToAngle( l.angle, getLinkAngle(l,c) ) ;
233      c.angle = adjustToAngle( c.angle, getLinkAngle(l,c) ) ;
234    }
235  }
236}
237//////////////////////////
238void setTypeColors() {
239  dColor = color(random(20,255),random(20,255),random(20,255));
240  aColor = color(random(20,255),random(20,255),random(20,255));
241  vColor = color(random(20,255),random(20,255),random(20,255));
242}
243color getColorType(int type) {
244  color res = color(200,200,200) ;
245  if(type==0) res = dColor ;
246  if(type==1) res = aColor ;
247  if(type==2) res = vColor ;
248  return res ;
249}
250//////////////////////////
251int getMaxNameLength() {
252  int res = 0 ;
253  for(int i=0;i<nClients;i++) {
254    String nam = getClient(i).name ;
255    if(nam.length()>res) res = nam.length() ;
256  }
257  return res ;
258}
259//////////////////////////
260float distance(Client A, Client B) {
261  float res = sqrt(pow(A.x()-B.x(),2)+pow(A.y()-B.y(),2)) ;
262  return res ;
263}
264
265
266
267
268
269
Note: See TracBrowser for help on using the browser.