OMD Documentation

omdPowerNode

Represents an exponentiation in a mathematical expression, such as x^2 or (a+b)^c. This node handles the specific layout requirements of a base and a superscripted exponent, ensuring correct visual hierarchy and alignment.

Class Definition

export class omdPowerNode extends omdNode

Constructor

new omdPowerNode(ast)

Creates a new omdPowerNode instance.

Static Methods

fromString(expressionString)

Creates an omdPowerNode from a string representation of a power expression. Requires window.math (math.js) to be available globally for parsing.

Public Properties

Public Methods

computeDimensions()

Calculates the dimensions of the power node. It computes the dimensions of the base and exponent (scaling the exponent's font size down). The total width is the sum of their widths, and the total height accounts for the base's height plus the vertical offset needed for the superscripted exponent.

updateLayout()

Updates the layout of the power node. It positions the base at the bottom of the node's bounding box to ensure space for the exponent above it. The exponent is then positioned to the right of the base and shifted upwards by a calculated superscript offset.

getSuperscriptOffset()

Calculates the vertical offset (in pixels) by which the exponent should be raised above the base. This offset is a proportion of the current font size to ensure consistent scaling.

getAlignmentBaseline()

Calculates the vertical alignment point for the node. For a power node, the baseline is determined by the baseline of its base expression, ensuring proper alignment with other mathematical elements.

clone()

Creates a deep, structural clone of the power node, including its base and exponent nodes. The cloned node's provenance array is updated to include the original node's ID.

toMathJSNode()

Converts the omdPowerNode back into its math.js AST representation. It creates an OperatorNode with op: '^' and fn: 'pow', containing the converted base and exponent ASTs.

toString()

Converts the node to its string representation, adding parentheses where necessary to preserve the correct order of operations for both the base and the exponent.

evaluate(variables)

Evaluates the power expression by raising the evaluated base to the power of the evaluated exponent. It handles cases where base or exponent might not be numerical.

isSquare()

Checks if the exponent of the power node is the constant value 2.

isCube()

Checks if the exponent of the power node is the constant value 3.

Internal Methods

Example

import { omdPowerNode } from '@teachinglab/omd';
import * as math from 'mathjs';

// Create a node representing x^2
const ast = math.parse('x^2');
const powerNode = new omdPowerNode(ast);

// Set font size and render
powerNode.setFontSize(32);
powerNode.initialize();

// Check properties
console.log(powerNode.isSquare()); // true

// Evaluate
console.log(powerNode.evaluate({ x: 5 })); // 25

// Add to an SVG container to display
// const svgContainer = new jsvgContainer();
// svgContainer.addChild(powerNode);
// document.body.appendChild(svgContainer.svgObject);

See Also

↑ Top