• No results found

The arc and arcn operators are used to draw arcs and circles. Their syntax is:

xc yc radius ∞∞∞∞begin˚ finish˚ arc xc yc radius ∞∞∞∞begin˚ finish˚ arcn

Where:

xc is the center of the arc/circle on the x axis.

yc is the center of the arc/circle on the y axis.

radius is the radius of the arc/circle.

begin˚ is the beginning point of the path.

finish˚ is the finishing point of the path.

∞ ∞ ∞

∞The difference between arc and arcn is the direction of the drawing from

∞ ∞ ∞

begin˚ to finish˚. See figure 6–1 below. The circle is divided into degree points in the counterclockwise direction from the three o’clock position. The same points are used by both arc and arcn. With arc, the direction of begin˚ to

finish˚ is in the counterclockwise direction, with arcn it is clockwise. The 0˚ and 360˚ points share the three o’clock position. Therefore, both 0 360 arc and 0 360 arcn could be used to draw a circle.

Neither arc nor arcn needs an existing current made by a moveto the way

lineto does. However, they can take advantage of a moveto as will be shown later in arc_2.eps. If there is an existing current point, it will attach itself with a straight line to the begin˚. To insure against an unwanted line, clear the current path with the operator newpath.

6

chapter

chapter

46

drawing basics

Here are three arcs drawn with arc.

%!PS-Adobe-2.0 EPSF-1.2 %%Title:arc_1.eps %%BoundingBox:34 34 256 110 3 setlinewidth 72 72 36 0 270 arc stroke 144 72 36 225 90 arc stroke 216 72 36 0 135 arc stroke

Now, the same program again with the same arguments used in arc_1.eps, but substituting arcn. The opposite portion of the arc is drawn.

arc

arcn

0˚ & 360˚

xc yc

radius

90˚

270˚

figure 6–1 6–1

PS

learn

drawing basics

47

%!PS-Adobe-2.0 EPSF-1.2 %%Title:arcn_1.eps %%BoundingBox:34 34 256 110 72 72 36 0 270 arcn stroke 144 72 36 225 90 arcn stroke 216 72 36 0 135 arcn stroke

The two previous arc examples were drawn without the customary establishing of a current point. If there is no existing current, one is created at the location of

∞ ∞ ∞

begin˚, the starting point of the arc. If there is an existing current point, a line will be drawn to the point of ∞∞∞∞begin˚. Notice the difference in the following two PostScript programs when a current point is made at the center of the arc and

closepath is used.

%!PS-Adobe-2.0 EPSF-1.2 %%Title:arc_2.eps

%%BoundingBox:34 34 256 110

72 72 moveto

72 72 36 0 270 arc closepath stroke

144 72 moveto 6–2

PS

learn

6–3

PS

learn

48

drawing basics

216 72 36 0 135 arc closepath stroke

In all the arcs in the previous and next example, a current point is first made at the center of the arc. ∞begin˚ connects to that current point and therefore draws a line. After arc, the current point will be at ∞∞∞∞finish˚. Then, closepath draws a line to the original current point made with the earlier moveto.

In this example, both arc and arcn are used with fill.

%!PS-Adobe-2.0 EPSF-1.2 %%Title:arc_3.eps

%%BoundingBox:34 34 256 110

.5 setgray 72 72 moveto

72 72 36 0 270 arc closepath fill

.1 setgray 72 72 moveto

72 72 36 0 270 arcn closepath fill

.3 setgray 144 72 moveto

144 72 36 225 90 arc closepath fill

.8 setgray 144 72 moveto

144 72 36 225 90 arcn closepath fill

.5 setgray 216 72 moveto

216 72 36 0 135 arc closepath fill

.8 setgray 216 72 moveto

216 72 36 0 135 arcn closepath fill

PS

learn

drawing basics

49

making a pie chart using arc

