Download presentation
Presentation is loading. Please wait.
Published byDoris Thompson Modified over 9 years ago
1
IS 1014 Introduction to Computer Graphics -- Paul Munro A Postscript Tutorial Book available at: http://www- cdf.fnal.gov/offline/PostScript/BLUEBOOK.PDF Postscript as a page description language –like HTML –text position, orientation font, style –figures position, orientation scaling coordinates pen control (move, line) PS as a programming language – like C++ – stack commands arithmetic operators – loops and conditionals – lines and shapes
2
IS 1014 Introduction to Computer Graphics -- Paul Munro The Stack A piece of memory set aside for immediate processing Store (push), retrieve (pop) LIFO “Last In -- First Out” Example: 12 6.3 -99 12 6.3 12 -99 6.3 12 Postscript stack
3
IS 1014 Introduction to Computer Graphics -- Paul Munro Stacks are not just for numbers a line of postscript code -- all objects that are not operators go onto the stack: mark /Font [1 2] (PS) (PS) [1 2] /Font mark
4
IS 1014 Introduction to Computer Graphics -- Paul Munro Stack Arithmetic 4444 554554 add 9 4 5 add 3.1 2 3.1 mul 6.2 3.1 2 mul 22652265 div 3 5 add 8 5 6 2 div add 665665 5555
5
IS 1014 Introduction to Computer Graphics -- Paul Munro Arithmetic Operators div idiv mod mul neg add sub 5 2 div = 5 2 idiv = 5 2 mod = 5 2 mul = 5 neg = 5 2 add = 5 2 sub = 2.5 2 1 10 -5 7 3
6
IS 1014 Introduction to Computer Graphics -- Paul Munro Stack Operators CBACBA clear CCBACCBA CBACBA dup BCABCA CBACBA exch BABA CBACBA pop
7
IS 1014 Introduction to Computer Graphics -- Paul Munro Graphics in Postscript construct a path on the current page coordinates are in points (72 pt = 1 inch) (0,0) is the lower left corner of the page newpath begins a path description moveto, rmoveto move the pen (up) lineto, rlineto draw with the pen (down) stroke places the marks on the virtual paper showpage renders the marks
8
IS 1014 Introduction to Computer Graphics -- Paul Munro An example (0,0) newpath 72 360 moveto 144 72 rlineto 144 432 moveto 0 -216 rlineto stroke showpage
9
IS 1014 Introduction to Computer Graphics -- Paul Munro Another example (a box) newpath 270 360 moveto 0 72 rlineto 72 0 rlineto 0 -72 rlineto -72 0 rlineto 4 setlinewidth stroke showpage closepath
10
IS 1014 Introduction to Computer Graphics -- Paul Munro Filled Shapes (a filled box) newpath 270 360 moveto 0 72 rlineto 72 0 rlineto 0 -72 rlineto closepath fill showpage
11
IS 1014 Introduction to Computer Graphics -- Paul Munro A shaded box newpath 270 360 moveto 0 72 rlineto 72 0 rlineto 0 -72 rlineto closepath.5 setgray fill showpage
12
IS 1014 Introduction to Computer Graphics -- Paul Munro A buncha boxes newpath %Begin black box 252 324 moveto 0 72 rlineto 72 0 rlineto 0 -72 rlineto closepath fill newpath %Begin gray box 270 360 moveto 0 72 rlineto 72 0 rlineto 0 -72 rlineto closepath.4 setgray fill newpath %Begin lighter box 288 396 moveto 0 72 rlineto 72 0 rlineto 0 -72 rlineto closepath.8 setgray fill showpage %Send to printer
13
IS 1014 Introduction to Computer Graphics -- Paul Munro Path Construction Operators closepath Closes the current path with a straight line to the last moveto point x y lineto Continue the path with line to (x,y) x y moveto Set the current point to (x,y) newpath Clear the current path x y rlineto Relative lineto (currentpoint + (x,y)) x y rmoveto Relative moveto
14
IS 1014 Introduction to Computer Graphics -- Paul Munro Painting Operators fill Fill current path with the current color n setgray Set the current color n setlinewidth Set the current line width stroke Paint the current path with the current color and line width An Output Operator showpage Transfer the current page to the output device
15
IS 1014 Introduction to Computer Graphics -- Paul Munro Variables /ppi 72 def 5555 ppi 72 5 mul 360 5 ppi mul example:
16
IS 1014 Introduction to Computer Graphics -- Paul Munro Procedures In Postscript, procedure definitions use the same syntax as variable definitions: /inch {72 mul} def whenever “inch” appears, it is replaced with “72 mul” 6 inch => 6 72 mul => 432 example:
17
IS 1014 Introduction to Computer Graphics -- Paul Munro Three Boxes Again % --------- Begin Program ----------- newpath % First box 252 324 moveto box 0 setgray fill newpath % Second box 270 360 moveto box.4 setgray fill newpath % Third box 288 396 moveto box.8 setgray fill showpage %Black box newpath 252 324 moveto 0 72 rlineto 72 0 rlineto 0 -72 rlineto closepath fill %Gray box newpath 270 360 moveto 0 72 rlineto 72 0 rlineto 0 -72 rlineto closepath.4 setgray fill %Begin lighter box newpath 288 396 moveto 0 72 rlineto 72 0 rlineto 0 -72 rlineto closepath.8 setgray fill % ----- Define box procedure --- /box { 72 0 rlineto 0 72 rlineto -72 0 rlineto closepath } def
18
IS 1014 Introduction to Computer Graphics -- Paul Munro Three Boxes Yet Again % ------- Define procedures---- /inch {72 mul} def /box % stack: x y => --- { newpath moveto 1 inch 0 rlineto 0 1 inch rlineto -1 inch 0 rlineto closepath } def /fillbox % stack: grayvalue => --- { setgray fill } def % ----------- Main Program ----------- 3.5 inch 4.5 inch box 0 fillbox 3.75 inch 5 inch box.4 fillbox 4 inch 5.5 inch box.8 fillbox showpage
19
IS 1014 Introduction to Computer Graphics -- Paul Munro Translating Space 100 200 translate
20
IS 1014 Introduction to Computer Graphics -- Paul Munro Translation example /square %procedure to draw a { newpath % filled square 0 0 moveto 90 0 lineto %define a square path 90 90 lineto 0 90 lineto closepath fill %fill it } def square %do a square 200 250 translate %move coord. sys. square %do another square 200 250 translate %and move again square %do a third square showpage
21
IS 1014 Introduction to Computer Graphics -- Paul Munro Rotation 45 rotate this statement will rotate the user coordinate system 45 degrees:
22
IS 1014 Introduction to Computer Graphics -- Paul Munro Rotation Example /square %procedure from { newpath % previous program 0 0 moveto 90 0 lineto 90 90 lineto 0 90 lineto closepath fill 6 92 moveto %Label the box } def square %do a square 300 150 translate %move coord. sys. 60 rotate %and rotate it square %do it again... 300 150 translate 60 rotate square %do a third square showpage
23
IS 1014 Introduction to Computer Graphics -- Paul Munro Scaling The scale operator rescales the two coordinate axes 1.5 2 scale
24
IS 1014 Introduction to Computer Graphics -- Paul Munro Scaling example /square %procedure to draw a { newpath % filled square 0 0 moveto 90 0 lineto 90 90 lineto 0 90 lineto closepath fill 6 92 moveto %Label the box } def square %do a square 100 100 translate 1.5 1.5 scale square 100 100 translate.75 1.25 scale %non-uniform scaling square showpage
25
IS 1014 Introduction to Computer Graphics -- Paul Munro Conditionals and loops eq ne lt gt le ge if ifelse loop exit for
26
IS 1014 Introduction to Computer Graphics -- Paul Munro if bool {commands} if /chkforendofline { currentpoint pop %get x-position 612 gt %greater than 612? {0 -12 translate 0 0 moveto} if } def
27
IS 1014 Introduction to Computer Graphics -- Paul Munro if else % ------- Variables & Procedures --------- /scalefactor 1 def /counter 0 def /DecreaseScale { scalefactor.2 sub /scalefactor exch def } def /IncreaseCounter { /counter counter 1 add def } def /trappath %construct a trapezoid { 0 0 moveto 90 0 rlineto -20 45 rlineto -50 0 rlineto closepath } def /doATrap { gsave 1 scalefactor scale %scale vert. axis trappath %construct path counter 2 mod %is counter even? 0 eq {.5} {0} ifelse %choose grey or black setgray fill grestore } def %restore scale, etc. % ------------ Begin Program ---------- 250 350 translate 5 {IncreaseCounter doATrap DecreaseScale 0 20 translate } repeat showpage bool {cmds A} {cmds B} ifelse
28
IS 1014 Introduction to Computer Graphics -- Paul Munro loop { cmds } loop /pagewidth 8.5 72 mul def /doCircle { xpos ypos radius 0 360 arc stroke} def /increase-x { xpos radius add /xpos exch def } def 7.2 LOOPS 69 /lineofcircles %stack: radius y { /ypos exch def %define ypos /radius exch def %...& radius /xpos 0 def %...& xpos {xpos pagewidth le %begin loop {doCircle increase-x} {exit} ifelse }loop %end loop } def %end definition % --------------- Begin Program ----------- 10 400 lineofcircles 30 400 lineofcircles 90 400 lineofcircles showpage
29
IS 1014 Introduction to Computer Graphics -- Paul Munro for a inc b {cmds} for % ----- Define box procedure --- /box { 72 0 rlineto 0 72 rlineto -72 0 rlineto closepath } def 50 50 moveto 1 1 10 {rmoveto 30 60 box} showpage
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.