4

I'm pretty poor at maths but managed to get someone who is decent at it to come up with a linear algebraic formula for the control over the distribution of points along a straight line.

The formula is:

$$f(x) = \left(\frac xL\right)^{\frac 1c}L$$

Would anyone be kind enough and help me translate that into a function I can write within Blender python?

Thanks in advance.

batFINGER
  • 84,216
  • 10
  • 108
  • 233

2 Answers2

6

With numpy

An ideal candidate for using numpy For example, can define a sequence

>>> x = np.arange(5)
>>> x
array([0, 1, 2, 3, 4])

and run a function quickly over all items, simple example squaring them.

>>> np.power(x, 2)
array([ 0,  1,  4,  9, 16])

Halving and squaring

>>> np.power(x / 2, 2)
array([0.  , 0.25, 1.  , 2.25, 4.  ])

The question equation.

Test script, defines a function of $x$ with scalars (assume they're scalars) $L$ and $c$

import bpy
import numpy as np

def distribute(x, L, c): return L * np.power(x / L, 1 / c)

test run

x = np.arange(20) for d in distribute(x, 10, 2): bpy.ops.object.empty_add(location=(d, 0, 0))

enter image description here

Empties placed as result of test run.

Related.

Create curve from Numpy Array using Python

batFINGER
  • 84,216
  • 10
  • 108
  • 233
  • 1
    What an excellently described answer. Thanks for this. Numpy really is a great extension to the core functionality. – James Field Apr 12 '21 at 19:14
3

The python expression is:

y = (x / L) ** ( 1 / c) * L

Chris
  • 59,454
  • 6
  • 30
  • 84
  • Although I'm going with the answer above, this is still really useful to know what the literal translation looks like. So thanks for your input. – James Field Apr 12 '21 at 19:16