#include "Main.h"
#include "Util.h"


/* **********************************************************************
 * struct Particle *NewParticle(int number)
 *   allocate number particles
 * **********************************************************************/
Particle *
NewParticle(int number) {
  return (Particle *) Malloc(sizeof(Particle)*number);
}


/* **********************************************************************
 * void InitParticle(struct Particle *p, int id, int mass,
 *                   int posX, int posY, int velX, int velY, 
 *                   int accX, int accY)
 *   initialize the particle with the given parameters
 * **********************************************************************/
void
InitParticle(Particle *p,
	     int id, int mass, 
	     float posX, float posY, float velX, float velY, 
	     float accX, float accY) {
  p->id = id;
  p->mass = mass;
  p->info[ePos].x = posX;
  p->info[ePos].y = posY;
  p->info[eVel].x = velX;
  p->info[eVel].y = velY;
  p->info[eAcc].x = accX;
  p->info[eAcc].y = accY;
}

/* **********************************************************************
 * void RandInitParticle(struct Particle *p, int id, int mass,
 *                   int posX, int posY, int velX, int velY, 
 *                   int accX, int accY)
 *   randomly initialize the particle with the given parameters
 * **********************************************************************/
void
RandInitParticle(Particle *p,
	     int id, float minX, float maxX, float minY, float maxY) {
  float xRange, yRange;

  xRange = maxX - minX;
  yRange = maxY - minY;
  p->id = id;
  p->mass = (lrand48() % 10 + 1) * 100;
  p->info[ePos].x = RandFrac()*xRange + minX;
  p->info[ePos].y = RandFrac()*yRange + minY;
  p->info[eVel].x = 0;
  p->info[eVel].y = 0;
  p->info[eAcc].x = 0;
  p->info[eAcc].y = 0;
}


/* **********************************************************************
 * void AssignParticle(struct Particle *pLeft, struct Particle *pRight)
 *   assign the right hand side particle to the left hand side
 * **********************************************************************/
void 
AssignParticle(Particle *pLeft, Particle *pRight) {
  InitParticle(pLeft,
	       pRight->id, pRight->mass,
	       pRight->info[ePos].x, pRight->info[ePos].y,
	       pRight->info[eVel].x, pRight->info[eVel].y,
	       pRight->info[eAcc].x, pRight->info[eAcc].y);
}


/* **********************************************************************
 * void PrintParticle(struct Particle *p)
 *   print the fields of a particle
 * **********************************************************************/
void
PrintParticle(Particle *p) {
  printf("pId: %d m: %d X: %f Y: %f vX: %f vY: %f aX: %f aY: %f\n",
	 p->id, p->mass, p->info[ePos].x, p->info[ePos].y,
	 p->info[eVel].x, p->info[eVel].y, p->info[eAcc].x, p->info[eAcc].y);
}
