示例#1
0
 void letItFall_worker(const P3& new_point, const P3** p0, const P3** p1, const P3** p2, bool *changed, V3 *N) {
   if (PtIsBelowPlane(**p0, **p1, **p2, *N, new_point)) {
     bool z0 = inDaZone(**p1, **p2, new_point);
     bool z1 = inDaZone(**p0, **p2, new_point);
     bool z2 = inDaZone(**p0, **p1, new_point);
     
     if (z2 && !colinear(**p0, **p1, P3::zero)) {
       *p2 = &new_point;
     } else if (z1 && !colinear(**p0, **p2, P3::zero)) {
       *p1 = &new_point;
     } else if (z0 && !colinear(**p1, **p2, P3::zero)) {
       *p0 = &new_point;
     } else {
       if (z2) {
         *p2 = &new_point;
       } else if (z1) {
         *p1 = &new_point;
       } else if (z0) {
         *p0 = &new_point;
       } else {
         return;
       }
     }
     *changed = true;
     *N = planeNormalFrom3Points(**p0, **p1, **p2);
   }
 }
示例#2
0
文件: 143.cpp 项目: waiwai444/oj
inline bool on_edge(Point e1, Point e2, Point p)
{
	if(e1.x < e2.x)
		return colinear(e1, e2, p) && e1.x <= p.x && p.x <= e2.x;
	else if(e1.x > e2.x)
		return colinear(e1, e2, p) && e2.x <= p.x && p.x <= e1.x;
	else if(e1.y < e2.y)
		return colinear(e1, e2, p) && e1.y <= p.y && p.y <= e2.y;
	else
		return colinear(e1, e2, p) && e2.y <= p.y && p.y <= e1.y;
}
示例#3
0
 bool findFeasiblePt(const P3* ptArray, int n, const P3& p0, const P3& p1, const P3** p2) {
   for (int i=0; i<n; i++) {
     if (inDaZone(p0, p1, ptArray[i]) && !colinear(p0, p1, ptArray[i])) {
       *p2 = &ptArray[i];
       return true;
     }
   }
   return false;
 }
示例#4
0
	void chase(glm::mat4 tar)	{
		float newRadian;
	//	showMat4("Chase target: ", tar);
		//get the foward vector of the missile
		glm::vec3 missileForwardVec = getIn(getOrientationMatrix());

		//get the distance vector between the missile and the target
		glm::vec3 distanceVec = getPosition(tar) - getMissilePosition();

		//normalizethe missile's forward and distance vector
		glm::vec3 targetNormal = glm::normalize(distanceVec);
		glm::vec3 missileNormal = glm::normalize(missileForwardVec);

		//showVec3("Target", targetNormal);
	//	showVec3("Missile", missileNormal);

		if (!(colinear(targetNormal, missileNormal, 0.1f)) )	{
			//showVec3("distance Vec Normal",targetNormal);
			//gets the cross product to find the axis of rotation
			//glm::vec3 axisOfRotation = glm::cross(targetNormal, missileNormal);
			glm::vec3 axisOfRotation = glm::cross(missileNormal, targetNormal);
			axisOfRotation = glm::normalize(axisOfRotation);

			//gets the axis of rotation's direction
			float axisOfRotationDirection = axisOfRotation.x + axisOfRotation.y + axisOfRotation.z;
			//printf("AOR-Direction: %f\n",axisOfRotationDirection);

			// get the dot product to find the rotation amount
			float totalRotation = glm::dot(missileNormal, targetNormal); 
			//float totalRotation = glm::dot(targetNormal, missileNormal);
			// get the length of the missile and target's normal
			float LOMN = glm::abs(distance(missileNormal, glm::vec3(0, 0, 0)));
			float LOTN = glm::abs(distance(targetNormal, glm::vec3(0, 0, 0)));


			//float totalRotation = glm::acos(glm::dot(targetNormal, missileNormal));
			//printf("Total Rotation: %f\n", totalRotation);
			//printf("AOR Direction: %f\n", axisOfRotationDirection);
			//check for the direction in which the missile is suppose to move in
			if (axisOfRotationDirection >= 0.01)
				newRadian = glm::acos(totalRotation);
			else
				newRadian = - glm::acos(totalRotation);

			rotationMatrix = glm::rotate(identity, (newRadian / 4.0f), axisOfRotation);
		}
		else
		{
			rotationMatrix = identity;
			//printf("colinear\n");
		}
			

	}
