Hot answers tagged

20

You can use MeshFunctions to do the trick: g = BSplineFunction[{RandomReal[1, 20], RandomReal[1, 20]}\[Transpose]]; dg = g'; ParametricPlot[ g[t], {t, 0, 1}, MeshFunctions -> Function[{x, y, t}, dg[t].{0, 1}], Mesh -> {{0}}, MeshStyle -> Directive[AbsolutePointSize[5], Red] ] Here the MeshFunctions specifies the value of dg[t]...


19

If my answer for the 2D case lacks detail, it's because Typeset`MakeBoxes is an internal, undocumented function. That makes it hard to say anything authoritative about how it works. Essentially though, we are defining the custom primitive in such a way that the definition only applies during conversion of a graphics expression to boxes. Here's a version of ...


18

General When given an array of integrands NIntegrate is run separately over each array element. That is not necessary though, the core NIntegrate integration strategies can work with any integrands as long as the error estimates are real numbers. The motivation for implementing ArrayOfFunctionsRule is to provide a significant speed-up for integrands that ...


16

The OP linked to an answer of mine for interpolating over general point sets; for constructing a single interpolating function, a slight modification of my procedure is needed. (In particular, you don't need centripetal or chord-length parametrization in this case.) Let's start with some data: data = {{0, 0}, {1/10, 3/10}, {1/2, 3/5}, {1, -1/5}, {2, 3}, {3,...


15

For a simple image like that (high-contrast object in front of a monochrome background), calling EdgeDetect is actually enough to find the edges: img = Import["http://i.stack.imgur.com/UhLrU.png"]; edges = EdgeDetect[img]; Then we can find the leftmost and rightmost points on every line to get an array of radii: radii = Map[Max[#[[All, 1]]] - Min[#[[...


14

Fortunately there is a solution but it appears to be undocumented and takes a bit of guess work. The magic is: FilledCurveBoxOptions -> {Method -> {"SplinePoints" -> (* integer value *)}} This may be set globally or for a Notebook: SetOptions[InputNotebook[], FilledCurveBoxOptions -> {Method -> {"SplinePoints" -> 30}}] (Use $FrontEnd ...


12

Here is my implementation of quadric spline arbitrary precision interpolation which is defined exactly as in Interpolation with options Method->"Spline", InterpolationOrder->2. Theoretical background Quadric spline interpolation for n datapoints is defined as a Piecewise function which consists of n-2 parabolas which are spliced together in the ...


12

I appreciate kguler's elegant solution, but it doesn't join the points. To be more precise, it joins only every third point because Bezier line additionally takes 2 anchor points for each point. There are different methods to obtain these points. The simplest one is the following (pictures taken here) In Mathematica it looks like the following code (for an ...


12

f[n_, j_, u_] := PiecewiseExpand[BernsteinBasis[n, j, u]] cv[pt_, n_, u_] := (f[n, #, u] & /@ Range[0, n]).pt Test: pts = {{0, 0}, {2, 4}, {4, 5}, {6, 0}}; ParametricPlot[cv[pts, Length@pts - 1, v], {v, 0, 1}, Epilog -> {Red, PointSize[0.02], Point[pts], Green, Line[pts]}, PlotRange -> {0, 6}] Or DynamicModule[{p = {{0, 0}, {2, 4}, {4, 5}, {...


12

Here is a Fourier Basis approach: ClearAll[FourierBasis2D]; FourierBasis2D[{numx_, numy_}, {λx_, λy_}, x_, y_] := N[With[{ωn = 2 π/λx, ωm = 2 π/λy}, Flatten[ {1}~Join~ Table[ {Cos[ n ωn x] Cos[m ωm y], Cos[ n ωn x] Sin[ m ωm y], Sin[ n ωn x] Cos[m ωm y], Sin[ n ωn x] Sin[ m ωm y]}, {n, numx}, {m, ...


12

Update Since version 12, this functionality in integrated in Mathematica via the Option FitRegularization Following on @Ajasja's answer in the spirit of this answer one can in fact provide controlled smoothing va an explicit Tichonov like penalty as follows: ff = Function[{x, y}, basis // Evaluate]; a = ff @@ # & /@ (Most /@ data); so that fit[x_,...


12

This was solved with help from Shutao Tang, J.M., Sander Huisman and Eric Rimbey. Why do BezierFunction[pt] and BezierCurve[pt] not agree? Because BezierCurve uses SplineDegree -> 3 by default and BezierFunction always uses degree Length[pt] - 1 (not settable). Why does DiscretizeGraphics give a bad result? Because it appears to use (the equivalent of) ...


12

So DiscretizeGraphics seems to always miss the first or last point of a BSplineCurve (it seems to do it with a BezierCurve as well). Here's the simplest example of this, pts = {{.5, 0}, {1, 0}, {1, 1}, {.5, 1}, {0, 1}, {0, 0}, {.5, 0}}; GraphicsRow[{Graphics@#, DiscretizeGraphics@#} &@BSplineCurve[pts], ImageSize -> 600] Why does it do this? Not ...


12

This might help you get an idea: n = 4; BlockRandom[SeedRandom[42, Method -> "Legacy"]; (* for reproducibility *) cpts = Table[{i, j, RandomReal[{-1, 1}]}, {i, n + 1}, {j, n + 1}]]; GraphicsRow[{ParametricPlot3D[BezierFunction[cpts][u, v], {u, 0, 1}, {v, 0, 1}, Evaluated -> True], (* built-in function *) ...


12

Graphics[{ Yellow, FilledCurve[{BSplineCurve /@ {pts1, pts4, Reverse@pts2, pts3}}], Green, Line[Join[pts1, pts4, Reverse@pts2, pts3]], Red, Point[Join[pts1, pts2, pts3, pts4]] }]


12

Extracting a spline from the image is easiest with ImageMesh. I've used the midpoints of the lines on the mesh boundary for the BSplineCurve control points. img = Import["https://i.stack.imgur.com/Yy0uN.png"]; mesh = ImageMesh@img; midpoints = Midpoint /@ MeshPrimitives[mesh, 1]; Graphics[{Thick, Darker[Red], BSplineCurve[midpoints, SplineClosed -> ...


11

Here is a (simplified) implementation of Reinsch's smoothing spline, which is effectively equivalent to csaps() in MATLAB's Curve Fitting Toolbox. Fancier methods have come along since then (e.g. Wahba's cross-validation splines), but this old workhorse has still proved serviceable: SmoothingSplineFunction[dat_?MatrixQ, p : (_?NumericQ | Automatic) : ...


11

Major update, version 2.0 What changed: Is a different, more clever way of solving the problem. Overcomes most of the issues of previous versions. Problem summary: I will rephrase the problem in simplest terms. There exests a named curve called limacon (french, pronounced [ˈlɪməsɒn], means snail), described by a simple equation $r=a+b\cos{\theta}$ in ...


11

The following works for your curve: points = {{1, 4}, {.5, 6}, {5, 4}, {3, 12}, {11, 14}, {8, 4}, {12, 3}, {11, 9}, {15, 10}, {17, 8}}; deg = 3; pointsCLOSE1 = Join[points, points]; n = Length@pointsCLOSE1; knotsCLOSE1 = Range[0, 1, 1/(n + 1)]; ParametricPlot[deBoor[pointsCLOSE1, {deg, knotsCLOSE1}, t], {t, deg/(n + 1), 1}, Axes -&...


11

Update Fully compiling the code to C makes it as fast as the built-in: cBernstein = Compile @@ (Hold[{n, {j, _Real, 1}, u}, Table[expr u^i (1 - u)^(n - i), {i, j}], CompilationTarget -> C] /. expr -> FunctionExpand[Binomial[n, i]]) BezierSurface4 = With[{cBernstein = cBernstein}, With[{AllBasis = Function[{deg, u0}, cBernstein[deg, ...


11

Here is my approach: pts = {{0, 0}, {2, 1}, {4, 3}, {6, 1}}; paras = FoldList[Plus, 0, Normalize[(Norm /@ Differences[pts]), Total]] // N mat = Outer[BernsteinBasis[3, #1, #2] &, Range[0, 3], paras] // Transpose; ctrlpts = LinearSolve[mat, pts] (* {{0., 0.}, {2.71043, -0.262717}, {3.94236, 6.32778}, {6., 1.}} *) Graphics[{BezierCurve[ctrlpts], PointSize[...


11

BezierCurve normally gives a composition of local 4point-Bezierfunctions. You get equal curves by setting SplineDegree->1+Length[pts] Show[Graphics[{Red, Point[pts], Green, Line[pts]}, Axes -> True], ParametricPlot[f[t], {t, 0, 1}], Graphics[{Blue, Dashed,BezierCurve[pts, SplineDegree -> 1 + Length[pts]]}]]


10

I have done this before in some previous spline-related threads, but here it is again: fspl = Interpolation[Xac, Method -> "Spline", InterpolationOrder -> 2]; fb = First[Cases[fspl, _BSplineFunction, ∞]]; {sd, scpts, sk} = fb /@ {"Degree", "ControlPoints", "Knots"}; (* explicitly constructed B-spline surface *) ip[x_, y_] = Fold[#2.#1 &, scpts, ...


9

As Ulises Cervantes Pimentel uncovered in this Wolfram Community thread, Plot and ParametricPlot/ParametricPlot3D for curves can take as MeshFunctions the values "ArcLength" and "CurveLength". This functionality provides straightforward solution for the plots with AspectRatio -> Automatic: mpoints = Table[t, {t, 0, 1, 0.04}]; ParametricPlot[g[t], {...


9

reg = BoundaryDiscretizeGraphics@ ParametricPlot[ BSplineFunction[{{0, 0}, {1, 0}, {2, .5}, {1, 1}, {0, 1}}, SplineClosed -> True][t], {t, 0, 1}] Plot3D[Sin[6 x y], {x, y} ∈ reg]


9

After permuting the points so that the cusp is the first point (just like in Gypaets's answer), the methods of this answer can be used: airfoil = Flatten[MapAt[Most @* Reverse, DeleteCases[Split[Drop[ Import["http://m-selig.ae.illinois.edu/ads/coord/n64015.dat"], 3], # =!= {} && #2 =!= {} &], {{}}], 1], 1]; (*...


9

1. To get "the black curve (to) completely obscure the red curve" You can replace BSplineCurves with Lines using BSplineFunction: sw = {1, 1/Sqrt[2], 1}; segments = {BSplineCurve[{{1, 0}, {1, 1}, {0, 1}}, SplineWeights -> sw], BSplineCurve[{{0, 1}, {-1, 1}, {-1, 0}}, SplineWeights -> sw]}; Graphics[{Red , segments, Black, Dashed, ...


8

A more interesting example with multiple extrema.. g = BSplineFunction[{{1, 2}, {2, 4}, {3, -1}, {4, 2}, {5, 0}, {6, 1}}]; gp = g'; gpy[t_?NumericQ] := gp[t][[2]]; This is utilising Plot to generate the curve and look for zero crossings, which we then pass as starting points to FindRoot loc = Flatten[ t /. # & /@ FindRoot[gpy[t] , ...


8

The definition of B-Spline curve $$\vec{C}(u)=\sum _{i=0}^n N_{i,p}(u) \vec{P}_i \text{ }\qquad (0\leq u\leq 1)$$ where, $\vec{P}_i$ is the control point, and the $N_ {i, p} (u)$ are the $p$-th - degree B-spline basis functions defined on the non-periodic(and non-uniform) knot vector $U$. $$U=\{\underbrace {0,\cdots ,0}_{p+1},u_{p+1},\cdots u_{m-p-1},\...


Only top voted, non community-wiki answers of a minimum length are eligible