bezier(float xb,float yb,float xc,float yc,float xd,float yd,int n) { float xab,yab,xbc,ybc,xcd,ycd; float xabc,yabc,xbcd,ybcd; float xabcd,yabcd; if (n==0) { line1(xb,yb); line1(xc,yc); line1(xd,yd); } else { xab = (xxx[0][0]+xb)/2; yab = (xxx[0][1]+yb)/2; xbc = (xb+xc)/2; ybc = (yb+yc)/2; xcd = (xc+xd)/2; ycd = (yc+yd)/2; xabc = (xab+xbc)/2; yabc = (yab+ybc)/2; xbcd = (xbc+xcd)/2; ybcd = (ybc+ycd)/2; xabcd = (xabc+xbcd)/2; yabcd = (yabc+ybcd)/2; n=n-1; bezier(xab,yab,xabc,yabc,xabcd,yabcd,n); bezier(xbcd,ybcd,xcd,ycd,xd,yd,n); } return (0); }
double NewtonRaphsonRootFind(Point<ndims> *Q, Point<ndims> P, double u) { /* Compute Q(u) */ Point<ndims> Q_u = bezier(3, Q, u); /* Generate control vertices for Q' */ Point<ndims> Q1[3]; for (int i = 0; i <= 2; i++) Q1[i] = (Q[i + 1] - Q[i]) * 3.; /* Generate control vertices for Q'' */ Point<ndims> Q2[2]; for (int i = 0; i <= 1; i++) Q2[i] = (Q1[i + 1] - Q1[i]) * 2.; /* Compute Q'(u) and Q''(u) */ Point<ndims> Q1_u = bezier(2, Q1, u); Point<ndims> Q2_u = bezier(1, Q2, u); /* Compute f(u)/f'(u) */ double numerator = 0; double denominator = 0; for (int i = 0; i < ndims; ++i) { numerator += (Q_u[i] - P[i]) * Q1_u[i]; denominator += Q1_u[i] * Q1_u[i] + (Q_u[i] - P[i]) * Q2_u[i]; } if (denominator == 0.0f) return u; /* u = u - f(u)/f'(u) */ double u_prime = u - (numerator/denominator); return u_prime; }
util::vec2 bezier(float t, util::vec2* ps, size_t cant) { if(cant == 1) { return *ps; } return (bezier(t, ps, cant - 1) * (1 - t)) + (bezier(t, ps + 1, cant - 1) * t); }
double bezier_gety(double x1, double y1, double x2, double y2, double x) { double l, r, mid; for (l = 0, r = 1; fabs(r - l) > 1e-6;) { mid = (l + r) / 2; double t = bezier(x1, x2, mid); if (t > x) r = mid; else l = mid; } return bezier(y1, y2, (l + r) / 2); }
static void bezier(fz_gel *gel, const fz_matrix *ctm, float flatness, float xa, float ya, float xb, float yb, float xc, float yc, float xd, float yd, int depth) { float dmax; float xab, yab; float xbc, ybc; float xcd, ycd; float xabc, yabc; float xbcd, ybcd; float xabcd, yabcd; /* termination check */ dmax = fz_abs(xa - xb); dmax = fz_max(dmax, fz_abs(ya - yb)); dmax = fz_max(dmax, fz_abs(xd - xc)); dmax = fz_max(dmax, fz_abs(yd - yc)); if (dmax < flatness || depth >= MAX_DEPTH) { line(gel, ctm, xa, ya, xd, yd); return; } xab = xa + xb; yab = ya + yb; xbc = xb + xc; ybc = yb + yc; xcd = xc + xd; ycd = yc + yd; xabc = xab + xbc; yabc = yab + ybc; xbcd = xbc + xcd; ybcd = ybc + ycd; xabcd = xabc + xbcd; yabcd = yabc + ybcd; xab *= 0.5f; yab *= 0.5f; xbc *= 0.5f; ybc *= 0.5f; xcd *= 0.5f; ycd *= 0.5f; xabc *= 0.25f; yabc *= 0.25f; xbcd *= 0.25f; ybcd *= 0.25f; xabcd *= 0.125f; yabcd *= 0.125f; bezier(gel, ctm, flatness, xa, ya, xab, yab, xabc, yabc, xabcd, yabcd, depth + 1); bezier(gel, ctm, flatness, xabcd, yabcd, xbcd, ybcd, xcd, ycd, xd, yd, depth + 1); }
bool EditBezierOP::onDraw() const { if (ZoomViewOP::onDraw()) return true; if (m_captured.shape) { if (m_cmpt) { if (BezierShape* bezier = dynamic_cast<BezierShape*>(m_captured.shape)) { PrimitiveDraw::drawCircle(Vector(bezier->getRect().xCenter(), bezier->getRect().yCenter()), m_cmpt->getNodeCaptureDistance(), true, 2, Colorf(0.4f, 1.0f, 0.4f)); if (m_captured.pos.isValid()) PrimitiveDraw::drawCircle(m_captured.pos, m_cmpt->getNodeCaptureDistance(), true, 2, Colorf(1.0f, 0.4f, 0.4f)); } } } else { if (m_firstPress.isValid() && m_currPos.isValid()) { BezierShape bezier(m_firstPress, m_currPos); bezier.draw(); } // PrimitiveDraw::drawRect(m_firstPress, m_currPos); } return false; }
bool AndroidAnimation::checkIterationsAndProgress(double time, float* finalProgress) { float progress = currentProgress(time); int currentIteration = static_cast<int>(progress); if (currentIteration != m_currentIteration) if (m_direction == Animation::AnimationDirectionAlternate) swapDirection(); m_currentIteration = currentIteration; progress -= m_currentIteration; if ((m_currentIteration >= m_iterationCount) && (m_iterationCount != Animation::IterationCountInfinite)) return false; if (m_timingFunction.type() != LinearTimingFunction) { UnitBezier bezier(m_timingFunction.x1(), m_timingFunction.y1(), m_timingFunction.x2(), m_timingFunction.y2()); if (m_duration > 0) progress = bezier.solve(progress, 1.0f / (200.0f * m_duration)); } *finalProgress = progress; return true; }
void ofxTweener::update(){ for(int i = tweens.size() -1; i >= 0; --i){ if(float(tweens[i]._timestamp.elapsed()) >= float(tweens[i]._duration)){ //tween is done bool found = false; if(!_override){ //if not found anymore, place on exact place for(int j = 0; j < tweens.size(); ++j){ if(tweens[j]._var == tweens[i]._var) { found = true; break; } } } if(!found) tweens[i]._var[0] = tweens[i]._to; tweens.erase(tweens.begin() + i); //dispatch event here! } else if(float(tweens[i]._timestamp.elapsed()) > 0){ //smaller than 0 would be delayed if(tweens[i]._useBezier) tweens[i]._var[0] = bezier(tweens[i]._from, tweens[i]._to ,(a.*tweens[i]._easeFunction )(float(tweens[i]._timestamp.elapsed()), 0, 1, float(tweens[i]._duration)), tweens[i]._by); else tweens[i]._var[0] = (a.*tweens[i]._easeFunction )(float(tweens[i]._timestamp.elapsed()), tweens[i]._from, tweens[i]._to - tweens[i]._from, float(tweens[i]._duration)); } } }
double AndroidAnimation::applyTimingFunction(float from, float to, double progress, const TimingFunction* tf) { double fractionalTime = progress; double offset = from; double scale = 1.0 / (to - from); if (scale != 1 || offset) fractionalTime = (fractionalTime - offset) * scale; const TimingFunction* timingFunction = tf; if (!timingFunction) timingFunction = m_timingFunction.get(); if (timingFunction && timingFunction->isCubicBezierTimingFunction()) { const CubicBezierTimingFunction* bezierFunction = static_cast<const CubicBezierTimingFunction*>(timingFunction); UnitBezier bezier(bezierFunction->x1(), bezierFunction->y1(), bezierFunction->x2(), bezierFunction->y2()); if (m_duration > 0) fractionalTime = bezier.solve(fractionalTime, 1.0f / (200.0f * m_duration)); } else if (timingFunction && timingFunction->isStepsTimingFunction()) { const StepsTimingFunction* stepFunction = static_cast<const StepsTimingFunction*>(timingFunction); if (stepFunction->stepAtStart()) { fractionalTime = (floor(stepFunction->numberOfSteps() * fractionalTime) + 1) / stepFunction->numberOfSteps(); if (fractionalTime > 1.0) fractionalTime = 1.0; } else { fractionalTime = floor(stepFunction->numberOfSteps() * fractionalTime) / stepFunction->numberOfSteps(); } } return fractionalTime; }
int GPsplinegon (Gwidget_t *widget, int gpn, Gpoint_t *gpp, Ggattr_t *ap) { PIXpoint_t p0, p1, p2, p3; int n, i; if (gpn == 0) return 0; Gppi = 1; if (Gppi >= Gppn) { n = (((Gppi + 1) + PPINCR - 1) / PPINCR) * PPINCR; Gppp = Marraygrow (Gppp, (long) n * PPSIZE); Gppn = n; } Gppp[0] = p3 = pdrawtopix (widget, gpp[0]); for (i = 1; i < gpn; i += 3) { p0 = p3; p1 = pdrawtopix (widget, gpp[i]); p2 = pdrawtopix (widget, gpp[i + 1]); p3 = pdrawtopix (widget, gpp[i + 2]); bezier (p0, p1, p2, p3); } setgattr (widget, ap); if (WPU->gattr.fill) { if (Gppp[Gppi - 1].x != Gppp[0].x || Gppp[Gppi - 1].y != Gppp[0].y) Gppp[Gppi] = Gppp[0], Gppi++; Polygon (GC, Gppp, (int) Gppi); } else Polyline (GC, Gppp, (int) Gppi); return 0; }
Vector3 BezierSpline::interpolate(scalar_t t) const { if (!get_segment_count()) return Vector3(0, 0, 0); if (arc_parametrize) { t = ease(parametrize(t)); } t = (t * get_segment_count()); int seg = (int) t; t -= (scalar_t) floor(t); if (seg >= get_segment_count()) { seg = get_segment_count() - 1; t = 1.0f; } seg *= 4; ListNode<Vector3> *iter = const_cast<BezierSpline*>(this)->control_points.begin(); for (int i = 0; i < seg; i++) iter = iter->next; Vector3 Cp[4]; for (int i = 0; i < 4; i++) { Cp[i] = iter->data; iter = iter->next; } // interpolate return bezier(Cp[seg], Cp[seg + 1], Cp[seg + 2], Cp[seg + 3], t); }
static inline double solveCubicBezierFunction(double p1x, double p1y, double p2x, double p2y, double t, double duration) { // Convert from input time to parametric value in curve, then from // that to output time. UnitBezier bezier(p1x, p1y, p2x, p2y); return bezier.solve(t, solveEpsilon(duration)); }
void ofxTweener::update(){ for(int i = tweens.size() -1; i >= 0; --i){ if(float(tweens[i]._timestamp.elapsed()) >= float(tweens[i]._duration)){ //tween is done bool found = false; if(!_override){ //if not found anymore, place on exact place for(int j = 0; j < tweens.size(); ++j){ if(tweens[j]._var == tweens[i]._var) { found = true; break; } } } if(!found) tweens[i]._var[0] = tweens[i]._to; std::map<float *, std::function<void(float *arg)>>::iterator it = callbacks.find(tweens[i]._var); if(it != callbacks.end()) { it->second(tweens[i]._var); callbacks.erase(it); } tweens.erase(tweens.begin() + i); } else if(float(tweens[i]._timestamp.elapsed()) > 0){ //smaller than 0 would be delayed if(tweens[i]._useBezier) tweens[i]._var[0] = bezier(tweens[i]._from, tweens[i]._to ,(a.*tweens[i]._easeFunction )(float(tweens[i]._timestamp.elapsed()), 0, 1, float(tweens[i]._duration)), tweens[i]._by); else tweens[i]._var[0] = (a.*tweens[i]._easeFunction )(float(tweens[i]._timestamp.elapsed()), tweens[i]._from, tweens[i]._to - tweens[i]._from, float(tweens[i]._duration)); } } }
// Convert this curve to Bezier surfaces // After inserting knot, nurbs bool MH_SrfNurbs::ConvertToBeziers(MH_SrfBezierVect& beziers, size_t& nBezierS, MH_SrfNurbs& nurbs) const { nurbs = *this; FloatVect vS, vT; FloatVect::const_iterator it = m_vKnotS.begin(); FloatVect::const_iterator itEnd = m_vKnotS.end(); for(; it!=itEnd; ) { vS.push_back(*it); size_t nTimes = GetRepeatTimes(true, *it); if(m_nOrderS-1 > nTimes) nurbs.InsertKnotS(*it, int(m_nOrderS-1-nTimes)); it = it+nTimes; } it = m_vKnotT.begin(); itEnd = m_vKnotT.end(); for(; it!=itEnd; ) { vT.push_back(*it); size_t nTimes = GetRepeatTimes(false, *it); if(m_nOrderT-1 > nTimes) nurbs.InsertKnotT(*it, int(m_nOrderT-1-nTimes)); it = it+nTimes; } beziers.clear(); const MH_CVVect& vCV = nurbs.GetCVs(); size_t nOrderS = nurbs.GetOrderS(); size_t nOrderT = nurbs.GetOrderT(); size_t nCVNumS = nurbs.GetCVNumS(); size_t nCVNumT = nurbs.GetCVNumT(); // After conversion, there are nBezierS Bezier-srf in S // nBezierT Bezier-srf in T ASSERT((nCVNumS-1)%(nOrderS-1) == 0); ASSERT((nCVNumT-1)%(nOrderT-1) == 0); nBezierS = (nCVNumS-1)/(nOrderS-1); size_t nBezierT = (nCVNumT-1)/(nOrderT-1); for(size_t t=0; t<nBezierT; t++) { for(size_t s=0; s<nBezierS; s++) { MH_CVVect vCVTemp; for(size_t h = 0; h<nOrderT; h++) { MH_CVVect::const_iterator itCV = vCV.begin() + (t*(nOrderT-1)+h)*nCVNumS + nOrderS*s-s; vCVTemp.insert(vCVTemp.end(), itCV, itCV+nOrderS); } MH_SrfBezier bezier(vCVTemp, nOrderS, vS[s], vS[s+1], vT[t], vT[t+1]); beziers.push_back(bezier); } } return true; }
Real Interp_<Real>::bezierAtTime(Real time, const Vec2& v0, const Vec2& v1_, const Vec2& v2_, const Vec2& v3) { assert(Alge::isInRange(time, v0.x, v3.x)); Vec2 v1, v2; tie(v1, v2) = bezierNormalizeHandles(v0, v1_, v2_, v3); Vec3 roots = get<0>(bezierRoots(time, v0.x, v1.x, v2.x, v3.x)); return bezier(roots.x, v0.y, v1.y, v2.y, v3.y); }
//curve3_div void curve3_div::init(scalar x1, scalar y1, scalar x2, scalar y2, scalar x3, scalar y3) { m_points.clear(); m_distance_tolerance_square = FLT_TO_SCALAR(0.5f) / m_approximation_scale; m_distance_tolerance_square *= m_distance_tolerance_square; bezier(x1, y1, x2, y2, x3, y3); m_count = 0; }
// -------------------------------------------------- void DrawShape::CreateBezierPath(BPoint *curve) { Bezier bezier(curve, 4); const int n = BezierPoints(curve, 4)-1; REPORT(kDebug, 0, "BezierPoints %d", n); for (int i = 0; i <= n; i ++) { fSubPath.AddPoint(bezier.PointAt(i / (float) n)); } }
int add_keyframe(lua_State* state) { int args = lua_gettop(state); if(args != 4 && args != 5 && args != 8) { luaL_error(state, "Invalid number of arguments for adding keyframes"); } if(!lua_isstring(state, 1)) { return luaL_argerror(state, 1, "animation name expected"); } if(!lua_isnumber(state, 2)) { return luaL_argerror(state, 2, "time value expected"); } if(!lua_isnumber(state, 3)) { return luaL_argerror(state, 3, "transition state expected"); } if(!lua_isnumber(state, 4)) { return luaL_argerror(state, 4, "attribute expected"); } if(args == 5 && !lua_isnumber(state, 5)) { return luaL_argerror(state, 5, "tween type expected"); } if(args == 8) { for(int i = 5; i != 8; i++) { if(!lua_isnumber(state, i)) { return luaL_argerror(state, i, "control point coordinate expected"); } } } lua_getglobal(state, "renderer"); OpticRender* render = static_cast<OpticRender*>(lua_touserdata(state, -1)); lua_pop(state, 1); try { if(args == 4) { render->addKeyframe(lua_tostring(state, 1), Keyframe(lua_tointeger(state, 2), lua_tonumber(state, 3)), TRANSFORM(lua_tointeger(state, 4)), LINEAR); } else if(args == 5) { render->addKeyframe(lua_tostring(state, 1), Keyframe(lua_tointeger(state, 2), lua_tonumber(state, 3)), TRANSFORM(lua_tointeger(state, 4)), TWEEN(lua_tointeger(state, 5))); } else { OpticBezier bezier(lua_tonumber(state, 5), lua_tonumber(state, 6), lua_tonumber(state, 7), lua_tonumber(state, 8)); render->addKeyframe(lua_tostring(state, 1), Keyframe(lua_tointeger(state, 2), lua_tonumber(state, 3)), TRANSFORM(lua_tointeger(state, 4)), bezier); } } catch(OpticException& e) { luaL_error(state, e.what()); } return 0; }
int main() { int i; int gm; int gd=DETECT; initgraph(&gd,&gm,""); int x=10; int y=10; int h=200; int k=200; while(1){ cleardevice(); hiosbox(x,y,h,k); int t=0; for(i=0;i<25;i++) { setcolor(i); hiosbox(x,y,h,k); if(i%5==0)t++; setcolor(3); fillellipse(x+h/3+i,y+k/6-i,h/50,k/50+t); barish(x,y,h,k); setcolor(i); delay(100); point p[4]={{x+rand()%h/6,y+rand()%k/6},{x+rand()%h/6,y+rand()%k/6},{x+rand()%h/6,y+rand()%k/6},{x+rand()%h/6,y+rand()%k/6}}; point q[4]={{x+rand()%h/7,y+rand()%k/7},{x+rand()%h/7,y+rand()%k/7},{x+rand()%h/7,y+rand()%k/7},{x+rand()%h/7,y+rand()%k/7}}; bezier(p); bezier(q); cleardevice(); } } getch(); closegraph(); }
//------------------------------------------------------------------------ void curve3_div::init(double x1, double y1, double x2, double y2, double x3, double y3) { m_points.remove_all(); m_distance_tolerance_square = 0.5 / m_approximation_scale; m_distance_tolerance_square *= m_distance_tolerance_square; bezier(x1, y1, x2, y2, x3, y3); m_count = 0; }
void fz_flatten_fill_path(fz_gel *gel, fz_path *path, const fz_matrix *ctm, float flatness) { float x1, y1, x2, y2, x3, y3; float cx = 0; float cy = 0; float bx = 0; float by = 0; int i = 0; while (i < path->len) { switch (path->items[i++].k) { case FZ_MOVETO: /* implicit closepath before moveto */ if (cx != bx || cy != by) line(gel, ctm, cx, cy, bx, by); x1 = path->items[i++].v; y1 = path->items[i++].v; cx = bx = x1; cy = by = y1; break; case FZ_LINETO: x1 = path->items[i++].v; y1 = path->items[i++].v; line(gel, ctm, cx, cy, x1, y1); cx = x1; cy = y1; break; case FZ_CURVETO: x1 = path->items[i++].v; y1 = path->items[i++].v; x2 = path->items[i++].v; y2 = path->items[i++].v; x3 = path->items[i++].v; y3 = path->items[i++].v; bezier(gel, ctm, flatness, cx, cy, x1, y1, x2, y2, x3, y3, 0); cx = x3; cy = y3; break; case FZ_CLOSE_PATH: line(gel, ctm, cx, cy, bx, by); cx = bx; cy = by; break; } } if (cx != bx || cy != by) line(gel, ctm, cx, cy, bx, by); }
//------------------------------------------------------------------------ void phobos::system::agg::curve4_div::init(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { m_points.remove_all(); m_distance_tolerance_square = 0.5 / m_approximation_scale; m_distance_tolerance_square *= m_distance_tolerance_square; bezier(x1, y1, x2, y2, x3, y3, x4, y4); m_count = 0; }
void curve4_div::init(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) { m_points.remove_all(); m_distance_tolerance_square = 1.0f / 4; m_distance_tolerance_manhattan = 1.0f * 4; bezier(x1, y1, x2, y2, x3, y3, x4, y4); m_count = 0; }
main() { igraph(); maxx = getmaxx(); maxy = getmaxy(); setcolor(2); bezier(0.85,0.75,0.57,0.78,0.534,0.9,4); setfillstyle(2,2); getch(); closegraph(); return(0); }
void graphics_test(){ // test des fonctions graphiques int i; rectangle(0,0,HRES-1,VRES-1); polygon(pts,3); circle(HRES/2,VRES/2,100); // for (i=0;i<100;i++){ // ellipse(HRES/3+i,VRES/3+i,50,30); // } bezier(20,200,20,40,300,40); delay_ms(500); }//graphics_test
void Root::bezier_curve(const int &every) { vector< Vect<float> >::iterator it; int inc = 0; for (it = vect_offset.begin(); it < vect_offset.end(); it += every) { vect_offset.at(inc) = *it; ++inc; } inc = 0; for (it = vect_rot.begin(); it < vect_rot.end(); it += every) { vect_rot.at(inc) = *it; ++inc; } vect_offset.resize(inc); vect_rot.resize(inc); bezier(vect_offset); bezier(vect_rot); }
int main() { int x[3], y[3]; int i; Screen screen; printf ("Enter the x- and y-coordinates of the four control points.\n"); for (i=0; i<3; i++) scanf ("%d%d", &x[i], &y[i]); screen.init(); bezier (screen, x, y); screen.terminate(); return 0; }
void quater::hermite(const quater& qa, const vector3& wa, const quater& qb, const vector3& wb, double t) { quater q0, q1, q2, q3; // p0=pa, p1=pa+va/3, p2=pb-vb/3, p3=pb // -> quaternion으로 바꾸면.. q0=qa; q1.mult((wa/3.f).quaternion(), qa); q2.mult((wb/3.f).quaternion().inverse(), qb); q3=qb; bezier(q0, q1, q2, q3, t); }
/* Extend the interval i to include the minimum and maximum of a 1-dimensional Bezier segment given by control points x0..x3. For efficiency, x0 in i is assumed as a precondition. */ static void bezier_limits(double x0, double x1, double x2, double x3, interval_t *i) { double a, b, c, d, r; double t, x; /* the min and max of a cubic curve segment are attained at one of at most 4 critical points: the 2 endpoints and at most 2 local extrema. We don't check the first endpoint, because all our curves are cyclic so it's more efficient not to check endpoints twice. */ /* endpoint */ extend(i, x3); /* optimization: don't bother calculating extrema if all control points are already in i */ if (in_interval(i, x1) && in_interval(i, x2)) { return; } /* solve for extrema. at^2 + bt + c = 0 */ a = -3*x0 + 9*x1 - 9*x2 + 3*x3; b = 6*x0 - 12*x1 + 6*x2; c = -3*x0 + 3*x1; d = b*b - 4*a*c; if (d > 0) { r = sqrt(d); t = (-b-r)/(2*a); if (t > 0 && t < 1) { x = bezier(t, x0, x1, x2, x3); extend(i, x); } t = (-b+r)/(2*a); if (t > 0 && t < 1) { x = bezier(t, x0, x1, x2, x3); extend(i, x); } } return; }
void draw3Line(FastVoxelView &vv,Pos3D p1,Pos3D p2,Pos3D p3,Color c,int a,float s,float s2) { float fp=0,fa=1.0/a; int ip; Pos3D mp(0,0,0); // cdebug(p1<<p2<<p3); for(ip=0;ip<=a;ip++) { float ms=(1-fp)*s+fp*s2; mp=bezier(fp,p1,p2,p3); // cdebug(mp<<"//"<<fp<<"//"<<ms); drawBall(vv,mp,ms,c); fp+=fa; } }