BookmarkSubscribeRSS Feed
tc
Lapis Lazuli | Level 10 tc
Lapis Lazuli | Level 10

XMASTREE.png

 

Truth be told, the underlying concept here isn't all that different than Rick Wicklin's 2011 throwback ASCII Xmas tree, but with shaded boxes ("presents") taking the place of ASCII characters in an effort to mimic some of the nice polygon Christmas tree art work floating about the web. Happy Holidays, all!

 

CODE

* Fun w/SAS ODS Graphics: Xmas tree made up of Xmas presents;

*==> Generate polygon vertices for a "Xmas present" (each face will be shaded differently); 

data XmasPresent;
angle=30*constant("PI")/180;                  * Add some perspective;
id=1;                                         * Right face;
x=0; y=1; output;
x=0; y=0; output;
x=cos(angle); y=sin(angle); output;
y+1; output;
id=2;                                         * Left face;
x=0; y=1; output;
x=0; y=0; output;
x=-cos(angle); y=sin(angle); output;
y+1; output;
id=3;                                         * Top face;
x=0; y=1; output;
x=-cos(angle); y=sin(angle)+1; output;
x=0; y=sin(angle)+1+sin(angle); output;
x=cos(angle); y=sin(angle)+1; output;
run;

data XmasPresent;                             * Add sequence # for plotting;
set XmasPresent;
seq+1;

*==> Generate points for presents that will make up the "Xmas tree"; 

data xmastree;                                
do row=12 to 3 by -1;                         * Tree is 10 rows (first row is 1 present, last is 10 presents); 
  yoffset=row*2*.875;                         * Adjust row height (87.5%) to tighten things up;
  do col=0 to 9;
    xoffset=^mod(row,2)+col*2+1;              * Offset each row's columns by 1;
    idoffset+3;
    rndcolor=put(ceil(7*ranuni(14)),1.);      * Assign one of 7 random colors to present; 
    if (row-3)<=xoffset<=(10.5+(12-row)) then
      output;                                 * Output only desired # points for row (1, 2, ..., 10);
  end;
end;
row=2;                                        * Generate "trunk" (2 rows of 3/2 presents);
yoffset=row*2*.875;
do col=3 to 5;
  xoffset=2+col*2;
  idoffset+3;
  rndcolor=put(ceil(7*rand("Uniform")),1.); 
  output;
end;
row=1;
yoffset=row*2*.875;
do col=4 to 5;
  xoffset=mod(row,2)+col*2;
  idoffset+3;
  rndcolor=put(ceil(7*rand("Uniform")),1.); 
  output;
end;
row=0;                                        * Generate "stand" (1 row of 5 presents);
yoffset=row*2*.875;
do col=2 to 6;
  xoffset=2+col*2;
  idoffset+3;
  rndcolor=put(ceil(7*rand("Uniform")),1.); 
  output;
end;

*==> Merge Xmas tree points with Xmas present polygon points; 

proc sql;
create table xmastreepresents as 
select x+xoffset as x, y+yoffset as y,                       /* Keep present faces separate for shading */
       case when id=1 then idoffset+id else . end as newid,  /* Right face */
       case when id=2 then idoffset+id else . end as newid2, /* Left face */
       case when id=3 then idoffset+id else . end as newid3, /* Top face */
       RndColor
from xmastree x, xmaspresent p order by idoffset+id, seq;

*==> Hard work's done, let's plot a Xmas tree! (transparency used to shade present faces); 

ods listing image_dpi=300 gpath='/folders/myfolders';
ods graphics on / reset antialias width=5in height=5in imagename="XMASTREE";

proc template;
define statgraph xmastemplate;
  begingraph;
    entryfootnote  "HAPPY HOLIDAYS!" / textattrs=(size=14pt weight=bold);     
    discreteAttrMap name='PresentColor';     * Translate random color #'s to colors;
      value '1' / fillattrs=(color=blue);
      value '2' / fillattrs=(color=red);
      value '3' / fillattrs=(color=green);
      value '4' / fillattrs=(color=purple);
      value '5' / fillattrs=(color=orange);
      value '6' / fillattrs=(color=hotpink);
      value '7' / fillattrs=(color=black);
    endDiscreteAttrMap;
    discreteAttrVar attrVar=PresentC var=RndColor attrMap="PresentColor";
    layout overlayequated / border=false xaxisopts=(display=(line ticks tickvalues)) 
                            yaxisopts=(display=(line ticks tickvalues));
      polygonplot id=newid x=x y=y / display=(fill) group=presentc fillATTRS=(transparency=.75) INCLUDEMISSINGGROUP=FALSE;
      polygonplot id=newid2 x=x y=y / display=(fill) group=presentc fillATTRS=(transparency=.45) INCLUDEMISSINGGROUP=FALSE;
      polygonplot id=newid3 x=x y=y / display=(fill) group=presentc fillATTRS=(transparency=0) INCLUDEMISSINGGROUP=FALSE;
    endlayout;
  endgraph;
end;

proc sgrender data=xmastreepresents template=xmastemplate;
run;

 

4 REPLIES 4
Jay54
Meteorite | Level 14

Another good one, Ted.   I suggest removing the axes and wall border.

tc
Lapis Lazuli | Level 10 tc
Lapis Lazuli | Level 10

Good suggestion - revised LAYOUT statement and image below!

 

XMASTREE.png

 

layout overlayequated / xaxisopts=(display=none) yaxisopts=(display=none) walldisplay=none;
ChrisHemedinger
Community Manager

A Q*bert Christmas!

 

from http://www.hardcoregaming101.net/

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 2753 views
  • 12 likes
  • 4 in conversation