FORMULA_CURVE
This element creates a curve defined by a mathematical formula (y=f(x)).
Attributes brief detailed show all inherited
See the CURVE element attributes. The FORMULA_CURVE element does not have the persistent attribute since its value is computed automatically.
Examples
A simple FORMULA_CURVE and other elements used to display that curve on a graph nicely.
- <!-- demo of a formula curve -->
- <FORMULA_CURVE id="formula_curve_demo" x_min="-50" x_max="50" x_default="0" y_default="0" y_max="1" y_min="-1" formula="sin(x)/x" />
- <!-- display the curve on a graph -->
- <PNS_LCD_GROUP_BOX>
- <!-- grid -->
- <GRID id="grid_1" width="100%" height="100%" x_positions_count="11" x_subdiv_count="1" y_positions_count="3" y_subdiv_count="3" color="#CCCCCC" subdiv_color="#999999" origin_color="#FFFFFF" line_width="2" subdiv_line_width="1" origin_line_width="3" x_param_info_id="formula_curve_demo.x_info" y_param_info_id="formula_curve_demo.y_info" opacity="0.2" />
- <!-- first graph for the line (stroke) -->
- <CURVE_GRAPH curve_id="formula_curve_demo" width="500" height="200" color="#327ca3" graph_style="stroke" opacity="1" line_width="4" />
- <!-- second line for the opaque bargraph -->
- <CURVE_GRAPH curve_id="formula_curve_demo" width="500" height="200" color="#327ca3" graph_style="bargraph" opacity="0.2" line_width="4" />
- <!-- horizontal ruler -->
- <RULER id="graph_ruler_x" orientation="horizontal" width="100%" value_suffix="false" param_info_id="formula_curve_demo.x_info" color="#AAAAAA" value_format=".1" content="{value}" positions_count="11" font_escapement="0" text_h_align="center" v_offset="0" v_align="bottom" h_offset="-1" font_size="12" />
- <!-- vertical ruler -->
- <RULER id="graph_ruler_y" orientation="vertical" height="100%" width="50" value_suffix="false" param_info_id="formula_curve_demo.y_info" color="#999999" value_format=".1" content="{value}" positions_count="3" font_escapement="0" text_v_align="top" h_align="left" text_h_align="left" h_offset="3" v_offset="0" font_size="12"/>
- </LAYER_STACK>
- </PNS_LCD_GROUP_BOX>

Changing variable name
Just for "cosmetic" purposes we can change the name of the variable used in the formula (instead of default "x"), so that it's maybe easier to read.
- <FORMULA_CURVE id="formula_curve_demo" x_min="-50" x_max="50" x_default="0" y_default="0" y_max="1" y_min="-1" formula="sin(pos)/pos" variable="pos" />
Complex curves
You can create complex formulas for complex curves using methods like this:
- <!-- stepped formula curve -->
- <FORMULA_CURVE id="formula_curve_demo" x_min="0" x_max="7" x_default="0" y_default="0" y_max="5" y_min="-5" formula="0 + 1*(x>=1)*(x<2) + 2*(x>=2)*(x<3) + -3*(x>=3)*(x<4) + -1*(x>=5)*(x<6)" />

A smoothed curve can be achieved using tahh or similar functions.
- <!-- stepped formula curve -->
- <FORMULA_CURVE id="formula_curve_demo" x_min="0" x_max="7" x_default="0" y_default="0" y_max="5" y_min="-5" formula="0 + ((1+tanh(k*(x - 1)))/2)*(1-0) + ((1+tanh(k*(x - 2)))/2)*(2-1) + ((1+tanh(k*(x - 3)))/2)*(-3-2) + ((1+tanh(k*(x - 4)))/2)*(0--3) + ((1+tanh(k*(x - 5)))/2)*(-1+0) + ((1+tanh(k*(x - 6)))/2)*(0--1) " />
- <!-- smoothing value -->
- <PARAM id="k" min="2" max="30" exposed="true" default="7" />
- <!-- control -->
- <PARAM_TEXT_CONTROL param_id="k" />

You can take a look at this script that does similar stuff in a more complex/organized manner.

Keep in mind that drawing complex curves (and other graphics) can be done using CANVAS.
Related elements
See CURVE_GRAPH, CURVE_FROM_PARAM_OVER_TIME, GRID, RULER, CURVE, CURVE_LINK, CANVAS.
Comments
Please, authorize to view and post comments.