Source Code Examples

Source Code Examples

 

Top  Previous  Next

From the following example well copy the expressions for the location of point D (a vector) and distance AD (a scalar) in our code generation.

source code ex 

Scalar / No Intermediate Variables

  • Select the distance expression.
  • Right-click and select Copy As / Source Code / C/Objective C

In this case the code generates a single expression (here we are generating C):

(pow((pow(c,2)+((a+(b*-1))*c)),0.5)*pow((a+b+c),-0.5)*pow(a,0.5)).

Scalar / Intermediate Variables

With intermediate variables showing we get the following for the distance expression:

double distance(

    double a ,

    double b ,

    double c )

{

   double d_1;

   double v_1;

   double phi_0;

   double d_2;

   double d_0;

   double u_1;

   d_1 = (pow(((a*-1)+b+c),0.5)*pow((a+(b*-1)+c),0.5)*pow((a+b+(c*-1)),0.5)*pow((a+b+c),0.5));

   v_1 = (d_1*pow(a,-1)*0.5);

   phi_0 = ((a+b+c)*v_1*pow(c,-1)*pow(b,-1));

   d_2 = (v_1*pow(b,-1)*a*-1);

   d_0 = (pow(a,2)+(pow(b,2)*-1)+pow(c,2));

   u_1 = (d_0*pow(a,-1)*0.5);

   return (pow(fabs(phi_0),-1)*fabs(d_2)*pow((pow(c,2)+(u_1*c*2)+pow(u_1,2)+pow(v_1,2)),0.5)*pow(c,-1));

}

We see that the name of the function is the name of the expression in Geometry Expressions, its parameters are the input variables, and its return value is the value of the expression.

Vector / Intermediate Variables

Select the location expression, a vector value, and our function returns two quantities.  This is done in different ways for different languages:

C / Objective C

Pointers to doubles are passed into the function:

void location(

    double a ,

    double b ,

    double c ,

    double *location_x_ ,

    double *location_y_ )

{

   double location_x;

   double location_y;

   double d_1;

   double v_1;

   double d_2;

   double d_0;

   double u_1;

   double phi_0;

   d_1 = (pow(((a*-1)+b+c),0.5)*pow((a+(b*-1)+c),0.5)*pow((a+b+(c*-1)),0.5)*pow((a+b+c),0.5));

   v_1 = (d_1*pow(a,-1)*0.5);

   d_2 = (v_1*pow(b,-1)*a*-1);

   d_0 = (pow(a,2)+(pow(b,2)*-1)+pow(c,2));

   u_1 = (d_0*pow(a,-1)*0.5);

   phi_0 = ((a+b+c)*v_1*pow(c,-1)*pow(b,-1));

   location_x = (((d_2*-1)+(u_1*d_2*pow(c,-1)*-1))*pow(phi_0,-1));

   location_y = (pow(phi_0,-1)*v_1*d_2*pow(c,-1)*-1);

   *location_x_ = location_x;

   *location_y_ = location_y;

}

C++

References are passed into the function:

void location(

    double a ,

    double b ,

    double c ,

    double &location_x_ ,

    double &location_y_ )

{

   double location_x;

   double location_y;

...

   location_x_ = location_x;

   location_y_ = location_y;

}

C Sharp

Out parameters are passed into the function:

void location(

    double a ,

    double b ,

    double c ,

    out double location_x_ ,

    out double location_y_ )

{

   double location_x;

   double location_y;

...

   location_x_ = location_x;

   location_y_ = location_y;

}

Visual Basic / VBA

References are passed into the function:

Sub z_0(ByVal a As Double , ByVal b As Double , ByVal c As Double , ByRef z_0_x_  As Double  ,ByRef z_0_y_ As Double  )

   Dim z_0_x As Double

   Dim z_0_y As Double

   z_0_x_ = z_0_x

   z_0_y_ = z_0_y

End Sub

Java

An array of doubles of size 2 is passed in and populated by the function:

double location(

    double a ,

    double b ,

    double c ,

    double[] location_v )

{

double location(

    double a ,

    double b ,

    double c ,

    double[] location_v )

{

   double location_x = 0;

   double location_y = 0;

...

   location_v[0] = location_x;

   location_v[1] = location_y;

}

JavaScript

An array is passed in and populated by the function:

function location(

    a ,

       b ,

    c ,

    location_v )

{

   var location_x = 0;

   var location_y = 0;

...

   location_v[0] = location_x;

   location_v[1] = location_y;

}

Action Script

An array is passed in and populated by the function:

public function location(

    a:Number ,

    b:Number ,

    c:Number ,

    location_v:Array) )

:void

{

   var location_x:Number;

   var location_y:Number;

   location_v[0] = location_x;

   location_v[1] = location_y;

}