Re: Defining and Programming Graphical Directives

• To: mathgroup at smc.vnet.net
• Subject: [mg39236] Re: [mg39232] Defining and Programming Graphical Directives
• From: Andrzej Kozlowski <andrzej at platon.c.u-tokyo.ac.jp>
• Date: Wed, 5 Feb 2003 00:11:06 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

```Here is one approach, which is far form perfect, but probably could be
developed to do what you want.

The idea is basically to parse your expression before applying plotting
lines and replace things like

{LineColor[Red],
ColoredLine[{0, 1}, {1, 1}]}

by

Block[{currentcolor=Red}, ColoredLine[{0, 1}, {1, 1}]

Here is a function that will do this, at least in simple cases:

SetAttributes[ff,HoldAll]

ff[expr_]:=Hold[expr]/.HoldPattern[{y___,LineColor[c_],x___}]:>
Block[{currentcolor=c},{y,x}]/;FreeQ[HoldPattern[{x,y}],LineColor]

Using your definitions both:

PlottingLines@@ff[
{ColoredLine[{0, 0}, {1, 0}],
LineColor[Red],
ColoredLine[{0, 1}, {1, 1}],
LineColor[Blue],
ColoredLine[{0, 2}, {1, 2}]}]
and

PlottingLines@@ff[
{ColoredLine[{0, 0}, {1, 0}],
{LineColor[Red],
ColoredLine[{0, 1}, {1, 1}]},
ColoredLine[{0, 2}, {1, 2}]}]

seem now to work correctly.

Andrzej Kozlowski
Yokohama, Japan
http://www.mimuw.edu.pl/~akoz/
http://platon.c.u-tokyo.ac.jp/andrzej/

On Tuesday, February 4, 2003, at 04:23 PM, David Park wrote:

> Dear MathGroup,
>
> I am working on an application where I define a number of graphical
> objects,
> which can then be used like graphical primitives. I also want to define
> graphical primitives that will control the color, position and size of
> some
> of these objects. The use of color is slightly complicated because the
> color
> that is set may be used for lines, or in SurfaceColor or in EdgeForm
> and it
> may be lightened or darkened.
>
> My basic approach is to store the value of the directives in internal
> variables and then the graphical primitives use these values.
>
> But it is difficult to make it all behave just like regular Mathematica
> graphical directives.
>
> 1) The list of primitives must be HoldFirst. Premature evaluation will
> cause
> the last instance of a programmed directive to be used for all cases.
> 2) The default values of any primitives must be set before the
> primitives
> are evaluated else whatever is left over from the last plot will be
> used.
>
> I can pretty much handle those problems. The last problem is more
> difficult
> and is the core of my question.
>
> 3) Regular graphics directives are nested in the sense that if one is
> set
> inside brackets, {directive,... }, then, after exiting the brackets,
> the
> directive reverts to the value existing before the brackets were
> entered.
>
> Is it possible to program that behavior?
>
> Here is an example. It could be done with regular Mathematica
> directives,
> but it was picked to be a simple example. In my actual cases I have to
> use
> programming. The extra graphical primitive is a ColoredLine. The color
> to be
> used for subsequent ColoredLines is set with the programmed directive
> LineColor. The value is stored in currentcolor.
>
> Needs["Graphics`Colors`"]
>
> currentcolor = Black;
> ColoredLine[start_, end_] := {currentcolor, Line[{start, end}]}
> LineColor[color_] := (currentcolor = color; Sequence[])
>
> Attributes[PlottingLines] = {HoldFirst};
> PlottingLines[primitives_List, opts___?OptionQ] :=
>   Show[Graphics[
>       {LineColor[Black], Sequence @@ primitives}], opts]
>
> In the following use everything works.
>
> PlottingLines[
>     {ColoredLine[{0, 0}, {1, 0}],
>       LineColor[Red],
>       ColoredLine[{0, 1}, {1, 1}],
>       LineColor[Blue],
>       Coloredline[{0, 2}, {1, 2}]},
>     Background -> Linen,
>     ImageSize -> 400];
>
> But if I use nesting it, of course, doesn't work like regular
> Mathematica
> directives. The color does not revert to Black.
>
> PlottingLines[
>     {ColoredLine[{0, 0}, {1, 0}],
>       {LineColor[Red],
>         ColoredLine[{0, 1}, {1, 1}]},
>       Coloredline[{0, 2}, {1, 2}]},
>     Background -> Linen,
>     ImageSize -> 400];
>
> It appears that I need to somehow implement a recursive evaluation of
> the
> primitives list. Any ideas on how I could do that.
>
> David Park
> djmp at earthlink.net
>
>
>
>
>
>
>

```

• Prev by Date: Re: Options[] creation
• Next by Date: On-line linear programming application
• Previous by thread: RE: Defining and Programming Graphical Directives
• Next by thread: Posting for MathGroup