⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch32.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 4 页
字号:
&nbsp;31 #&nbsp;&nbsp;&nbsp;$incr = increment<BR>

&nbsp;32 #&nbsp;&nbsp;&nbsp;$maxAngle = up to this angle.<BR>

&nbsp;33<BR>

&nbsp;34 sub tree {<BR>

&nbsp;35&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my ($h,$x,$y,$theta,$flen)

= @_; # local variables<BR>

&nbsp;36&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my ($nx,$ny,$nlen,$mycolor);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#

for drawing to.<BR>

&nbsp;37&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my ($i,$incr,$startAngle,$maxAngle);

# for the fanout<BR>

&nbsp;38&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my ($randFans);<BR>

&nbsp;39<BR>

&nbsp;40&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#<BR>

&nbsp;41&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Stopping conditions first.

<BR>

&nbsp;42&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#<BR>

&nbsp;43&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($h &lt; 1) {<BR>

&nbsp;44&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$im-&gt;filledRectangle($x-1,$base_y+$y-1,$x+2,$base_y+$y+2,$leaf);

<BR>

&nbsp;45&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return

0; }<BR>

&nbsp;46&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$nlen = $flen * $fanRatio;

<BR>

&nbsp;47&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($nlen &lt; 1) { return

0;}<BR>

&nbsp;48<BR>

&nbsp;49&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$mycolor = $brown;<BR>

&nbsp;50&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($h == 2) { $mycolor

= $color3; }<BR>

&nbsp;51&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($h == 3) { $mycolor

= $color2; }<BR>

&nbsp;52&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($h == 4) { $mycolor

= $color1; }<BR>

&nbsp;53&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($h == 5) { $mycolor

= $color0; }<BR>

&nbsp;54&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#<BR>

&nbsp;55&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Important!!!<BR>

&nbsp;56&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#<BR>

&nbsp;57&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$h -= 1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#

for recursion<BR>

&nbsp;58<BR>

&nbsp;59<BR>

&nbsp;60&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$maxAngle = $theta + $fanAngle;

<BR>

&nbsp;61&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$incr = ($maxAngle)/($maxfans

+ int(rand() * 3));<BR>

&nbsp;62<BR>

&nbsp;63&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$startAngle = $theta - ($fanAngle/2);

<BR>

&nbsp;64<BR>

&nbsp;65&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($startAngle &lt; $NPIBY2)

{ $startAngle = $NPIBY2; }<BR>

&nbsp;66&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($startAngle &gt; $NPIBY2)

{ $startAngle = $PIBY2;&nbsp;}<BR>

&nbsp;67<BR>

&nbsp;68&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# printf &quot;\n From %5.1f

--&gt; %5.1f, incr %5.1f&quot;,<BR>

&nbsp;69&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$startAngle,$maxAngle,

$incr;<BR>

&nbsp;70<BR>

&nbsp;71&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for ($i = $startAngle ;

$i &lt; $maxAngle; $i += $incr)<BR>

&nbsp;72&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{

<BR>

&nbsp;73&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$nx

= $nlen * sin($i);<BR>

&nbsp;74&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ny

= $nlen * cos($i);<BR>

&nbsp;75&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#

<BR>

&nbsp;76&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#

<BR>

&nbsp;77&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#

<BR>

&nbsp;78&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$im-&gt;line($x,$base_y+$y,$nx+$x,$base_y+$ny+$y,$mycolor);

<BR>

&nbsp;79&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tree($h,$nx+$x,$ny+$y,$i,$nlen);

<BR>

&nbsp;80&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

<BR>

&nbsp;81&nbsp;}<BR>

&nbsp;82<BR>

&nbsp;83 $height = 300;<BR>

&nbsp;84 $width&nbsp;&nbsp;= 300;<BR>

&nbsp;85 $maxfans = 3;<BR>

&nbsp;86 $maxdepth = 6;<BR>

&nbsp;87 $fanAngle = deg2rad(120);&nbsp;&nbsp;&nbsp;# Maximum

120 degrees<BR>

&nbsp;88 $fanRatio = 0.65;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#

Each new branch is 4/5 of old.<BR>

&nbsp;89 $base_y = $height / 2;<BR>

&nbsp;90 #<BR>

&nbsp;91 #<BR>

&nbsp;92 #<BR>

&nbsp;93 $start_x = $width/2;<BR>

&nbsp;94 $start_y = 25;<BR>

&nbsp;95 $start_length = 80;<BR>

&nbsp;96 $start_angle = deg2rad(80);<BR>

&nbsp;97<BR>

&nbsp;98 srand(time);<BR>

&nbsp;99<BR>

100 use GD;<BR>

101<BR>

102 $im = new GD::Image(300,300);<BR>

103 $backgd = $im-&gt;colorAllocate(154,192,205);<BR>

104 $white = $im-&gt;colorAllocate(255, 255, 255);<BR>

105 $black = $im-&gt;colorAllocate(0, 0, 0);<BR>

106 $brown = $im-&gt;colorAllocate(188,130, 130);<BR>

107 $leaf&nbsp;&nbsp;= $im-&gt;colorAllocate(157,255,12);<BR>

108 $color0 = $im-&gt;colorAllocate(245,196,90);<BR>

109 $color1 = $im-&gt;colorAllocate(95,158,60);<BR>

110 $color2 = $im-&gt;colorAllocate(46,139,87);<BR>

111 $color3 = $im-&gt;colorAllocate(152,251,152);<BR>

112 $red = $im-&gt;colorAllocate(255,0,0);<BR>

113 $blue = $im-&gt;colorAllocate(0,0,255);<BR>

114 $im-&gt;transparent($white);&nbsp;&nbsp;&nbsp;&nbsp;# white

color is transparent<BR>

115 $im-&gt;interlaced(1);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#

cool venetian blinds effect<BR>

116<BR>

117 tree ($maxdepth,$start_x,$start_y,$start_angle,$start_length);

<BR>

118<BR>

119<BR>

120 $text_y = $base_y / 2 + $width / 2;<BR>

121 $text_x = $width / 5;<BR>

122 $infoStr = &quot;Welcome to Kamran's Home Page&quot;;<BR>

123 $dateStr = &quot;Today is &quot; . `date`;<BR>

124 $message = &quot;&nbsp;&nbsp;http://www.ikra.com/index.html&quot;;

<BR>

125<BR>

126 $im-&gt;string(gdLargeFont,$text_x,$text_y,$infoStr,$red);

<BR>

127 $im-&gt;string(gdSmallFont,$text_x,$text_y + 30,$dateStr,$blue);

<BR>

128 $im-&gt;stringUp(gdLargeFont,10,$height - 10,$message,$blue);

<BR>

129<BR>

130<BR>

131 # print the image to stdout<BR>

132<BR>

133 $|=1;<BR>

134<BR>

135 print &quot;Content-type: image/gif \n\n&quot;;<BR>

136 print $im-&gt;gif;</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

Lines 4 through 12 define some of the constants for using degrees

and radians. The script uses the following global variables:

<UL>

<LI><TT><FONT FACE="Courier">$height</FONT></TT> and <TT><FONT FACE="Courier">$width</FONT></TT>

are the extents of the image.

<LI><TT><FONT FACE="Courier">$maxfans</FONT></TT> is the maximum

number of sub-branches allowed per branch.

<LI><TT><FONT FACE="Courier">$fanAngle</FONT></TT> is the angle

where each new sub-branch is created.

<LI><TT><FONT FACE="Courier">$fanRatio</FONT></TT> is the ratio

of the length of the new branch to the length of its parent branch.

</UL>

<P>

Each call to the recursive function, <TT><FONT FACE="Courier">tree()</FONT></TT>,

uses the following variables:

<UL>

<LI><TT><FONT FACE="Courier">$h</FONT></TT> is the current depth

in recursion.

<LI><TT><FONT FACE="Courier">$x</FONT></TT> is the current <TT><FONT FACE="Courier">x</FONT></TT>

to draw from.

<LI><TT><FONT FACE="Courier">$y</FONT></TT> is the current <TT><FONT FACE="Courier">y</FONT></TT>

to draw from.

<LI><TT><FONT FACE="Courier">$theta</FONT></TT> is the angle to

start drawing from.

<LI><TT><FONT FACE="Courier">$flen</FONT></TT>is the length of

the current branch.

</UL>

<P>

If the depth is <TT><FONT FACE="Courier">1</FONT></TT> or less,

the <TT><FONT FACE="Courier">tree()</FONT></TT> function draws

a rectangle at the current location and backs out of the recursion.

See lines 43 and 44. It also backs out without drawing a leaf

if the new branch is too small to draw.

<P>

Different colors are used based on the depth of the recursion

in lines 49 through 53.

<P>

Line 57 is where the depth of the recursion is marked off. The

value of <TT><FONT FACE="Courier">$h</FONT></TT> is decremented

before a recursive call to <TT><FONT FACE="Courier">tree()</FONT></TT>

is made. Lines 60 to 66 make sure that the <I>fanout</I> (maximum

number of degrees between the outer most branches) and number

of sub-branches is set in the correct <TT><FONT FACE="Courier">-PI/2</FONT></TT>

to <TT><FONT FACE="Courier">+PI/2</FONT></TT> range. You may want

to customize these lines to see how you want your tree to &quot;grow.&quot;

<P>

At line 71 we use a <TT><FONT FACE="Courier">for</FONT></TT> loop

to make a recursive call to the <TT><FONT FACE="Courier">tree()</FONT></TT>

function with different fanout angles, locations to draw, and

colors.

<P>

The initialization of global variables is done from lines 83 to

98. The random number generator is seeded at line 98.

<P>

The image is created at line 102, with colors for its components

created in lines 103 to 115. The <TT><FONT FACE="Courier">tree()</FONT></TT>

function is called at line 117. Add more calls to <TT><FONT FACE="Courier">tree()</FONT></TT>

if you want more than one tree printed. Some text is drawn in

the image area to customize the output in the lines up to 131.

<P>

Lines 133 to 136 actually print the image to standard output as

the response to an HTTP request.

<P>

To use the previous listing in an HTML file, you can add this

tag to an HTML page:

<BLOCKQUOTE>

<TT>&lt;IMG SRC=&quot;http://www.ikra.com/cgi-bin/tree.pl&quot;&gt;</TT>

</BLOCKQUOTE>

<P>

The browser showing the HTML page will call the <TT><FONT FACE="Courier">tree.pl</FONT></TT>

script, which in turn will return the GIF image back. Therefore,

every time you reload the Web page, you'll see a new image. Neat.

Actually, you can also refer to the <TT><FONT FACE="Courier">tree.pl</FONT></TT>

in the Location prompt on your browser. Refer to Figure 32.5 to

see how it's referred to in a URL. The script is called <TT><FONT FACE="Courier">fractal.pl</FONT></TT>

in Figure 32.5.

<P>

<A HREF="f32-5.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/f32-5.gif" ><B>Figure 32.5 : </B><I>Dynamic image creating with Web pages.</I></A>

<P>

You can see how easy it is to create GIF images on-the-fly and

use them in Web pages. Using the <TT><FONT FACE="Courier">GD.pm</FONT></TT>

module, it's easy to have Perl write your output as a GIF file

for use in other applications.

<H2><A NAME="Summary"><B><FONT SIZE=5 COLOR=#FF0000>Summary</FONT></B></A>

</H2>

<P>

In this chapter I covered two ways of creating images: using <TT><FONT FACE="Courier">gnuplot</FONT></TT>

and directly through the use of the <TT><FONT FACE="Courier">GD.pm</FONT></TT>

module. The <TT><FONT FACE="Courier">gnuplot</FONT></TT> method

is simpler to use when you have a set of data points in a logical

order that can be read directly from within <TT><FONT FACE="Courier">gnuplot</FONT></TT>.

An example for using <TT><FONT FACE="Courier">gnuplot</FONT></TT>

is when drawing charts and 3D plots. If you are drawing more than

just charts and plots, consider generating the images directly

from within Perl by using the <TT><FONT FACE="Courier">GD.pm</FONT></TT>

module. Using <TT><FONT FACE="Courier">GD.pm</FONT></TT> is a

bit more difficult because you have to manage the details of drawing

yourself, but it does eliminate the need for an external program

like <TT><FONT FACE="Courier">gnuplot</FONT></TT>.

<P>

<HR WIDTH="100%"></P>



<CENTER><P><A HREF="ch31.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch31.htm"><IMG SRC="pc.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/pc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="#CONTENTS"><IMG SRC="cc.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="index.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/index.htm"><IMG SRC="hb.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="ch33.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch33.htm"><IMG 

SRC="nc.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/nc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A></P></CENTER>



<P>

<HR WIDTH="100%"></P>



</BODY>

</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -