Re: Challenge: converting python (or Javascript) code to Livecode

2022-06-15 Thread Bob Sneidar via use-livecode
For extra credit, try writing a handler in LC to convert the python/js code to 
LC script. 

Bob S


> On Jun 15, 2022, at 02:46 , David Bovill via use-livecode 
>  wrote:
> 
> Thanks Craig - I dug around but no joy. I hate it when you loose code as I 
> had a function somewhere… The javascript and python code is not that long, so 
> it will be an interesting challenge to translate:
> 
> Python code:
> 
> • https://github.com/volkerp/fitCurves/blob/master/fitCurves.py
> 
> Javascript code:
> 
> • https://github.com/soswow/fit-curve/blob/master/src/fit-curve.js

___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode


Challenge: converting python (or Javascript) code to Livecode

2022-06-15 Thread David Bovill via use-livecode
Thanks Craig - I dug around but no joy. I hate it when you loose code as I had 
a function somewhere… The javascript and python code is not that long, so it 
will be an interesting challenge to translate:

Python code:

• https://github.com/volkerp/fitCurves/blob/master/fitCurves.py

Javascript code:

• https://github.com/soswow/fit-curve/blob/master/src/fit-curve.js

Here is the core python function:
> quote_type
> def fitCubic(points, leftTangent, rightTangent, error):
>  # Use heuristic if region only has two points in it
>  if (len(points) == 2):
>  dist = linalg.norm(points[0] - points[1]) / 3.0
>  bezCurve = [points[0], points[0] + leftTangent * dist, points[1] + 
> rightTangent * dist, points[1]]
>  return [bezCurve]
>
>  # Parameterize points, and attempt to fit curve
>  u = chordLengthParameterize(points)
>  bezCurve = generateBezier(points, u, leftTangent, rightTangent)
>  # Find max deviation of points to fitted curve
>  maxError, splitPoint = computeMaxError(points, bezCurve, u)
>  if maxError < error:
>  return [bezCurve]
>
>  # If error not too large, try some reparameterization and iteration
>  if maxError < error**2:
>  for i in range(20):
>  uPrime = reparameterize(bezCurve, points, u)
>  bezCurve = generateBezier(points, uPrime, leftTangent, rightTangent)
>  maxError, splitPoint = computeMaxError(points, bezCurve, uPrime)
>  if maxError < error:
>  return [bezCurve]
>  u = uPrime
>
>  # Fitting failed -- split at max error point and fit recursively
>  beziers = []
>  centerTangent = normalize(points[splitPoint-1] - points[splitPoint+1])
>  beziers += fitCubic(points[:splitPoint+1], leftTangent, centerTangent, error)
>  beziers += fitCubic(points[splitPoint:], -centerTangent, rightTangent, error)
>
>  return beziers
>
The maths code for the Newton Raphson method is:
> quote_type
> def newtonRaphsonRootFind(bez, point, u):
>  """
>  Newton's root finding algorithm calculates f(x)=0 by reiterating
>  x_n+1 = x_n - f(x_n)/f'(x_n)
>
>  We are trying to find curve parameter u for some point p that minimizes
>  the distance from that point to the curve. Distance point to curve is 
> d=q(u)-p.
>  At minimum distance the point is perpendicular to the curve.
>  We are solving
>  f = q(u)-p * q'(u) = 0
>  with
>  f' = q'(u) * q'(u) + q(u)-p * q''(u)
>
>  gives
>  u_n+1 = u_n - |q(u_n)-p * q'(u_n)| / |q'(u_n)**2 + q(u_n)-p * q''(u_n)|
>  """
>  d = bezier.q(bez, u)-point
>  numerator = (d * bezier.qprime(bez, u)).sum()
>  denominator = (bezier.qprime(bez, u)**2 + d * bezier.qprimeprime(bez, 
> u)).sum()
>
>
>  if denominator == 0.0:
>  return u
>  else:
>  return u - numerator/denominator
>
Seems a useful little challenge for the list?

📆    Schedule a call with me
On 14 Jun 2022, 14:43 +0100, Craig Newman via use-livecode 
, wrote:
> There is a similar thread on the forum:
>
> https://forums.livecode.com/viewtopic.php?f=8&t=34550 
> 
>
> Craig
>
> > On Jun 14, 2022, at 8:54 AM, David Bovill via use-livecode 
> >  wrote:
> >
> > I found some well documented python code:
> >
> > • https://github.com/volkerp/fitCurves/blob/master/fitCurves.py
> >
> > And some Javascript code:
> >
> > • https://github.com/soswow/fit-curve/blob/master/src/fit-curve.js
> >
> > The javascript code could be called from Livecode. To see the demo of how 
> > this would work in the browser:
> >
> > • http://soswow.github.io/fit-curve/demo/
> > • https://codepen.io/Sphin/pen/jrLxvQ
> >
> > Would seem to be a useful library to have available in native LC?
> >
> > 📆 Schedule a call with me
> > On 14 Jun 2022, 12:30 +0100, David Bovill , wrote:
> > > Searching around for a function in LC. It should take the points of a 
> > > graphic and return a smoothed the points of a smoothed line. I’ve found 
> > > lots of bezier style experiments but no “curve fitting” code. Anyone have 
> > > a function?
> > >
> > > 📆 Schedule a call with me
> > > On 6 Dec 2015, 12:10 +, Michael Kristensen 
> > > , wrote:
> > > > Pointlist to Bezier
> > > >
> > > > Hi there
> > > >
> > > > I wonder if any have code to take a point-list and turn it into a 
> > > > smooth-lined bezier.
> > > >
> > > > There are explanations around the net for C-code but it is very hard to 
> > > > understand.
> > > >
> > > > (one here said to be good but misses the graphics:)
> > > > http://www.benknowscode.com/2012/09/path-interpolation-using-cubic-bezier_9742.html
> > > >
> > > > What could this code be used for.
> > > >
> > > > — Tracing an image
> > > >
> > > > — Smoothing the lines drawn by a user
> > > >
> > > > plus a lot more Im sure
> > > >
> > > > Thanks
> > > > Michael
> > > > ___
> > > > use-livecode mailing list
> > > > use-livecode@lists.runrev.com
> > > > Please visit this url to subscribe, unsubscribe and manage your 
> > > > subscription preferences:
> > > > http://lists.runrev.com/mailman/listinfo/use-livecode
> > ___