2
$\begingroup$

Both of these inputs seem to give exactly the same output:

Graphics3D[{Red, Cuboid[{-1, -1, -1}], Green, Cuboid[{1, 1, 1}]}]
Graphics3D[{{Red, Cuboid[{-1, -1, -1}]}, {Green, Cuboid[{1, 1, 1}]}}]

In the first, the argument to Graphics3D is a 1-dimensional list, and the scope of each colour directive seems to be the primitives that follow until the next directive is encountered. In the second, the argument is a 2-dimensional array, where directives have been safely packaged together with the primitive to which they are supposed to apply.

Is either form preferred ? Does it ever make any difference which one uses ?

$\endgroup$
1
  • 1
    $\begingroup$ Graphics and Graphics3D process their input in a bit different way then the "ordinary" language. As first approximation, you may assume that it applies Flatten onto its first argument (if the first argument is a list). So, IMHO, it just does not matter. $\endgroup$ Commented Sep 26, 2019 at 13:21

1 Answer 1

2
$\begingroup$

Graphics appear to follow the rules of object inheritance as follows:

The following command creates a Red rectangle with Blue edges then the second Green rectangle inherits the Blue edges of the first.

Graphics[{
    Red,
    EdgeForm[Blue],
    Rectangle[{-1,-1}],

    Green,
    Rectangle[{1,1}]
}]

We can overwrite the inherited blue edge for second by specifying explicitly.

Graphics[{
    Red,
    EdgeForm[Blue],
    Rectangle[{-1,-1}],

    EdgeForm[Orange],
    Green,
    Rectangle[{1,1}]
}]

Inheritance model goes deeper into the properties of the directives, for example here the thickness of the first EdgeForm is inherited by the second EdgeForm.

Graphics[{
    Red,
    EdgeForm[{Thickness[0.05],Blue}],
    Rectangle[{-1,-1}],

    EdgeForm[Orange],
    Green,
    Rectangle[{1,1}]
}]

If you don't want the rules of inheritance to apply to a certain directive you can enclose it with the graphics object then that directive will not move down the inheritance tree and apply to further incoming objects.

Graphics[{
    Red,
    {
        EdgeForm[{Thickness[0.05],Blue}],
        Rectangle[{-1,-1}]
    },

    EdgeForm[Orange],
    Green,
    Rectangle[{1,1}]
}]

These inheritance rules are necessary to make the code less verbose.

I have used Graphics for simplicity but the same inheritance rules should govern the Graphics3D as well.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.