/** * metodo que es el que va a utilizarse con los pThreads, no recibe nada, * se le tiene que ingresar un NULL, si se quiere interactuar con este; no * retorna nada, es meramente utilizado para los pThreads. * @param dato NULL, escriba NULL. * @return no retorna nada, esto solo se usa para los pThreads. */ void *Cliente_Thread::interactuar(void) { _sockfd = socket(AF_INET, SOCK_STREAM, cero); if (_sockfd < cero) error(error1); _server = gethostbyname(_ip); if (_server == NULL) { fprintf(stderr,error3); exit(cero); } bzero((char *) &_serv_addr, sizeof(_serv_addr)); _serv_addr.sin_family = AF_INET; bcopy((char *)_server->h_addr,(char *)&_serv_addr.sin_addr.s_addr, _server->h_length); _serv_addr.sin_port = htons(_portno); if (connect(_sockfd, (struct sockaddr *) &_serv_addr, sizeof(_serv_addr)) < cero) error(error2); if(_debug) printf("Please enter the message: "); ////////////////////////////////////////// while(true){ bzero(_buffer,DosCientaSeis); fgets(_buffer,DosCientaSeis-uno,stdin); _n = write(_sockfd, _buffer, getLenght(_buffer)); if (_n < cero) error(error4); bzero(_buffer,DosCientaSeis); _n = read(_sockfd, _buffer, DosCientaSeis-uno); if (_n < cero) error(error5); if(_debug) printf("%s\n", _buffer); } }
void reversePrint(dyn_string string) { int lenght = getLenght(string); for (lenght--; lenght >= 0; lenght--) printf("%c", string.elements[lenght]); printf("\n"); }
Vector2D* Vector2D::normalize() { float lenght = getLenght(); xPos/= lenght; yPos/= lenght; return this; }
ofVec2f Boid::flock() { ofVec2f sep; ofVec2f ali; ofVec2f coh; int sepCount = 0; int aliCount = 0; int cohCount = 0; for(int i = 0; i < neighbours->size(); i++){ if (index != i) { Boid currentBoid = neighbours->at(i); ofVec2f delta = pos - currentBoid.pos; float dist = getLenght(delta); if (dist < seperationRadius) { sep += delta.normalize() / sqrt(dist); sepCount++; } if (dist < neighbourRadius) { ali += currentBoid.vel; aliCount++; coh += currentBoid.pos; cohCount++; } } } if (sepCount > 0) sep = sep / sepCount; if (aliCount > 0) ali = ali / aliCount; if (cohCount > 0) { // mean of all pos of neighbours coh = coh / cohCount; } else { // no neighbours -> nowhere to go coh = pos; } coh = steerTo(coh); ali = ali.limit(maxForce); sep *= sepWeight; ali *= aliWeight; coh *= cohWeight; return sep + ali + coh; }
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* removeNthFromEnd(struct ListNode* head, int n) { struct ListNode *p = head; int index = getLenght(head) - n - 1; // 要获取删除节点的上一个 if (index == -1) { // 此时删除头节点 head = head->next; free(p); } else { while (index--) { p = p->next; } struct ListNode *removeNode = p->next; p->next = removeNode->next; free(removeNode); } return head; }
void Vector2D::normalize() { *this = *this / getLenght(); }