[OpenSCAD] function syntax

MichaelAtOz oz.at.michael at gmail.com
Tue Oct 25 19:11:44 EDT 2016


Hi John, 

Welcome to the forum. Your post is still flagged as "This post has NOT been
accepted by the mailing list yet", so nobody gets it unless they look. 
You need to subscribe to the  mailing list
<http://forum.openscad.org/mailing_list/MailingListOptions.jtp?forum=1>  ,
and respond to the registration email.

I've copied it below for mailing-list readers


JohnMark wrote*
> It can be done! But it's not easy.
*
> I recommend you write and test your code in something easy like
> javascript.
> And then transcribe it into openSCAD compatible code.
> 
> Most functions you can code in JavaScript you can also code in OpenSCAD.
> But it's super hacky.
> 
> The two tricks you need to know:
*
> 1. Functions can call other functions.
> 2. Conditionals are allowed in functions.
*
> 
> I also recommend two monitors for getting this job done.
> One monitor I have my javascript code on that I work from.
> The other monitor I have the openSCAD code I am writing
> from the javascript reference.
> 
> Basically, just make a getter function for EVERY SINGLE VARAIBLE
> in your original function. For simplicity, every single getter should take
> the same arguments as the main function. Which, is really dirty and...
> not efficient code at all. But it will make things simpler for your brain.
> 
> Oh. One last thing.
> It's going to be a lot of code.
*
> So I recommend importing it with a "use" statement.
*
> I don't do an "include" because I usually put test-code at the bottom of
> the file
> and don't want that to execute in whatever file I am using it with.
> 
> And... Take the time and go slow.
> Make sure your javascript (or whatever language you use)
> code works perfectly. Test it!
> Then carefully transcribe it into openSCAD and test the openSCAD code
> with test functions as well.
> 
> This code is NOT going to be maintainable. But the good thing is, if you
> are writing
> mathematical functions. It doesn't need to be maintainable if you get it
> 100% correct.
> Because, for example, the logic for how to intersect a line and a circle
> will never change.
> 
> For example: Here is some of my circle-line collision code in openSCAD:
> //Use conditional to split off two two different main
> //Functions depending on if any edge cases were detected.
> function getPNCC(pX,pY,vX,vY,cX,cY,r) =
> 0 < getPNCC_HasEdgeCases(pX,pY,vX,vY,cX,cY,r)
> //--------------------------------------------//
> ?   getPNCC_TheEdgeCases(pX,pY,vX,vY,cX,cY,r,
>     getPNCC_HasEdgeCases(pX,pY,vX,vY,cX,cY,r) //<--supply edge case to
> func.
>                          )
> //--------------------------------------------//                         
> :   getPNCC_StandardCase(pX,pY,vX,vY,cX,cY,r);
> //--------------------------------------------//
> 
> //DETECTION, but NOT handling, of edge cases:
> //[ECD]++[ECD]++[ECD]++[ECD]++[ECD]++[ECD]++[ECD]++[ECD]++[ECD]//[|START|]
> //Each edge-case detection sub-function will return 0 or a postive
> //value indicating which edge case was triggered.
> function getPNCC_HasEdgeCases(pX,pY,vX,vY,cX,cY,r) = 
> max( 
>     //edge case #1: point at center of circle.
>     PNCC_EC_1(pX,pY,cX,cY),         //10,000
>     
>     //edge case #2: Parallel vectors, line passes through
>     //center-point of circle. But start is not at center of circle.
>     PNCC_EC_2(pX,pY,vX,vY,cX,cY,r), //2,000
>     
>     //edge case #3: dist_CD > r, AKA:
>     //Perpendicular distance from line to center of
>     //circle exceeds radius of circle.
>     PNCC_EC_3(pX,pY,vX,vY,cX,cY,r), //300
> 
>     //edge case #4: dist_CD === r, AKA: Tangency.
>     PNCC_EC_4(pX,pY,vX,vY,cX,cY,r)  //40
> );
> 
> function PNCC_EC_1(pX,pY,cX,cY)=
> (
>   (pX==cX) && (pY==cY)
> ) ? 
>   EC_01
>   : 
>   0;
> 
> function PNCC_EC_2(pX,pY,vX,vY,cX,cY,r)=
> (   
>   abs(PNCC_GET_u_dot_v(pX,pY,vX,vY,cX,cY,r))
>   ==
>   abs(PNCC_GET_prod_of_mags(pX,pY,vX,vY,cX,cY,r))
> ) ?
>   EC_02
>   :
>   0;
>   
> function PNCC_EC_3(pX,pY,vX,vY,cX,cY,r)=
> (
>   PNCC_GET_dist_CD(pX,pY,vX,vY,cX,cY,r) > r
> ) ?
>   EC_03
>   :
>   0;
>   
> function PNCC_EC_4(pX,pY,vX,vY,cX,cY,r)=
> (
>   PNCC_GET_dist_CD(pX,pY,vX,vY,cX,cY,r) == r
> ) ?
>   EC_04
>   :
>   0;
> //[ECD]++[ECD]++[ECD]++[ECD]++[ECD]++[ECD]++[ECD]++[ECD]++[ECD]//[|END|]





-----
Admin - PM me if you need anything, or if I've done something stupid...

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.

The TPP is no simple “trade agreement.”   Fight it! http://www.ourfairdeal.org/   time is running out!
--
View this message in context: http://forum.openscad.org/function-syntax-tp5915p18806.html
Sent from the OpenSCAD mailing list archive at Nabble.com.




More information about the Discuss mailing list