示例#5
0
int main (void){
	int cnum, couche, cplr, i, j, col_num, x, y, ang, dist, thrown[2];
	double sin_table[361], cos_table[361], min_dist;
	double conv = acos(-1)/180;
	bool dbg = 0;
	for(i = 0; i < 361; i++) sin_table[i] = sin(i*conv), cos_table[i] = cos(i*conv);
	scanf("%d",&cnum);
	while(cnum--){
		cplr = couche = thrown[0] = thrown[1] = 0;
		scanf("%s%s",n[0],n[1]);
		for(i = 0; i < 7; i++){
			if(i && ++thrown[cplr] > 3) cplr = 1-cplr;
			scanf("%d%d%d%d",&x,&y,&ang,&dist);
			start = Ball(x,y,cplr,0);
			b[i] = Ball(x+cos_table[ang]*dist,y+sin_table[ang]*dist,cplr,i);
			for(col_num = j = 0; j < i; j++)
				if(colinear(start,b[j],b[i]))
					col[col_num++] = Ball(b[j].x,b[j].y,b[j].plr,b[j].n);
			std::sort(col,col+col_num,closerToStart);
			for(j = 0; j < col_num; j++){
				int aux = col[j].plr;
				b[col[j].n].plr = b[i].plr;
				b[i].plr = aux;
			}
			min_dist = 1<<23;
			for(j = 0; j <= i; j++)
				if(b[j].plr == 2){ couche = j; break;}
			for(j = 0; j <= i; j++){
				if(j == couche) continue;
				double temp = sqdist(b[j],b[couche]);
				if(temp < min_dist){
					min_dist = temp;
					cplr = 1 - b[j].plr;
				}
			}
			if(i == 0) b[i].plr = 2;
		}
		for(i = 0; i < 7; i++)
			if(b[i].plr == 2){
				start = Ball(b[i].x,b[i].y,2,0);
				break;
			}
		std::sort(b,b+7,closerToStart);
		if(b[1].plr == 2) std::swap(b[0],b[1]);
		for(i = 2; b[i].plr == b[1].plr; i++);
		printf("%s %d\n",n[b[1].plr],i-1);
	}
	return 0;
}
示例#6
0
文件: Pgen137.cpp 项目: LasseD/uva
  ConvexHull(const Polygon &scatteredPoints) {
    const int numPoints = scatteredPoints.size();
    Polygon copy(scatteredPoints);
    // Sort input by x (lex):
    std::sort(&copy[0], &copy[numPoints]);

    // Find left and rightmost points:
    Point leftMost = copy[0];
    Point rightMost = copy[numPoints-1];

    //Split points in upper and lower:
    Polygon upperPoints;
    Polygon lowerPoints;
    for(int i = 1; i < numPoints-1; ++i) {			
      Point p = copy[i];
      if(colinear(leftMost, rightMost, p)) {
	continue;
      }
      if(leftTurn(leftMost, rightMost, p)) {
	upperPoints.push_back(p);
      }
      else {
	lowerPoints.push_back(flipY(p));
      }
    }

    upperPoints = upperHull(leftMost, rightMost, upperPoints);
    lowerPoints = upperHull(flipY(leftMost), flipY(rightMost), lowerPoints);
    
    //Make points of hull:
    int actualNumPoints = size = upperPoints.size() + lowerPoints.size();
    points = new Point[actualNumPoints];
    int i = 0;
    for(Polygon::const_iterator it = upperPoints.begin(); it != upperPoints.end(); ++it) {
      points[i++] = *it;
    }		
    points[i] = rightMost;
    i = actualNumPoints;
    for(Polygon::const_iterator it = lowerPoints.begin(); it != lowerPoints.end(); ++it) {
      i--;
      if(i != actualNumPoints-1)
	points[i+1] = flipY(*it);
    }
    std::cout << actualNumPoints;
    for(i = 0; i < actualNumPoints; ++i)
      std::cout << " " << points[i].first << " " << points[i].second;
    std::cout << std::endl;
  }
示例#7
0
 bool inDaZone(const P3& p0, const P3& p1, const P3& p2) {
   double a = filtered_det2(p0.coord, p1.coord);
   double b = filtered_det2(p1.coord, p2.coord);
   double c = filtered_det2(p2.coord, p0.coord);
   return ((a >= 0 && b >= 0 && c >= 0) || (a <= 0 && b <= 0 && c <= 0)) && !colinear(p0, p1, p2);
 }
