예제 #1
0
/**
 * @brief Where your program starts running.
 * @param argc How many 'tokens' were typed when you ran the program
 * @param argv A collection of all the tokens that were typed
 * @sideeffect The program is the side effect! CS language is weird...
 * @return This returns 0, unless it crashes or something weird happens.
 *
 * This program has three stages.
 *
 * In the first stage, it figures out the first eight Fibonacci numbers, starting with 1,1,....
 *
 * In the second stage, it figures out the nine Ulam numbers after 5 (which ends up getting redundant after a while...)
 *
 * In the third stage, it counts out the six integers after 1.
 *
 * In the fourth and last stage, it finds the averages of a few trios of numbers.
 */
int main(int argc, char* argv[])
{
	int shaggy_dog;
	int scratchpad2;
	
    printf("Integers...\n");
    nextInteger(1);
    nextInteger(2);
    nextInteger(3);
    nextInteger(4);
    nextInteger(5);
    nextInteger(6);
    printf("\n\n");
   
    printf("Averages...\n");
	//TASK 4: PUT averageOfThree FUNCTION CALLS HERE
    printf("\n\n");
   
    printf("Squares...\n");
    shaggy_dog=0;
    squareOf(        shaggy_dog=nextInteger(shaggy_dog)       );
    squareOf(shaggy_dog=nextInteger(shaggy_dog));
    squareOf(shaggy_dog=nextInteger(shaggy_dog));
    squareOf(shaggy_dog=nextInteger(shaggy_dog));
    squareOf(shaggy_dog=nextInteger(shaggy_dog));
    printf("\n\n");
	
	printf("Fibonacci numbers...\n");
	shaggy_dog=nextFibonacci(1,1);
	scratchpad2=nextFibonacci(shaggy_dog,1);
   
	shaggy_dog=nextFibonacci(shaggy_dog, scratchpad2);
	scratchpad2=nextFibonacci(shaggy_dog, scratchpad2);
	shaggy_dog=nextFibonacci(shaggy_dog, scratchpad2);
	scratchpad2=nextFibonacci(shaggy_dog, scratchpad2);
	shaggy_dog=nextFibonacci(shaggy_dog, scratchpad2);
	scratchpad2=nextFibonacci(shaggy_dog, scratchpad2);
	printf("\n\n");
	
	printf("Ulam numbers...\n");
	shaggy_dog=nextUlam(5);
	scratchpad2=nextUlam(shaggy_dog);
   
	scratchpad2=nextUlam(scratchpad2);
	scratchpad2=nextUlam(scratchpad2);
	scratchpad2=nextUlam(scratchpad2);
	scratchpad2=nextUlam(scratchpad2);
	scratchpad2=nextUlam(scratchpad2);
	scratchpad2=nextUlam(scratchpad2);
	scratchpad2=nextUlam(scratchpad2);
	printf("\n\n");
   
	return 0;
}
예제 #2
0
파일: BitObject.C 프로젝트: binary42/avedac
// ######################################################################
void BitObject::computeSecondMoments()
{
  ASSERT(isValid());

  int w = itsObjectMask.getWidth();
  int h = itsObjectMask.getHeight();

  // The bounding box is stored in image coordinates, and so is the centroid. For
  // computing the second moments, however we need the centroid in object coords.
  float cenX = itsCentroidXY.x() - itsBoundingBox.left();
  float cenY = itsCentroidXY.y() - itsBoundingBox.top();

  // compute the second moments
  std::vector<float> diffX(w), diffX2(w), diffY(h), diffY2(h);
  for (int y = 0; y < h; ++y) 
    {
      diffY[y] = y - cenY;
      diffY2[y] = diffY[y] * diffY[y];
    }
  for (int x = 0; x < w; ++x) 
    {
      diffX[x] = x - cenX;
      diffX2[x] = diffX[x] * diffX[x];
    }

  Image<byte>::const_iterator optr = itsObjectMask.begin();
  for (int y = 0; y < h; ++y)
    for(int x = 0; x < w; ++x)
      {
        if (*optr != 0)
          {
            itsUxx += diffX2[x];
            itsUyy += diffY2[y];
            itsUxy += (diffX[x] * diffY[y]);
          }
        ++optr;
      }
  itsUxx /= itsArea; 
  itsUyy /= itsArea;
  itsUxy /= itsArea;

  // compute the parameters d, e and f for the ellipse:
  // d*x^2 + 2*e*x*y + f*y^2 <= 1
  float coeff = 0.F;
  if((itsUxx * itsUyy - itsUxy * itsUxy) > 0)
    coeff = 1 / (4 * (itsUxx * itsUyy - itsUxy * itsUxy));
  float d =  coeff * itsUyy;
  float e = -coeff * itsUxy;
  float f =  coeff * itsUxx;

  // from these guys, compute the parameters d and f for the
  // ellipse when it is rotated so that x is the main axis
  // and figure out the angle of rotation for this.
  float expr = sqrt(4*e*e + squareOf(d - f));
  float d2 = 0.5 * (d + f + expr);
  float f2 = 0.5 * (d + f - expr);

  // the angle is defined in clockwise (image) coordinates:
  //  --  is 0
  //  \   is 45
  //  |   is 90
  //  /   is 135
  if( d != f)  itsOriAngle = 90 * atan(2 * e / (d - f)) / M_PI;
  else	itsOriAngle = 0.F;
  if (itsUyy > itsUxx) itsOriAngle += 90.0F;
  if (itsOriAngle < 0.0F) itsOriAngle += 180.0F;

  // this checks if itsOriAngle is nan
  if (itsOriAngle != itsOriAngle) itsOriAngle = 0.0F;

  // now get the length of the major and the minor axes:
  if(f2) itsMajorAxis = 2 / sqrt(f2);
  if(d2) itsMinorAxis = 2 / sqrt(d2);
  if(itsMinorAxis) itsElongation = itsMajorAxis / itsMinorAxis;
  else itsElongation = 0.F;
  // We're done
  haveSecondMoments = true;
  return;
}