Using arc to develop a pie chart is a good exercise in learning how to make use of the operand stack and a few of the PostScript math operators. The pie chart below is for the percentages of 43%, 25%, 17%, and 15%. This program example is written to handle any four percentages that add up to 100. The program for the filled version of the pie chart can be found in chapter 19.

%!PS-Adobe-2.0 EPSF-1.2 %%Title:pieChart_1.eps %%Creator:John F Sherman %%CreationDate:June 1990 %%BoundingBox:10 10 154 154 2 setlinewidth 10 10 translate

43 25 17 15 % the 4 percentages with space between

/p1 exch 3.6 mul def

/p2 exch 3.6 mul p1 add def /p3 exch 3.6 mul p2 add def /p4 exch 3.6 mul p3 add def

/x 72 def /y 72 def /r 72 def

/wedge{arc closepath stroke} def

x y moveto x y r 0 p1 wedge x y moveto x y r p1 p2 wedge x y moveto x y r p2 p3 wedge x y moveto x y r p3 p4 wedge

Looking at the first part of the program, 43 25 17 15 are the four percentages with a word space separating them. They can be any four percentages as long as

6.2

PS

learn

50

drawing basics

counterclockwise from the point. If we were to chart the stack of these two lines of code, it would look like figure 6–2.

In column one, the four numbers, /p1, and exch are entered onto the stack. exch

exchanges the top two items on the stack. Therefore, as we see in column two, /p1

and 15 have switched places on the stack. p1 will eventually be the name for our first point on the arc. Next in column three, 3.6 and mul are added to the stack.

mul multiplies the top two numbers on the stack and pushes (puts) the product onto the stack. We multiply by 3.6 because our four numbers are fractions of 100 and our pie wedges will be fractions of 360. In column four, 54 is on top from the multiplying of 3.6 by 15. Next comes def, which associates /p1 with the value 54. The steps are then repeated in the same way for points /p2, /p3, and /p4. However, with those points the new point will also need to have the value of the previous point added to it. We do this because we need to make our way around the circumference of the pie. Note that in defining the variable p1 the { } are not used. They are only used when defining procedures.

The next line of the program begins in the same way except for the addition of adding the value of p1 to definition of p2. Starting at where 17 times 3.6 equals 61.2 is pushed onto the stack, the stack would continue as shown in figure 6–3.

p1 and add are added to the stack. As you might guess, add adds the top two numbers on the stack. We defined p1 in the previous line to be 54. So we then have in column three p1 plus 61.2 equaling 115.2 pushed onto the stack. It is then given the name p2. Our stack now has two numbers left to process as p3 and p4. They are done in the same way as p2. We now have these points around the pie (see figure 6–4). exch /p1 15 17 25 43 15 /p1 17 25 43 mul 3.6 15 /p1 17 25 43 54 /p1 17 25 43 def 54 /p1 17 25 43 17 25 43 figure 6–2 61.2 /p2 25 43 add p1 (54) 61.2 /p2 25 43 115.2 /p2 25 43 def 115.2 /p2 25 43 25 43 figure 6–3

drawing basics

51

The next part of the program is:

/x 72 def /y 72 def /r 72 def

/wedge {arc closepath stroke} def

x and y determine the center of the pie chart. They will be used by both the

moveto and arc operators later in the program. r is the radius of the pie chart. Defining the x, y, and r variables like this gives us flexibility. We need to change only one number, not many, to make a change to the chart. wedge is the name for the procedure {arc closepath stroke}. This simplifies the program and helps it to be more readable.

Next in the program comes the actual drawing of the pie chart using the values and procedures defined earlier.

x y moveto

x y r 0 p1 wedge

These program lines draw the first wedge, and so forth.

To rewrite the program to work with five percentage numbers, add these lines:

/p5 exch 3.6 mul p4 add def and

x y moveto

x y r p4 p5 wedge

Follow this same pattern for each wedge that will be needed for the chart. Also, since we know that the last point must be 360, we could leave out the process of defining the last point and put in 360.

Related documents