int RescaleLayerTestProbe::outputState(double timed)
{
   int status = StatsProbe::outputState(timed);
   if (timed==getParent()->getStartTime()) { return PV_SUCCESS; }
   float tolerance = 2.0e-5f;
   InterColComm * icComm = getTargetLayer()->getParent()->icCommunicator();
   bool isRoot = icComm->commRank() == 0;

   RescaleLayer * targetRescaleLayer = dynamic_cast<RescaleLayer *>(getTargetLayer());
   assert(targetRescaleLayer);

   if (targetRescaleLayer->getRescaleMethod()==NULL) {
      fprintf(stderr, "RescaleLayerTestProbe \"%s\": RescaleLayer \"%s\" does not have rescaleMethod set.  Exiting.\n", name, targetRescaleLayer->getName());
      status = PV_FAILURE;
   }
   else if (!strcmp(targetRescaleLayer->getRescaleMethod(), "maxmin")) {
      if (!isRoot) { return PV_SUCCESS; }
      for(int b = 0; b < parent->getNBatch(); b++){
         float targetMax = targetRescaleLayer->getTargetMax();
         if (fabs(fMax[b]-targetMax)>tolerance) {
            fprintf(stderr, "RescaleLayerTestProbe \"%s\": RescaleLayer \"%s\" has max %f instead of target max %f\n", getName(), targetRescaleLayer->getName(), fMax[b], targetMax);
            status = PV_FAILURE;
         }
         float targetMin = targetRescaleLayer->getTargetMin();
         if (fabs(fMin[b]-targetMin)>tolerance) {
            fprintf(stderr, "RescaleLayerTestProbe \"%s\": RescaleLayer \"%s\" has min %f instead of target min %f\n", getName(), targetRescaleLayer->getName(), fMin[b], targetMin);
            status = PV_FAILURE;
         }

         // Now, check whether rescaled activity and original V are colinear.
         PVLayerLoc const * rescaleLoc = targetRescaleLayer->getLayerLoc();
         PVHalo const * rescaleHalo = &rescaleLoc->halo;
         int nk = rescaleLoc->nx * rescaleLoc->nf;
         int ny = rescaleLoc->ny;
         int rescaleStrideYExtended = (rescaleLoc->nx + rescaleHalo->lt + rescaleHalo->rt) * rescaleLoc->nf;
         int rescaleExtendedOffset = kIndexExtended(0, rescaleLoc->nx, rescaleLoc->ny, rescaleLoc->nf, rescaleHalo->lt, rescaleHalo->rt, rescaleHalo->dn, rescaleHalo->up);
         pvadata_t const * rescaledData = targetRescaleLayer->getLayerData() + b * targetRescaleLayer->getNumExtended() + rescaleExtendedOffset;
         PVLayerLoc const * origLoc = targetRescaleLayer->getOriginalLayer()->getLayerLoc();
         PVHalo const * origHalo = &origLoc->halo;
         assert(nk == origLoc->nx * origLoc->nf);
         assert(ny == origLoc->ny);
         int origStrideYExtended = (origLoc->nx + origHalo->lt + origHalo->rt) * origLoc->nf;
         int origExtendedOffset = kIndexExtended(0, rescaleLoc->nx, rescaleLoc->ny, rescaleLoc->nf, rescaleHalo->lt, rescaleHalo->rt, rescaleHalo->dn, rescaleHalo->up);
         pvadata_t const * origData = targetRescaleLayer->getOriginalLayer()->getLayerData() + b * targetRescaleLayer->getOriginalLayer()->getNumExtended() + origExtendedOffset;

         bool iscolinear = colinear(nk, ny, origStrideYExtended, rescaleStrideYExtended, origData, rescaledData, tolerance, NULL, NULL, NULL);
         if (!iscolinear) {
            fprintf(stderr, "RescaleLayerTestProbe \"%s\": Rescale layer \"%s\" data is not a linear rescaling of original membrane potential.\n", getName(), targetRescaleLayer->getName());
            status = PV_FAILURE;
         }
      }
   }
   //l2 norm with a patch size of 1 (default) should be the same as rescaling with meanstd with target mean 0 and std of 1/sqrt(patchsize)
   else if (!strcmp(targetRescaleLayer->getRescaleMethod(), "meanstd") || !strcmp(targetRescaleLayer->getRescaleMethod(), "l2")) {
      if (!isRoot) { return PV_SUCCESS; }
      for(int b = 0; b < parent->getNBatch(); b++){
         float targetMean, targetStd;
         if(!strcmp(targetRescaleLayer->getRescaleMethod(), "meanstd")){
            targetMean = targetRescaleLayer->getTargetMean();
            targetStd = targetRescaleLayer->getTargetStd();
         }
         else{
            targetMean = 0;
            targetStd = 1/sqrt((float)targetRescaleLayer->getL2PatchSize());
         }

         if (fabs(avg[b]-targetMean)>tolerance) {
            fprintf(stderr, "RescaleLayerTestProbe \"%s\": RescaleLayer \"%s\" has mean %f instead of target mean %f\n", getName(), targetRescaleLayer->getName(), (double)avg[b], targetMean);
            status = PV_FAILURE;
         }
         if (sigma[b]>tolerance && fabs(sigma[b]-targetStd)>tolerance) {
            fprintf(stderr, "RescaleLayerTestProbe \"%s\": RescaleLayer \"%s\" has std.dev. %f instead of target std.dev. %f\n", getName(), targetRescaleLayer->getName(), (double)sigma[b], targetStd);
            status = PV_FAILURE;
         }

         // Now, check whether rescaled activity and original V are colinear.
         PVLayerLoc const * rescaleLoc = targetRescaleLayer->getLayerLoc();
         PVHalo const * rescaleHalo = &rescaleLoc->halo;
         int nk = rescaleLoc->nx * rescaleLoc->nf;
         int ny = rescaleLoc->ny;
         int rescaleStrideYExtended = (rescaleLoc->nx + rescaleHalo->lt + rescaleHalo->rt) * rescaleLoc->nf;
         int rescaleExtendedOffset = kIndexExtended(0, rescaleLoc->nx, rescaleLoc->ny, rescaleLoc->nf, rescaleHalo->lt, rescaleHalo->rt, rescaleHalo->dn, rescaleHalo->up);
         pvadata_t const * rescaledData = targetRescaleLayer->getLayerData() + b*targetRescaleLayer->getNumExtended() + rescaleExtendedOffset;
         PVLayerLoc const * origLoc = targetRescaleLayer->getOriginalLayer()->getLayerLoc();
         PVHalo const * origHalo = &origLoc->halo;
         assert(nk == origLoc->nx * origLoc->nf);
         assert(ny == origLoc->ny);
         int origStrideYExtended = (origLoc->nx + origHalo->lt + origHalo->rt) * origLoc->nf;
         int origExtendedOffset = kIndexExtended(0, rescaleLoc->nx, rescaleLoc->ny, rescaleLoc->nf, rescaleHalo->lt, rescaleHalo->rt, rescaleHalo->dn, rescaleHalo->up);
         pvadata_t const * origData = targetRescaleLayer->getOriginalLayer()->getLayerData() + b*targetRescaleLayer->getOriginalLayer()->getNumExtended() + origExtendedOffset;

         bool iscolinear = colinear(nk, ny, origStrideYExtended, rescaleStrideYExtended, origData, rescaledData, tolerance, NULL, NULL, NULL);
         if (!iscolinear) {
            fprintf(stderr, "RescaleLayerTestProbe \"%s\": Rescale layer \"%s\" data is not a linear rescaling of original membrane potential.\n", getName(), targetRescaleLayer->getName());
            status = PV_FAILURE;
         }
      }
   }
   else if (!strcmp(targetRescaleLayer->getRescaleMethod(), "pointmeanstd")) {
      PVLayerLoc const * loc = targetRescaleLayer->getLayerLoc();
      int nf = loc->nf;
      if (nf<2) { return PV_SUCCESS; }
      PVHalo const * halo = &loc->halo;
      float targetMean = targetRescaleLayer->getTargetMean();
      float targetStd = targetRescaleLayer->getTargetStd();
      int numNeurons = targetRescaleLayer->getNumNeurons();
      for(int b = 0; b < parent->getNBatch(); b++){
         pvpotentialdata_t const * originalData = targetRescaleLayer->getV() + b*targetRescaleLayer->getNumNeurons();
         pvadata_t const * rescaledData = targetRescaleLayer->getLayerData() + b*targetRescaleLayer->getNumExtended();
         for (int k=0; k<numNeurons; k+=nf) {
            int kExtended = kIndexExtended(k, loc->nx, loc->ny, loc->nf, halo->lt, halo->rt, halo->dn, halo->up);
            double pointmean = 0.0;
            for (int f=0; f<nf; f++) {
               pointmean += rescaledData[kExtended+f];
            }
            pointmean /= nf;
            double pointstd = 0.0;
            for (int f=0; f<nf; f++) {
               double d = rescaledData[kExtended+f]-pointmean;
               pointstd += d*d;
            }
            pointstd /= nf;
            pointstd = sqrt(pointstd);
            if (fabs(pointmean-targetMean)>tolerance) {
               fprintf(stderr, "RescaleLayerTestProbe \"%s\": RescaleLayer \"%s\", location in rank %d, starting at restricted neuron %d, has mean %f instead of target mean %f\n",
                     getName(), targetRescaleLayer->getName(), getParent()->columnId(), k, pointmean, targetMean);
               status = PV_FAILURE;
            }
            if (pointstd>tolerance && fabs(pointstd-targetStd)>tolerance) {
               fprintf(stderr, "RescaleLayerTestProbe \"%s\": RescaleLayer \"%s\", location in rank %d, starting at restricted neuron %d, has std.dev. %f instead of target std.dev. %f\n",
                     getName(), targetRescaleLayer->getName(), getParent()->columnId(), k, pointstd, targetStd);
               status = PV_FAILURE;
            }
            bool iscolinear = colinear(nf, 1, 0, 0, &originalData[k], &rescaledData[kExtended], tolerance, NULL, NULL, NULL);
            if (!iscolinear) {
               fprintf(stderr, "RescaleLayerTestProbe \"%s\": RescaleLayer \"%s\", location in rank %d, starting at restricted neuron %d, is not a linear rescaling.\n",
                     getName(), targetRescaleLayer->getName(), parent->columnId(), k);
               status = PV_FAILURE;
            }
         }
      }
   }
   else if (!strcmp(targetRescaleLayer->getRescaleMethod(), "zerotonegative")) {
      int numNeurons = targetRescaleLayer->getNumNeurons();
      assert(numNeurons == targetRescaleLayer->getOriginalLayer()->getNumNeurons());
      PVLayerLoc const * rescaleLoc = targetRescaleLayer->getLayerLoc();
      PVHalo const * rescaleHalo = &rescaleLoc->halo;
      int nf = rescaleLoc->nf;
      HyPerLayer * originalLayer = targetRescaleLayer->getOriginalLayer();
      PVLayerLoc const * origLoc = originalLayer->getLayerLoc();
      PVHalo const * origHalo = &origLoc->halo;
      assert(origLoc->nf == nf);

      for(int b = 0; b < parent->getNBatch(); b++){
         pvadata_t const * rescaledData = targetRescaleLayer->getLayerData() + b * targetRescaleLayer->getNumExtended();
         pvadata_t const * originalData = originalLayer->getLayerData() + b * originalLayer->getNumExtended();
         for (int k=0; k<numNeurons; k++) {
            int rescale_kExtended = kIndexExtended(k, rescaleLoc->nx, rescaleLoc->ny, rescaleLoc->nf, rescaleHalo->lt, rescaleHalo->rt, rescaleHalo->dn, rescaleHalo->up);
            int orig_kExtended = kIndexExtended(k, origLoc->nx, origLoc->ny, origLoc->nf, origHalo->lt, origHalo->rt, origHalo->dn, origHalo->up);
            pvadata_t observedval = rescaledData[rescale_kExtended];
            pvpotentialdata_t correctval = originalData[orig_kExtended] ? observedval : -1.0;
            if (observedval != correctval) {
               fprintf(stderr, "RescaleLayerTestProbe \"%s\": RescaleLayer \"%s\", rank %d, restricted neuron %d has value %f instead of expected %f\n.",
                     this->getName(), targetRescaleLayer->getName(), parent->columnId(), k, observedval, correctval);
               status = PV_FAILURE;
            }
         }
      }
   }
   else {
      assert(0);  // All allowable rescaleMethod values are handled above.
   }
   if (status == PV_FAILURE) {
      exit(EXIT_FAILURE);
   }
   return status;
}