// move object o in the direction it's facing void moveObject(object *o) { // If object is ship, adjust for player controls if (o->type == SHIP) { // Acceleration if (o->state & OBJ_ACCELERATING) { vector acc = scaleVec(&(o->dir), o->spd*dtheta); if (lengthVec(&acc) < MAXSPEED) o->vel = addVec(&(o->vel), &acc); } // Turning if (o->state & OBJ_TURNING_LEFT) { turnObject(o, o->turn*dtheta); } else if (o->state & OBJ_TURNING_RIGHT) { turnObject(o, o->turn*-dtheta); } } // If object is gib, rotate it if (o->type == GIB) turnObject(o, o->turn*dtheta); // translate object and wrap its position if necessary vector x = scaleVec(&(o->vel), dtheta); o->pos = addVec(&(o->pos), &x); wrapPosition(o); }
// Game initialization function // ========================================================== void init(void) { // init random seed unsigned int seed = time(NULL); srand(seed); // Init asteroid object array asteroids = (object *)malloc(sizeof(object)*MAXASTEROIDS); int i; for(i=0;i<MAXASTEROIDS;i++) { // Create an asteroid at a random location on screen vector p = makeVec(10.0-(float)(rand()%25), 10.0-(float)(rand()%25)); initAsteroid(asteroids+i, &p, 3.0); // If asteroid spawns too close to ship, make it somewhere else while(lengthVec(&(asteroids[i].pos)) < 5.0) { p = makeVec(10.0-(float)(rand()%25), 10.0-(float)(rand()%25)); initAsteroid(asteroids+i, &p, 3.0); } // Activate 4 big asteroids at the beginning if (i%4!=0) asteroids[i].state &= !ASTEROID_ACTIVE; else asteroids[i].state |= ASTEROID_BIG; } // Init asteroid gibs (inactive) asteroidGibs = (object**)malloc(sizeof(object*)*MAXASTEROIDS); int ab; for (ab=0; ab<MAXASTEROIDS; ab++) { asteroidGibs[ab] = (object*)malloc(sizeof(object)*asteroids[ab].size); initGib(asteroidGibs[ab], asteroids+ab, 0); } // init ship ships = malloc(sizeof(object)*MAXSHIPS); for (i = 0; i < MAXSHIPS; i++) { initShip(ships+i); } myship = ships; myship->state = 0; // init ship stuff //initMissile(); // init ship gibs (inactive) shipGibs = (object*)malloc(sizeof(object)*ships[0].size*MAXSHIPS); for (i=0; i < MAXSHIPS; i++) { initGib(shipGibs+i*ships[0].size, ships, 0); } // init stars stars = (object*)malloc(sizeof(object)*MAXSTARS); int si; for(si=0;si<MAXSTARS; si++) { vector v = makeVec((float)(rand()%50)-25.0, (float)(rand()%50)-25.0); initStar(stars+si, &v); } }
int main(){ int N; scanf("%d\n", &N); std::vector<int> lengthVec(N); for(int p = 0; p < N; p++){scanf("%d", &lengthVec[p]);} sort(lengthVec.begin(), lengthVec.end()); int index = 0; int currentMin = lengthVec[0]; while(index < N){ printf("%d\n", N - index); while(index < N && lengthVec[index] == currentMin){++index;} currentMin = lengthVec[index]; } return 0; }
// Collisions void checkCollisions(void) { // Laser->asteroid collisions int li; for (li=0; li<MAXSHOTS; li++) { object *l = LAZORZ+li; // If shot is inactive, skip if (!(l->state & LAZER_ACTIVE)) continue; int a; // Else check all asteroids for a collision for (a = 0; a < MAXASTEROIDS; a++) { // Get distance vector from laser to asteroid centre vector dv = subVec(&(l->pos), &((asteroids+a)->pos)); if (asteroids[a].state & ASTEROID_ACTIVE // Asteroid active? && lengthVec(&dv) < asteroids[a].radius //Laser close enough? && pointPolygonCollide(asteroids+a, &(l->pos))) {//Collision? // Kill asteroid and deactivate laser shot initGib(asteroidGibs[a], asteroids+a, GIB_ACTIVE); killAsteroid(asteroids+a); l->state &= !LAZER_ACTIVE; break; } } } // // Missile->asteroid collisions // if (missile.state & MISSILE_ACTIVE) { // int si; // // Check eack vertex of the ship for a collision // for (si=0; si<missile.size; si++) { // int a; // // Get world coordinate of ship vertex // vector sv = localToWorld(missile.verts+si, &missile); // // Check all asteroids for collision // for (a = 0; a < MAXASTEROIDS; a++) { // // Skip if asteroid is not active (destroyed) // if (!(asteroids[a].state & ASTEROID_ACTIVE)) continue; // // Get distance vector // vector dv = subVec( &sv, &((asteroids+a)->pos)); // if (lengthVec(&dv) < asteroids[a].radius //Close enough? // && pointPolygonCollide(asteroids+a, &sv)) {//Collision? // // Kill asteroid and ship, display lose message // killAsteroid(asteroids+a); // // initGib(shipGibs, &ship, GIB_ACTIVE); // initGib(asteroidGibs[a], asteroids+a, GIB_ACTIVE); // // initMessage(); // missile.state &= !MISSILE_ACTIVE; // break; // } // } // } // } // Ship->asteroid collisions int si; object *ship; for ( si = 0; si < MAXSHIPS; si++ ) { ship = ships+si; if ( !(ship->state & ( SHIP_DED | SHIP_INACTIVE)) ) { int si; // Check eack vertex of the ship for a collision for (si=0; si<ship->size; si++) { int a; // Get world coordinate of ship vertex vector sv = localToWorld(ship->verts+si, ship); // Check all asteroids for collision for (a = 0; a < MAXASTEROIDS; a++) { // Skip if asteroid is not active (destroyed) if (!(asteroids[a].state & ASTEROID_ACTIVE)) continue; // Get distance vector vector dv = subVec( &sv, &((asteroids+a)->pos)); if (lengthVec(&dv) < asteroids[a].radius //Close enough? && pointPolygonCollide(asteroids+a, &sv)) {//Collision? // Kill asteroid and ship, display lose message killAsteroid(asteroids+a); initGib(shipGibs+si*ship->size, ship, GIB_ACTIVE); initGib(asteroidGibs[a], asteroids+a, GIB_ACTIVE); initMessage(); ship->state += SHIP_DED; break; } } } } } }
/*! \internal Draws the line ending with the specified \a painter at the position \a pos. The direction of the line ending is controlled with \a dir. */ void QCPLineEnding::draw(QCPPainter *painter, const QVector2D &pos, const QVector2D &dir) const { if (mStyle == esNone) return; QVector2D lengthVec(dir.normalized()); if (lengthVec.isNull()) lengthVec = QVector2D(1, 0); QVector2D widthVec(-lengthVec.y(), lengthVec.x()); lengthVec *= (float)(mLength*(mInverted ? -1 : 1)); widthVec *= (float)(mWidth*0.5*(mInverted ? -1 : 1)); QPen penBackup = painter->pen(); QBrush brushBackup = painter->brush(); QPen miterPen = penBackup; miterPen.setJoinStyle(Qt::MiterJoin); // to make arrow heads spikey QBrush brush(painter->pen().color(), Qt::SolidPattern); switch (mStyle) { case esNone: break; case esFlatArrow: { QPointF points[3] = {pos.toPointF(), (pos-lengthVec+widthVec).toPointF(), (pos-lengthVec-widthVec).toPointF() }; painter->setPen(miterPen); painter->setBrush(brush); painter->drawConvexPolygon(points, 3); painter->setBrush(brushBackup); painter->setPen(penBackup); break; } case esSpikeArrow: { QPointF points[4] = {pos.toPointF(), (pos-lengthVec+widthVec).toPointF(), (pos-lengthVec*0.8f).toPointF(), (pos-lengthVec-widthVec).toPointF() }; painter->setPen(miterPen); painter->setBrush(brush); painter->drawConvexPolygon(points, 4); painter->setBrush(brushBackup); painter->setPen(penBackup); break; } case esLineArrow: { QPointF points[3] = {(pos-lengthVec+widthVec).toPointF(), pos.toPointF(), (pos-lengthVec-widthVec).toPointF() }; painter->setPen(miterPen); painter->drawPolyline(points, 3); painter->setPen(penBackup); break; } case esDisc: { painter->setBrush(brush); painter->drawEllipse(pos.toPointF(), mWidth*0.5, mWidth*0.5); painter->setBrush(brushBackup); break; } case esSquare: { QVector2D widthVecPerp(-widthVec.y(), widthVec.x()); QPointF points[4] = {(pos-widthVecPerp+widthVec).toPointF(), (pos-widthVecPerp-widthVec).toPointF(), (pos+widthVecPerp-widthVec).toPointF(), (pos+widthVecPerp+widthVec).toPointF() }; painter->setPen(miterPen); painter->setBrush(brush); painter->drawConvexPolygon(points, 4); painter->setBrush(brushBackup); painter->setPen(penBackup); break; } case esDiamond: { QVector2D widthVecPerp(-widthVec.y(), widthVec.x()); QPointF points[4] = {(pos-widthVecPerp).toPointF(), (pos-widthVec).toPointF(), (pos+widthVecPerp).toPointF(), (pos+widthVec).toPointF() }; painter->setPen(miterPen); painter->setBrush(brush); painter->drawConvexPolygon(points, 4); painter->setBrush(brushBackup); painter->setPen(penBackup); break; } case esBar: { painter->drawLine((pos+widthVec).toPointF(), (pos-widthVec).toPointF()); break; } case esHalfBar: { painter->drawLine((pos+widthVec).toPointF(), pos.toPointF()); break; } case esSkewedBar: { if (qFuzzyIsNull(painter->pen().widthF()) && !painter->modes().testFlag(QCPPainter::pmNonCosmetic)) { // if drawing with cosmetic pen (perfectly thin stroke, happens only in vector exports), draw bar exactly on tip of line painter->drawLine((pos+widthVec+lengthVec*0.2f*(mInverted?-1:1)).toPointF(), (pos-widthVec-lengthVec*0.2f*(mInverted?-1:1)).toPointF()); } else { // if drawing with thick (non-cosmetic) pen, shift bar a little in line direction to prevent line from sticking through bar slightly painter->drawLine((pos+widthVec+lengthVec*0.2f*(mInverted?-1:1)+dir.normalized()*qMax(1.0f, (float)painter->pen().widthF())*0.5f).toPointF(), (pos-widthVec-lengthVec*0.2f*(mInverted?-1:1)+dir.normalized()*qMax(1.0f, (float)painter->pen().widthF())*0.5f).toPointF()); } break; } } }