void drawLine(int x1, int y1, int x2, int y2, int color) { float u, s, v, d1x, d1y, d2x, d2y, m, n; int x = x1, y = y1; u = x2-x1; v = y2-y1; d1x = d2x = _sign(u); d1y = _sign(v); d2y = 0; m = abs(u); n = abs(v); if (m <= n) { d2x = 0; d2y = _sign(v); m = abs(v); n = abs(u); } s = (int)(m/2); for (int i=0; i<round(m); i++) { drawPixel(x, y, color); s += n; if (s >= m) { s -= m; x += d1x; y += d1y; } else { x += d2x; y += d2y; } } }
/* immediate */ static int _immediate(State * state) /* [ sign ] "$" NUMBER */ { int ret = 0; AsmArchOperand * p; char const * string; uint64_t value = 0; #ifdef DEBUG fprintf(stderr, "DEBUG: %s()\n", __func__); #endif /* [ sign ] */ if(_parser_in_set(state, TS_SIGN)) ret |= _sign(state); else state->negative = 0; /* "$" NUMBER */ p = &state->call.operands[state->call.operands_cnt]; if((string = token_get_string(state->token)) != NULL || strlen(string) == 0) value = strtoul(string + 1, NULL, 0); else ret = _parser_error(state, "%s", "Empty values are not" " allowed"); if(state->address > 0) p->value.dregister.offset = value; else { p->value.immediate.value = strtoul(string + 1, NULL, 0); p->definition = AO_IMMEDIATE(0, 0, 0); p->value.immediate.negative = state->negative; } return ret | _parser_scan(state); }
void CAlgorithmBresenham::_init() { int nTmp; m_curPoint = m_startPoint; m_nDx = qAbs(m_endPoint.x() - m_startPoint.x()); m_nDy = qAbs(m_endPoint.y() - m_startPoint.y()); m_nS1 = _sign(m_endPoint.x() - m_startPoint.x()); m_nS2 = _sign(m_endPoint.y() - m_startPoint.y()); if(m_nDx < m_nDy) { nTmp = m_nDx; m_nDx = m_nDy; m_nDy = nTmp; m_nSwap = 1; } else { m_nSwap = 0; } m_nError = 2*m_nDy - m_nDx; // m_nError = 0; m_bFirstStep = true; m_nCurStep = 0; m_sInitInfo.clear(); m_sInitInfo.append("Algorithm Bresenham. Initialize info\n"); m_sInitInfo.append(QString("Start point (%1,%2)\n").arg(QString::number(m_startPoint.x())).arg(QString::number(m_startPoint.y()))); m_sInitInfo.append(QString("End point (%1,%2)\n").arg(QString::number(m_endPoint.x())).arg(QString::number(m_endPoint.y()))); m_sInitInfo.append(QString("dx = %1\n").arg(QString::number(m_nDx))); m_sInitInfo.append(QString("dy = %1\n").arg(QString::number(m_nDy))); m_sInitInfo.append(QString("swap = %1\n").arg(QString::number(m_nSwap))); m_sInitInfo.append(QString("error = %1").arg(QString::number(m_nError))); }
/* address */ static int _address(State * state) /* "[" [ space ] [ sign [ space ] ] value [ space ] [ offset [ space ] ] "]" */ { int ret; #ifdef DEBUG fprintf(stderr, "DEBUG: %s()\n", __func__); #endif state->address = 1; /* "[" */ ret = _parser_scan(state); /* [ space ] */ if(_parser_in_set(state, TS_SPACE)) ret |= _space(state); /* [ sign [ space ] ] */ if(_parser_in_set(state, TS_SIGN)) { ret |= _sign(state); if(_parser_in_set(state, TS_SPACE)) ret |= _space(state); } else state->negative = 0; /* value */ if(_parser_in_set(state, TS_VALUE)) ret |= _value(state); else ret |= _parser_error(state, "%s", "Expected value"); /* [ space ] */ if(_parser_in_set(state, TS_SPACE)) ret |= _space(state); /* [ offset [ space ] ] */ if(_parser_in_set(state, TS_OFFSET)) { state->address = 2; ret |= _offset(state); if(_parser_in_set(state, TS_SPACE)) ret |= _space(state); } state->address = 0; /* "]" */ return ret | _parser_check(state, AS_CODE_OPERATOR_RBRACKET); }
/* offset */ static int _offset(State * state) /* sign [ space ] value */ { int ret; #ifdef DEBUG fprintf(stderr, "DEBUG: %s()\n", __func__); #endif /* sign */ ret = _sign(state); /* [ space ] */ if(_parser_in_set(state, TS_SPACE)) ret |= _space(state); /* value */ if(_parser_in_set(state, TS_VALUE)) ret |= _value(state); else ret |= _parser_error(state, "%s", "Expected a value"); return ret; }
//判点在凸多边形内,顶点按顺时针或逆时针给出,在多边形边上返回0 int inside_convex_v2(point q,int n,point* p){ int i,s[3]={1,1,1}; for (i=0;i<n&&s[0]&&s[1]|s[2];i++) s[_sign(xmult(p[(i+1)%n],q,p[i]))]=0; return s[0]&&s[1]|s[2]; }
//判定凸多边形,顶点按顺时针或逆时针给出,允许相邻边共线 int is_convex(int n,point* p){ int i,s[3]={1,1,1}; for (i=0;i<n&&s[1]|s[2];i++) s[_sign(xmult(p[(i+1)%n],p[(i+2)%n],p[i]))]=0; return s[1]|s[2]; }
floatComplex catans(floatComplex z) { static float sSlim = 0.2f; /* . ** / \ WARNING : this algorithm was based on double precision ** / ! \ using float truncate the value to 0. ** `----' ** ** static float sAlim = 1E-150f; */ static float sAlim = 0.0f; static float sTol = 0.3f; static float sLn2 = 0.6931471805599453094172321f; float RMax = (float) getOverflowThreshold(); float Pi_2 = 2.0f * satans(1); float _inReal = creals(z); float _inImg = cimags(z); float _outReal = 0; float _outImg = 0; /* Temporary variables */ float R2 = 0; float S = 0; if(_inImg == 0) { _outReal = satans(_inReal); _outImg = 0; } else { R2 = _inReal * _inReal + _inImg * _inImg; /* Oo */ if(R2 > RMax) { if( dabss(_inImg) > RMax) S = 0; else S = 1.0f / (((0.5f * _inReal) / _inImg) * _inReal + 0.5f * _inImg ); } else S = (2 * _inImg) / (1+R2); if(dabss(S) < sSlim) { /* s is small: |s| < SLIM <=> |z| outside the following disks: D+ = D(center = [0; 1/slim], radius = sqrt(1/slim**2 - 1)) if b > 0 D- = D(center = [0; -1/slim], radius = sqrt(1/slim**2 - 1)) if b < 0 use the special evaluation of log((1+s)/(1-s)) (5) */ _outImg = slnp1m1s(S) * 0.25f; } else { if(sabss(S) == 1 && sabss(_inReal) <= sAlim) { /* |s| >= SLIM => |z| is inside D+ or D- */ _outImg = _sign(0.5f,_inImg) * ( sLn2 - logf(sabss(_inReal))); } else { _outImg = 0.25f * logf((powf(_inReal,2) + powf((_inImg + 1.0f),2)) / (powf(_inReal,2) + powf((_inImg - 1.0f),2))); } } if(_inReal == 0) {/* z is purely imaginary */ if( dabss(_inImg) > 1) {/* got sign(b) * pi/2 */ _outReal = _sign(1, _inImg) * Pi_2; } else if( dabss(_inImg) == 1) {/* got a Nan with 0/0 */ _outReal = (_inReal - _inReal) / (_inReal - _inReal); /* Oo */ } else _outReal = 0; } else if(R2 > RMax) {/* _outImg is necessarily very near sign(a)* pi/2 */ _outReal = _sign(1, _inReal) * Pi_2; } else if(sabss(1 - R2) + sabss(_inReal) <= sTol) {/* |b| is very near 1 (and a is near 0) some cancellation occur in the (next) generic formula */ _outReal = 0.5f * atan2f(2.0f * _inReal, (1.0f - _inImg) * (1.0f + _inImg) - powf(_inReal,2.0f)); } else _outReal = 0.5f * atan2f(2.0f * _inReal, 1.0f - R2); } return FloatComplex(_outReal, _outImg); }
floatComplex csqrts(floatComplex in) { float RMax = (float) getOverflowThreshold(); float BRMin = 2.0f * (float) getUnderflowThreshold(); float RealIn = creals(in); float ImgIn = cimags(in); float RealOut = 0; float ImgOut = 0; if(RealIn == 0) {/* pure imaginary case */ if(dabss(ImgIn >= BRMin)) RealOut = ssqrts(0.5f * sabss(ImgIn)); else RealOut = ssqrts(sabss(ImgIn)) * ssqrts(0.5); ImgOut = _sign(1, ImgIn) * RealOut; } else if( sabss(RealIn) <= RMax && sabss(ImgIn) <= RMax) {/* standard case : a (not zero) and b are finite */ float Temp = ssqrts(2.0f * (sabss(RealIn) + spythags(RealIn, ImgIn))); /* overflow test */ if(Temp > RMax) {/* handle (spurious) overflow by scaling a and b */ float RealTemp = RealIn / 16.0f; float ImgTemp = ImgIn / 16.0f; Temp = ssqrts(2.0f * (sabss(RealIn) + spythags(RealIn, ImgTemp))); if(RealTemp >= 0) { RealOut = 2 * Temp; ImgOut = 4 * ImgTemp / Temp; } else { RealOut = 4 * sabss(ImgIn) / Temp; ImgOut = _sign(2, ImgIn) * Temp; } } else if(RealIn >= 0) /* classic switch to get the stable formulas */ { RealOut = 0.5f * Temp; ImgOut = ImgIn / Temp; } else { RealOut = sabss(ImgIn) / Temp; ImgOut = (_sign(0.5f, ImgIn)) * Temp; } } else { /* //Here we treat the special cases where a and b are +- 00 or NaN. //The following is the treatment recommended by the C99 standard //with the simplification of returning NaN + i NaN if the //the real part or the imaginary part is NaN (C99 recommends //something more complicated) */ if(isnan(RealIn) == 1 || isnan(ImgIn) == 1) {/* got NaN + i NaN */ RealOut = RealIn + ImgIn; ImgOut = RealOut; } else if( dabss(ImgIn) > RMax) {/* case a +- i oo -> result must be +oo +- i oo for all a (finite or not) */ RealOut = sabss(ImgIn); ImgOut = ImgIn; } else if(RealIn < -RMax) {/* here a is -Inf and b is finite */ RealOut = 0; ImgOut = _sign(1, ImgIn) * sabss(RealIn); } else {/* here a is +Inf and b is finite */ RealOut = RealIn; ImgOut = 0; } } return FloatComplex(RealOut, ImgOut); }