NAME

Graphics::Grid::UnitArithmetic - Expression created from Graphics::Grid::Unit objects

VERSION

version 0.001

SYNOPSIS

use Graphics::Grid::UnitArithmetic;
use Graphics::Grid::Unit;

my $ua1 = Graphics::Grid::UnitArithmetic->new(
    node     => '+',
    children => [
        Graphics::Grid::Unit->new( [ 1, 2, 3 ], "cm" ),
        Graphics::Grid::Unit->new(0.5),
    ],
);
my $ua2 = Graphics::Grid::UnitArithmetic->new(
    Graphics::Grid::Unit->new(0.1, "npc")
);
my $ua3 = $ua1 - $ua2;
my $ua4 = $ua1 * 2;

# or use the function interface
use Graphics::Grid::Functions qw(:all);
my $ua = unit(2, 'inches') * 2;

DESCRIPTION

You would mostly never directly use this class. See Graphics::Grid::Unit for unit arithmetic on unit objects.

This class Graphics::Grid::UnitArithmetic represents arithmetic on Graphics::Grid::Unit objects. It provides a way to create a unit-like value that combines both relative and absolute values.

Supported operators are +, -, and *. A plus or minus operation requires both its binary operands are consumers of Graphics::Grid::UnitLike. The multiply operation requires one of its operands is consumer of Graphics::Grid::UnitLike, the other a number or array ref of numbers.

ATTRIBUTES

node

It can be one of the followings,

  • A number or an array ref of numbers. If a single number is given it will be coerced to an array ref that contains the number. This is used for operands of multiplications.

  • One of "+", "-", "*". In this case the children attr should contain the operands.

  • A Graphics::Grid::Unit object. This is used for operands of plus and minus.

children

When the object represents an arithmetic operation, this children attribute has the operands.

METHODS

elems

Number of effective values in the object.

length

This is an alias of elems().

at($idx)

This method returns an object of the same Graphics::Grid::UnitArithmetic class. The returned object represents the data at given index, and has at only one value at each node.

# $ua1 has 3 elems: 1cm+0.5npc, 2cm+0.5npc, 3cm+0.5npc
my $ua1 = Graphics::Grid::UnitArithmetic->new(
    node     => '+',
    children => [
        Graphics::Grid::Unit->new( [ 1, 2, 3 ], "cm" ),
        Graphics::Grid::Unit->new(0.5),
    ],
);

# $ua2 has only 2cm+0.5npc
my $ua2 = $u1->at(1);

$idx is applied like wrap-indexing. So below is same as above.

my $ua3 = $ua1->at(4);

slice($indices)

Slice by indices.

string()

Returns a string representing the object.

sum()

Sum the effective unit vector in a unit object.

append($other)

Append two UnitLike objects. If both are Graphics::Grid::Unit objects, this method would return a Graphics::Grid::Unit object. Otherwise, it would return a Graphics::Grid::UnitList object.

insert($other, $after=$self->elems-1)

Insert another UnitLike object after index $after. Insert before first element if after is a negative value.

is_unit

Checks if the object is a Graphics::Grid::Unit.

is_number

Checks if the object is an array ref of numbers.

is_arithmetic

Check is the object is an arithmetic operation. It is equivalent to !($obj-≥is_unit() or $obj-≥is_number()).

reduce()

Try to reduce the tree by evaluating arithmetics. Returns a new Graphics::Grid::UnitArithmetic or Graphics::Grid::Unit object.

SEE ALSO

Graphics::Grid

Graphics::Grid::UnitLike

Graphics::Grid::Unit

Forest::Tree

AUTHOR

Stephan Loyd <sloyd@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2018-2023 by Stephan Loyd.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.