views:

145

answers:

9

Given maths is not my strongest point I'm implementing a bezier curve for 3D animation.

The formula is shown here, and as you can see it is quite nasty. In my programming I use descriptive names, and like to break complex lines down to smaller manageable ones.

How is the best way to handle a scenario like this?

Is it to ignore programming best practices and stick with variable names such as x, y, and t?

+1  A: 

I'd encourage you to use mathematic's best practices and denote variables with letters. Just provide explanation for the variables above the formula. And if you can split the formula to smaller subformulas, even better.

Tatu Ulmanen
A: 

You could key the formula into wolfram alpha ... it will try to simplify for you.

It'll also output in a mathematica friendly style ... funnily enough ;)

Kindness,

Dan

Daniel Elliott
+4  A: 

In my opinion when you have a predefined mathematical equation it is perfectly acceptable to use short variable names: x, y, t, P_0 etc. which correspond to the equation. Make sure to reference the formula clearly though.

invariant
+2  A: 

if the formulas is extrated to its own function i'd certainly use the canonical maths representation, and maybe add the wiki page url in a comment

if its imbedded in code with a specific usage of the function then keeping the domain names from your code might be better

it depends

jk
+2  A: 

Seeing as only the mathematician in you is actually going to understand the formula, my advice would be to go with a style that a mathematician would be most comfortable with (so letters as variables etc...)

I would also definitely put a comment in there somewhere that clearly states what the formula is, and what it does, for example "This method returns a series of points along a quadratic Bezier curve". That way whenever the programmer in you revisits the code you can safely ignore the mathematical complexity with the assumption that your inner mathematician has already checked to make sure its all ok.

Kragen
+1  A: 

Don't bother. Just reference the documentation (the wikipedia page in this case or even better your own documentation) and make sure the variable names match your documentation. Code comments are just not well suited (nor need them to) describe mathematical formulation.

Sometimes a reference is better than 40 lines of comments or even suggestive variable names.

Jorge Córdoba
A: 

I tend to break an equation down into its root parts.

def sum(array)
  array.inject(0) { |result, item| result + item }
end

def average(array)
  sum(array) / array.length
end

def sum_squared_error(array)
  avg = average(array)
  array.inject(0) { |result, item| result + (item - avg) ** 2 }
end

def variance(array)
  sum_squared_error(array) / (array.length - 1)
end

def standard_deviation(array)
  Math.sqrt(variance(array))
end
Jarrett Meyer
A: 

You might consider using a domain-specific language to handle this. Mathematica would allow you to write out the equation just as it appears in mathematical notion.

The more your final form resembles the original equation, the more maintainable it will be in the long run (otherwise you have to interpret the code every time you see it).

Shane
+1  A: 

Make the formula in C# (or other language of preference) resemble the mathematical formula as closely as possible, and include a reference to the formula, including a description of the variables. The idea in coding is to be readable, and if you're dealing with mathematical formulae the most readable representation is the one that looks most like mathematics.

David Thornley