Size twinkle::nextSearchArea(Point* tl, Point last, Point previous, bool left) { int dx = last.x-previous.x; int dy = last.y-previous.y; //double grad; Size s; //---------Special cases: vertical sliding----------- if(dx==0) { if(dy<0) { //grad = DBL_MAX; std::cout<<"TWINKLE: GRADIENT IS MAX"<<std::endl; tl->x = last.x-1; tl->y = last.y-3; } else { //grad = DBL_MIN; std::cout<<"TWINKLE: GRADIENT IS MIN"<<std::endl; tl->x = last.x-1; tl->y = last.y +1; } s.width = 3; s.height = 3; return s; //Tertium non datur } //-------General behaviour: diagonal sliding--------- //Identify new point should last gradient be repeated Point hyp(last.x+dx, last.y+dy); //Identify area zone int minX, maxX; if(left) { minX = hyp.x; maxX = last.x -1; } else { minX = last.x+1; maxX = hyp.x; } int minY = std::min(last.y-1, hyp.y); int maxY = std::max(last.y+1, hyp.y); //Assign area size s.height = maxY-minY+1; s.width = maxX-minX+1; std::cout<<"NEXTSEARCHAREA: Area size is "<< s <<std::endl; //Update top left point coords and return area size tl->x = minX; tl->y = minY; return s; }
int main() { double a, b, h; /* declare the variables for sides of the right triangle */ /* Prompt for and receive input from user for sides a and b */ printf("Enter the length of side a: "); scanf("%lf", &a); printf("Enter the length of side b: "); scanf("%lf", &b); /* Call to function to calculuate hypotenus */ h = hyp(a, b); /* Print the length of the hypotenus */ printf("The hypotenus is %lf\n", h); return 0; }
int main(int argc, char **argv) { if (argc != 3) { help(argv[0]); return -1; } FstAlignOption option; FstAlignResult result; FstAligner aligner; OneBestFstLoader ref(argv[1]); OneBestFstLoader hyp(argv[2]); aligner.align(option, ref, hyp, result); result.writeAlignment(std::cout); return 0; }
void Ball::ProcessCollisions(unordered_map<string, Game_Object*> objects) { // Calculate Vector4 thisCenter(this->translationMatrix[0][3], this->translationMatrix[1][3], 0.0f, 0.0f); // Check collision with all objects for(unordered_map<string, Game_Object*>::iterator itr = objects.begin(); itr != objects.end(); itr++) { // Vector4 otherCenter(itr->second->translationMatrix[0][3], itr->second->translationMatrix[1][3], 0.0f, 0.0f); Vector4 combinedRadius = this->sprite->GetRadius() + itr->second->sprite->GetRadius(); // Check for collidable objects, based on dictionary name if(itr->first == "WallLeft" && thisCenter.x - sprite->GetRadius().x < otherCenter.x + itr->second->sprite->GetRadius().x) { velocity->x += this->velocity->x * -2.0f; } if(itr->first == "WallRight" && thisCenter.x + sprite->GetRadius().x > otherCenter.x - itr->second->sprite->GetRadius().x) { velocity->x += this->velocity->x * -2.0f; } if(itr->first == "WallTop" && thisCenter.y + sprite->GetRadius().x > otherCenter.y - itr->second->sprite->GetRadius().y) { velocity->y += this->velocity->y * -2.0f; } if(itr->first == "WallBottom" && thisCenter.y - sprite->GetRadius().x < otherCenter.y + itr->second->sprite->GetRadius().y) { velocity->y += this->velocity->y * -2.0f; } // Detect spherical collision if( ( itr->first.find("Ball") != string::npos || itr->first == "Bumper" /*|| itr->first == "Flipper1"*/) && itr->second != this && ( (thisCenter.x - otherCenter.x) * (thisCenter.x - otherCenter.x) ) + ( (thisCenter.y - otherCenter.y) * (thisCenter.y - otherCenter.y) ) < ( combinedRadius.x * combinedRadius.x) ) { // Get the vector in between the two objects Vector4 hyp( (thisCenter.x - otherCenter.x), (thisCenter.y - otherCenter.y), 0.0f, 0.0f ); // Get the normal vector to the colliding object Vector4 normal(hyp.normalize(hyp)); // Calculate a new velocity *velocity = normal * (velocity->dot(normal) * -2.0f) + *velocity; // Separate collided objects (upon collision 2 objects will slightly overlap so this is necessary) Matrix4::UpdatePositionMatrix(translationMatrix, velocity->x, velocity->y, 0); // NOTE: Ball-Ball collision sometimes only changes the velocity of one of the balls } // Angle of reflection (angle of flipper) float bounceAngle = 0.0f; if(itr->first == "Flipper1" && FlipperCollision(otherCenter, *(itr->second), bounceAngle)) { Vector4 flipperRadius(itr->second->sprite->GetRadius().x, itr->second->sprite->GetRadius().y, 0.0f, 0.0f); GLfloat cosAngle(itr->second->rotationMatrix[0][0]); GLfloat sinAngle(-itr->second->rotationMatrix[0][1]); // Calculate angle of impact GLfloat hitAngle = atan(velocity->y/velocity->x) + PI; if(velocity->x < 0) hitAngle += 180 * PI / 180; else if(velocity->x >= 0 && velocity->y < 0) hitAngle += 360 * PI / 180; if(hitAngle >= 2*PI - .0000002 && hitAngle <= 2*PI + .0000002) hitAngle = 0.0f; bounceAngle = PI - hitAngle - 2 * bounceAngle; if(bounceAngle < 0) bounceAngle += 2 * PI; float length = velocity->length(); // How to get a vector based on this angle Vector4 normal(length * cos(bounceAngle), length * sin(bounceAngle), 0.0f, 0.0f); *velocity = normal; // Separate collided objects (upon collision 2 objects will slightly overlap so this is necessary) Matrix4::UpdatePositionMatrix(translationMatrix, velocity->x, velocity->y, 0); // NOTE: Corner wonky, need to implement with rotation } } }