当前位置: 首页>>代码示例>>C++>>正文


C++ SGN函数代码示例

本文整理汇总了C++中SGN函数的典型用法代码示例。如果您正苦于以下问题:C++ SGN函数的具体用法?C++ SGN怎么用?C++ SGN使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了SGN函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: fillLine

/* static  */
void fillLine(point p, point q, PointSet * ps)
{
    int x1 = p.x;
    int y1 = p.y;
    int x2 = q.x;
    int y2 = q.y;
    int d, x, y, ax, ay, sx, sy, dx, dy;

    dx = x2 - x1;
    ax = ABS(dx) << 1;
    sx = SGN(dx);
    dy = y2 - y1;
    ay = ABS(dy) << 1;
    sy = SGN(dy);

/* fprintf (stderr, "fillLine %d %d - %d %d\n", x1,y1,x2,y2); */
    x = x1;
    y = y1;
    if (ax > ay) {		/* x dominant */
	d = ay - (ax >> 1);
	for (;;) {
/* fprintf (stderr, "  addPS %d %d\n", x,y); */
	    addPS(ps, x, y);
	    if (x == x2)
		return;
	    if (d >= 0) {
		y += sy;
		d -= ax;
	    }
	    x += sx;
	    d += ay;
	}
    } else {			/* y dominant */
开发者ID:Goettsch,项目名称:game-editor,代码行数:34,代码来源:pack.c

示例2: XORDrawAttachedLine

/*-----------------------------------------------------------
 * Draws the outline of a line
 */
static void
XORDrawAttachedLine (LocationType x1, LocationType y1, LocationType x2,
		     LocationType y2, BDimension thick)
{
  LocationType dx, dy, ox, oy;
  float h;

  dx = x2 - x1;
  dy = y2 - y1;
  if (dx != 0 || dy != 0)
    h = 0.5 * thick / sqrt (SQUARE (dx) + SQUARE (dy));
  else
    h = 0.0;
  ox = dy * h + 0.5 * SGN (dy);
  oy = -(dx * h + 0.5 * SGN (dx));
  gui->draw_line (Crosshair.GC, x1 + ox, y1 + oy, x2 + ox, y2 + oy);
  if (abs (ox) >= pixel_slop || abs (oy) >= pixel_slop)
    {
      LocationType angle = atan2 ((float) dx, (float) dy) * 57.295779;
      gui->draw_line (Crosshair.GC, x1 - ox, y1 - oy, x2 - ox, y2 - oy);
      gui->draw_arc (Crosshair.GC,
		     x1, y1, thick / 2, thick / 2, angle - 180, 180);
      gui->draw_arc (Crosshair.GC, x2, y2, thick / 2, thick / 2, angle, 180);
    }
}
开发者ID:thequux,项目名称:pcb,代码行数:28,代码来源:crosshair.c

示例3: search

/* modified lambda (mlambda) search (ref. [2]) -------------------------------*/
static int search(int n, int m, const double *L, const double *D,
                  const double *zs, double *zn, double *s)
{
    int i,j,k,c,nn=0,imax=0;
    double newdist,maxdist=1E99,y;
    double *S=zeros(n,n),*dist=mat(n,1),*zb=mat(n,1),*z=mat(n,1),*step=mat(n,1);
    
    k=n-1; dist[k]=0.0;
    zb[k]=zs[k];
    z[k]=ROUND(zb[k]); y=zb[k]-z[k]; step[k]=SGN(y);
    for (c=0;c<LOOPMAX;c++) {
        newdist=dist[k]+y*y/D[k];
        if (newdist<maxdist) {
            if (k!=0) {
                dist[--k]=newdist;
                for (i=0;i<=k;i++)
                    S[k+i*n]=S[k+1+i*n]+(z[k+1]-zb[k+1])*L[k+1+i*n];
                zb[k]=zs[k]+S[k+k*n];
                z[k]=ROUND(zb[k]); y=zb[k]-z[k]; step[k]=SGN(y);
            }
            else {
                if (nn<m) {
                    if (nn==0||newdist>s[imax]) imax=nn;
                    for (i=0;i<n;i++) zn[i+nn*n]=z[i];
                    s[nn++]=newdist;
                }
                else {
                    if (newdist<s[imax]) {
                        for (i=0;i<n;i++) zn[i+imax*n]=z[i];
                        s[imax]=newdist;
                        for (i=imax=0;i<m;i++) if (s[imax]<s[i]) imax=i;
                    }
                    maxdist=s[imax];
                }
                z[0]+=step[0]; y=zb[0]-z[0]; step[0]=-step[0]-SGN(step[0]);
            }
        }
        else {
            if (k==n-1) break;
            else {
                k++;
                z[k]+=step[k]; y=zb[k]-z[k]; step[k]=-step[k]-SGN(step[k]);
            }
        }
    }
    for (i=0;i<m-1;i++) { /* sort by s */
        for (j=i+1;j<m;j++) {
            if (s[i]<s[j]) continue;
            SWAP(s[i],s[j]);
            for (k=0;k<n;k++) SWAP(zn[k+i*n],zn[k+j*n]);
        }
    }
    free(S); free(dist); free(zb); free(z); free(step);
    
    if (c>=LOOPMAX) {
        fprintf(stderr,"%s : search loop count overflow\n",__FILE__);
        return -1;
    }
    return 0;
}
开发者ID:Andreas-Krimbacher,项目名称:rtklib,代码行数:61,代码来源:lambda.c

示例4: ABS

void
NWindowScreen::draw_line(int x1, int y1, int x2, int y2, int color)
{
	// Simple Bresenham's line drawing algorithm
	int d,x,y,ax,ay,sx,sy,dx,dy;
	
#define ABS(x) (((x)<0) ? -(x) : (x))
#define SGN(x) (((x)<0) ? -1 : 1)

	dx=x2-x1; ax=ABS(dx)<<1; sx=SGN(dx);
	dy=y2-y1; ay=ABS(dy)<<1; sy=SGN(dy);
	
	x=x1;
	y=y1;
	if(ax>ay)
	{
		d=ay-(ax>>1);
		for(;;)
		{
			set_pixel(x,y,color);
			if(x==x2) return;
			if(d>=0)
			{
				y+=sy;
				d-=ax;
			}
			x+=sx;
			d+=ay;
		}
	}
开发者ID:SummerSnail2014,项目名称:haiku,代码行数:30,代码来源:particlesII.cpp

示例5: draw_line

void draw_line(int c1, int r1, int c2, int r2) {
    uint8_t  *hud = hud_image+c1+r1*128;

    int dc=(c2-c1), dr=(r2-r1);
    int cs=SGN(dc), rs=SGN(dr)*128;

    int h = abs(dc) > abs(dr); // line is more horizontal
    int ml = h ? abs(dc) : abs(dr);
    int ol = h ? abs(dr) : abs(dc);
    int ms = h ? cs : rs;
    int os = h ? rs : cs;

    int state = ml/2;
    int pl = ml;
    while (pl>=0) {
        hud[0] = fg_color;
        hud+=ms;
        state -= ol;
        if (state < 0) {
            state += ml;
            hud+=os;
        }
        pl --;
    }
}
开发者ID:broese,项目名称:mcbuild,代码行数:25,代码来源:hud.c

示例6: PathIsClear

/*
 * PathIsClear:  Return True iff the path from p1 to p2 is clear, with these
 *   2 exceptions:
 *    1) p1 may contain a piece
 *    2) p2 may contain a piece NOT of the given color
 *   Assumes that p1 and p2 are connected by a horizontal, vertical, or diagonal
 *   line.
 */
Bool PathIsClear(Board *b, POINT p1, POINT p2, BYTE color)
{
   int dx, dy;
   int x, y;

   dx = SGN(p2.x - p1.x);
   dy = SGN(p2.y - p1.y);

   // Start at first square piece enters
   x = p1.x + dx;
   y = p1.y + dy;
   
   while (x != p2.x || y != p2.y)
   {
      if (b->squares[y][x].piece != NONE)
	 return False;
      x += dx;
      y += dy;
   }

   // p2 can't contain piece of same color
   if (b->squares[y][x].piece != NONE && b->squares[y][x].color == color)
      return False;
   
   return True;
}
开发者ID:AlleyCat1976,项目名称:Meridian59_103,代码行数:34,代码来源:cmove.c

示例7: check_one

void
check_one (const char *name, mpz_srcptr x, double y, int cmp, int cmpabs)
{
  int   got;

  got = mpz_cmp_d (x, y);
  if (SGN(got) != cmp)
    {
      unsigned i;
      printf    ("mpz_cmp_d wrong (from %s)\n", name);
      printf    ("  got  %d\n", got);
      printf    ("  want %d\n", cmp);
    fail:
      printf ("  x=");
      mpz_out_str (stdout, 10, x);
      printf    ("\n  y %g\n", y);
      printf ("  x=0x");
      mpz_out_str (stdout, -16, x);
      printf    ("\n  y %g\n", y);
      printf    ("  y");
      for (i = 0; i < sizeof(y); i++)
        printf (" %02X", (unsigned) ((unsigned char *) &y)[i]);
      printf ("\n");
      abort ();
    }

  got = mpz_cmpabs_d (x, y);
  if (SGN(got) != cmpabs)
    {
      printf    ("mpz_cmpabs_d wrong\n");
      printf    ("  got  %d\n", got);
      printf    ("  want %d\n", cmpabs);
      goto fail;
    }
}
开发者ID:ChristopherRussell,项目名称:gap,代码行数:35,代码来源:t-cmp_d.c

示例8: convex_hull_marching

void convex_hull_marching(Bezier src_bz, Bezier bz,
                          std::vector<double> &solutions,
                          double left_t,
                          double right_t)
{
    while(bz.order() > 0 and bz[0] == 0) {
        std::cout << "deflate\n";
        bz = bz.deflate();
        solutions.push_back(left_t);
    }
    if (bz.order() > 0) {
    
        int old_sign = SGN(bz[0]);
    
        int sign;
        double left_bound = 0;
        double dt = 0;
        for (size_t i = 1; i < bz.size(); i++)
        {
            sign = SGN(bz[i]);
            if (sign != old_sign)
            {
                dt = double(i) / bz.order();
                left_bound = dt * bz[0] / (bz[0] - bz[i]);
                break;
            }
            old_sign = sign;
        }
        if (dt == 0) return;
        std::cout << bz << std::endl;
        std::cout << "dt = " << dt << std::endl;
        std::cout << "left_t = " << left_t << std::endl;
        std::cout << "right_t = " << right_t << std::endl;
        std::cout << "left bound = " << left_bound 
                  << " = " << bz(left_bound) << std::endl; 
        double new_left_t = left_bound * (right_t - left_t) + left_t;
        std::cout << "new_left_t = " << new_left_t << std::endl;
        Bezier bzr = subRight(src_bz, new_left_t);
        while(bzr.order() > 0 and bzr[0] == 0) {
            std::cout << "deflate\n";
            bzr = bzr.deflate();
            solutions.push_back(new_left_t);
        }
        if (left_t < new_left_t) {
            convex_hull_marching(src_bz, bzr,
                                 solutions,
                                 new_left_t, right_t); 
        } else {
            std::cout << "epsilon reached\n";
            while(bzr.order() > 0 and fabs(bzr[0]) <= 1e-10) {
                std::cout << "deflate\n";
                bzr = bzr.deflate();
                std::cout << bzr << std::endl;
                solutions.push_back(new_left_t);
            }

        }
    }
}
开发者ID:Spin0za,项目名称:inkscape,代码行数:59,代码来源:solve-bezier.cpp

示例9: __plm_minmod

static inline double __plm_minmod(double ul, double u0, double ur)
{
  const double a = plm_theta * (u0 - ul);
  const double b =     0.5   * (ur - ul);
  const double c = plm_theta * (ur - u0);
  const double fabc[3] = { fabs(a), fabs(b), fabs(c) };
  return 0.25*fabs(SGN(a)+SGN(b))*(SGN(a)+SGN(c))*min3(fabc);
}
开发者ID:fmaymi,项目名称:ctf,代码行数:8,代码来源:weno.c

示例10: int_compare

/**
 * Compare intervals x and y. Intervals are sorted according to their
 * lower bound; ties are broken by considering also the upper
 * bound.
 */
static int int_compare( const struct interval* x, const struct interval* y, uint16_t current_dim )
{
    if ( x == y ) return 0;

    if ( x->lower[current_dim] != y->lower[current_dim])
	return SGN(x->lower[current_dim] - y->lower[current_dim]);
    else
	return SGN(x->upper[current_dim] - y->upper[current_dim]);
}
开发者ID:nicholasricci,项目名称:DDM_Framework,代码行数:14,代码来源:int_tree.c

示例11: line_to

//Trying to pull points out of a tripoint vector is messy and
//probably slow, so leaving two full functions for now
std::vector <point> line_to(const int x1, const int y1, const int x2, const int y2, int t)
{
    std::vector<point> ret;
    // Preallocate the number of cells we need instead of allocating them piecewise.
    const int numCells = square_dist(tripoint(x1, y1, 0),tripoint(x2, y2, 0));
    ret.reserve(numCells);
    const int dx = x2 - x1;
    const int dy = y2 - y1;

    point cur;
    cur.x = x1;
    cur.y = y1;

    // Draw point
    if (dx==0 && dy==0) {
      ret.push_back(cur);
      // Should exit here
      return ret;
    }

    // Any ideas why we're multiplying the abs distance by two here?
    const int ax = abs(dx) << 1; // bitshift one place, functional *2
    const int ay = abs(dy) << 1;
    const int sx = (dx == 0 ? 0 : SGN(dx)), sy = (dy == 0 ? 0 : SGN(dy));

    // The old version of this algorithm would generate points on the line and check min/max for each point
    // to determine whether or not to continue generating the line. Since we already know how many points
    // we need, this method saves us a half-dozen variables and a few calculations.
    if (ax == ay) {
        for (int i = 0; i < numCells; i++) {
            cur.y += sy;
            cur.x += sx;
            ret.push_back(cur);
        } ;
    } else if (ax > ay) {
        for (int i = 0; i < numCells; i++) {
            if (t > 0) {
                cur.y += sy;
                t -= ax;
            }
            cur.x += sx;
            t += ay;
            ret.push_back(cur);
        } ;
    } else {
        for (int i = 0; i < numCells; i++) {
            if (t > 0) {
                cur.x += sx;
                t -= ay;
            }
            cur.y += sy;
            t += ax;
            ret.push_back(cur);
        } ;
    }
    return ret;
}
开发者ID:Dogeturtle,项目名称:Cataclysm-DDA,代码行数:59,代码来源:line.cpp

示例12: line_to

std::vector <point> line_to(int x0, int y0, int x1, int y1)
{
 int t = 0;
 std::vector<point> ret;
 int dx = x1 - x0;
 int dy = y1 - y0;
 int ax = abs(dx)<<1;
 int ay = abs(dy)<<1;
 int sx = SGN(dx);
 int sy = SGN(dy);
 if (dy == 0) sy = 0;
 if (dx == 0) sx = 0;
 point cur;
 cur.x = x0;
 cur.y = y0;

 int xmin = (x0 < x1 ? x0 : x1), ymin = (y0 < y1 ? y0 : y1),
     xmax = (x0 > x1 ? x0 : x1), ymax = (y0 > y1 ? y0 : y1);

 xmin -= abs(dx);
 ymin -= abs(dy);
 xmax += abs(dx);
 ymax += abs(dy);

 if (ax == ay) {
  do {
   cur.y += sy;
   cur.x += sx;
   ret.push_back(cur);
  } while ((cur.x != x1 || cur.y != y1) &&
           (cur.x >= xmin && cur.x <= xmax && cur.y >= ymin && cur.y <= ymax));
 } else if (ax > ay) {
  do {
   if (t > 0) {
    cur.y += sy;
    t -= ax;
   }
   cur.x += sx;
   t += ay;
   ret.push_back(cur);
  } while ((cur.x != x1 || cur.y != y1) &&
           (cur.x >= xmin && cur.x <= xmax && cur.y >= ymin && cur.y <= ymax));
 } else {
  do {
   if (t > 0) {
    cur.x += sx;
    t -= ay;
   }
   cur.y += sy;
   t += ax;
   ret.push_back(cur);
  } while ((cur.x != x1 || cur.y != y1) &&
           (cur.x >= xmin && cur.x <= xmax && cur.y >= ymin && cur.y <= ymax));
 }
 return ret;
}
开发者ID:DrPlotDevice,项目名称:Cataclysm2,代码行数:56,代码来源:geometry.cpp

示例13: DPLL_add_binary_implications

int DPLL_add_binary_implications( int lit1, int lit2 )
{
	int i, *bImp;

	if( IS_FIXED(lit1) )
	{
	    if( !FIXED_ON_COMPLEMENT(lit1) )	return SAT;
	    else if( IS_FIXED(lit2) )	
		    return( !FIXED_ON_COMPLEMENT(lit2) );
	    else    return look_fix_binary_implications(lit2);
	}
	else if( IS_FIXED(lit2) )
	{
	    if( !FIXED_ON_COMPLEMENT(lit2) )	return SAT;
	    else    return look_fix_binary_implications(lit1);
	}
	
#ifdef BIEQ
	while( (VeqDepends[ NR(lit1) ] != INDEPENDENT) && 
	    (VeqDepends[ NR(lit1) ] != EQUIVALENT) )
		lit1 = VeqDepends[ NR(lit1) ] * SGN(lit1);

	while( (VeqDepends[ NR(lit2) ] != INDEPENDENT) && 
	    (VeqDepends[ NR(lit2) ] != EQUIVALENT) )
		lit2 = VeqDepends[ NR(lit2) ] * SGN(lit2);

	if( lit1 == -lit2 ) return SAT;
	if( lit1 ==  lit2 ) return look_fix_binary_implications(lit1);
#endif

	STAMP_IMPLICATIONS( -lit1 );
	if( bImp_stamps[ -lit2 ] == current_bImp_stamp )
	    return look_fix_binary_implications( lit1 );
	if( bImp_stamps[lit2] != current_bImp_stamp )
	{
	    int _result;

	    bImp_stamps[ BinaryImp[-lit1][ BinaryImp[-lit1][0] - 1] ] = current_bImp_stamp;

	    _result = DPLL_add_compensation_resolvents( lit1, lit2 );
	    if( _result != UNKNOWN ) 
		return _result;
	 
	    STAMP_IMPLICATIONS( -lit2 ); 
	    if( bImp_stamps[ -lit1 ] == current_bImp_stamp )
	    	return look_fix_binary_implications( lit2 );

	    _result = DPLL_add_compensation_resolvents( lit2, lit1 );
	    if( _result != UNKNOWN ) 
		return _result;

	    ADD_BINARY_IMPLICATIONS( lit1, lit2 );
	}
	return SAT;
}
开发者ID:xxyzzzq,项目名称:open-wbo,代码行数:55,代码来源:solver.c

示例14: line_to

std::vector <point> line_to(int x1, int y1, int x2, int y2, int t)
{
 std::vector<point> ret;
 int dx = x2 - x1;
 int dy = y2 - y1;
 int ax = abs(dx)<<1;
 int ay = abs(dy)<<1;
 int sx = SGN(dx);
 int sy = SGN(dy);
 if (dy == 0) sy = 0;
 if (dx == 0) sx = 0;
 point cur;
 cur.x = x1;
 cur.y = y1;

 int xmin = (x1 < x2 ? x1 : x2), ymin = (y1 < y2 ? y1 : y2),
     xmax = (x1 > x2 ? x1 : x2), ymax = (y1 > y2 ? y1 : y2);

 xmin -= abs(dx);
 ymin -= abs(dy);
 xmax += abs(dx);
 ymax += abs(dy);

 if (ax == ay) {
  do {
   cur.y += sy;
   cur.x += sx;
   ret.push_back(cur);
  } while ((cur.x != x2 || cur.y != y2) &&
           (cur.x >= xmin && cur.x <= xmax && cur.y >= ymin && cur.y <= ymax));
 } else if (ax > ay) {
  do {
   if (t > 0) {
    cur.y += sy;
    t -= ax;
   }
   cur.x += sx;
   t += ay;
   ret.push_back(cur);
  } while ((cur.x != x2 || cur.y != y2) &&
           (cur.x >= xmin && cur.x <= xmax && cur.y >= ymin && cur.y <= ymax));
 } else {
  do {
   if (t > 0) {
    cur.x += sx;
    t -= ay;
   }
   cur.y += sy;
   t += ax;
   ret.push_back(cur);
  } while ((cur.x != x2 || cur.y != y2) &&
           (cur.x >= xmin && cur.x <= xmax && cur.y >= ymin && cur.y <= ymax));
 }
 return ret;
}
开发者ID:Alpha16,项目名称:Cataclysm-DDA,代码行数:55,代码来源:line.cpp

示例15: AdjustTwoLine

/* ---------------------------------------------------------------------------
 *  adjusts the insert lines to make them 45 degrees as necessary
 */
void
AdjustTwoLine (int way)
{
  LocationType dx, dy;
  AttachedLineTypePtr line = &Crosshair.AttachedLine;

  if (Crosshair.AttachedLine.State == STATE_FIRST)
    return;
  /* don't draw outline when ctrl key is pressed */
  if (gui->control_is_pressed ())
    {
      line->draw = false;
      return;
    }
  else
    line->draw = true;
  if (TEST_FLAG (ALLDIRECTIONFLAG, PCB))
    {
      line->Point2.X = Crosshair.X;
      line->Point2.Y = Crosshair.Y;
      return;
    }
  /* swap the modes if shift is held down */
  if (gui->shift_is_pressed ())
    way = !way;
  dx = Crosshair.X - line->Point1.X;
  dy = Crosshair.Y - line->Point1.Y;
  if (!way)
    {
      if (abs (dx) > abs (dy))
	{
	  line->Point2.X = Crosshair.X - SGN (dx) * abs (dy);
	  line->Point2.Y = line->Point1.Y;
	}
      else
	{
	  line->Point2.X = line->Point1.X;
	  line->Point2.Y = Crosshair.Y - SGN (dy) * abs (dx);
	}
    }
  else
    {
      if (abs (dx) > abs (dy))
	{
	  line->Point2.X = line->Point1.X + SGN (dx) * abs (dy);
	  line->Point2.Y = Crosshair.Y;
	}
      else
	{
	  line->Point2.X = Crosshair.X;
	  line->Point2.Y = line->Point1.Y + SGN (dy) * abs (dx);;
	}
    }
}
开发者ID:bert,项目名称:pcb-update,代码行数:57,代码来源:line.c


注:本文中的SGN函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。