home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / polyline-example.pl < prev    next >
Encoding:
Text File  |  2002-07-19  |  9.0 KB  |  367 lines

  1. ############################################################################
  2. #
  3. # Sample code for use of Polyline.pm
  4. #
  5. # Author:    Dan Harasty
  6. # Email:    harasty@cpan.org
  7. # Version:    0.1
  8. # Date:        7/20/2002
  9. #
  10. #
  11.  
  12. use GD;
  13. use GD::Polyline;
  14.  
  15. $PI = 3.14159; $TWO_PI = 2 * $PI;
  16. sub r2d {$_[0] * 180 / $PI};
  17.  
  18. $splinekey = "<UL><LI>Green: original polygon or polyline<LI>Blue: control points added with addControlPoints()<LI>Black: spline generated by toSpline()</UL>";
  19.  
  20. if (1) {
  21.  
  22.     use GD;
  23.     use GD::Polyline;
  24.  
  25.     # create an image
  26.     $image = new GD::Image (500,300);
  27.     $white  = $image->colorAllocate(255,255,255);
  28.     $black  = $image->colorAllocate(  0,  0,  0);
  29.     $red    = $image->colorAllocate(255,  0,  0);
  30.  
  31.     # create a new polyline
  32.     $polyline = new GD::Polyline;
  33.  
  34.     # add some points
  35.     $polyline->addPt(  0,  0);
  36.     $polyline->addPt(  0,100);
  37.     $polyline->addPt( 50,125);
  38.     $polyline->addPt(100,  0);
  39.  
  40.     # polylines can use polygon methods (and vice versa)
  41.     $polyline->offset(200,100);
  42.  
  43.     # rotate 60 degrees, about the centroid
  44.     $polyline->rotate(3.14159/3, $polyline->centroid());
  45.  
  46.     # scale about the centroid
  47.     $polyline->scale(1.5, 2, $polyline->centroid());
  48.  
  49.     # draw the polyline
  50.     $image->polydraw($polyline,$black);
  51.  
  52.     # create a spline, which is also a polyine
  53.     $spline = $polyline->addControlPoints->toSpline;
  54.     $image->polydraw($spline,$red);
  55.  
  56.     # output the png
  57.     #binmode STDOUT;
  58.     #print $image->png;
  59.  
  60.     SampleImage($image, "polyline-synopsis.png", "Synopsis", "Polyline created by 'SYNOPSIS' section of documentation.");
  61.  
  62. }
  63.  
  64. if (1) {
  65.     $image = NewImage();
  66.  
  67.     $offset = 50;
  68.  
  69.  
  70.     for $poly (new GD::Polygon, new GD::Polyline) {
  71.  
  72.         $table_info = [];
  73.  
  74.         $poly->addPt(  0,  0);
  75.         $poly->addPt(  0,100);
  76.         $poly->addPt( 50,125);
  77.         $poly->addPt(100,  0);
  78.  
  79.         #print "this " . ref($poly) . " has " . $poly->length() . " points\n";
  80.  
  81.         push @$table_info, ["<B>".ref($poly)."</B>"];
  82.         push @$table_info, ['vertex number: ', 0..($poly->length()-1)];
  83.  
  84.         @coords = $poly->vertices();
  85.         @coords = map {"[".int($_->[0]).",".int($_->[1])."]"} @coords;
  86.         push @$table_info, ['coordinates (pre-offset): ', @coords];
  87.  
  88.         @lengths = $poly->segLength();
  89.         @lengths = map {int($_+0.5)} @lengths;
  90.         #print "segLengths are   : @lengths\n";
  91.         #print "perimeter is     : " . int($poly->segLength()) . "\n";
  92.         push @$table_info, ['segment lengths: ', @lengths];
  93.  
  94.         @angles = $poly->segAngle();
  95.         @angles = map {int(r2d($_)+0.5)} @angles;
  96.         #print "seg angles are   : @angles\n";
  97.         push @$table_info, ['segment angles: ', @angles];
  98.  
  99.         @angles = $poly->vertexAngle();
  100.         @angles = map {defined ($_) ? int(r2d($_)+0.5) : "undef"} @angles;
  101.         #print "vertex angles are: @angles\n";
  102.         push @$table_info, ['vertex angles: ', @angles];
  103.  
  104.         $poly->offset(50 + $offset,80);
  105.         $offset += 200;
  106.  
  107.         # draw the original poly
  108.         $image->polydraw($poly,$black);
  109.  
  110.         #print "\n\n";
  111.  
  112.         push @$summary_table, genHTMLTable($table_info, 0);
  113.     }
  114.  
  115.     SampleImage($image, "polyline-simple.png", "Simple", "GD::Polygon and GD::Polyline with same vertexes.</P>" . genHTMLTable([$summary_table], 1));
  116.  
  117. }
  118.  
  119. if (1) {
  120.     $image = NewImage();
  121.  
  122.     $offset = 50;
  123.  
  124.     for $poly (new GD::Polygon, new GD::Polyline) {
  125.  
  126.         $poly->addPt(  0,  0);
  127.         $poly->addPt(  0,100);
  128.         $poly->addPt( 50,125);
  129.         $poly->addPt(100,  0);
  130.  
  131.         $poly->offset(50 + $offset,80);
  132.         $offset += 200;
  133.  
  134.         # draw the original poly
  135.         $image->polydraw($poly,$green);
  136.  
  137.         # create and draw the control line for the spline
  138.         $ctrlline = $poly->addControlPoints();
  139.         $image->polydraw($ctrlline,$cyan);
  140.  
  141.         # create and draw the spline itself
  142.         $spline = $ctrlline->toSpline();
  143.         $image->polydraw($spline,$black);
  144.  
  145.     }
  146.  
  147.     SampleImage($image, "polyline-spline.png", "Spline", "Splines fit to vertices of polygon and polyline.  $splinekey");
  148.  
  149. }
  150.  
  151.  
  152. if (1) {
  153.     $image = NewImage();
  154.  
  155.     $triangle = new GD::Polygon;
  156.  
  157.     $triangle->addPt(  0,  0);
  158.     $triangle->addPt(-19, 95);
  159.     $triangle->addPt( 19, 95);
  160.  
  161.     $triangle->offset(250,50);
  162.  
  163.     foreach (1..9) {
  164.         $image->polydraw($triangle,gdBrushed);
  165.         $triangle->rotate($TWO_PI / 9, 250, 150);
  166.     }
  167.  
  168.     SampleImage($image, "polyline-star9.png", "Nine Pointed Star", "A triangle, rotated about a point other than the origin.<BR>Demonstration of \$poly->rotate() and \$poly->offset()");
  169.  
  170. }
  171.  
  172. if (1) {
  173.     $image = NewImage();
  174.  
  175.     $cloverControl = new GD::Polyline;
  176.     $cloverControl->addPt(45,45);
  177.     $cloverControl->addPt(10,10);
  178.     $cloverControl->addPt(90,10);
  179.     $cloverControl->addPt(55,45);
  180.     $cloverControl->addPt(90,10);
  181.     $cloverControl->addPt(90,90);
  182.     $cloverControl->addPt(55,55);
  183.     $cloverControl->addPt(90,90);
  184.     $cloverControl->addPt(10,90);
  185.     $cloverControl->addPt(45,55);
  186.     $cloverControl->addPt(10,90);
  187.     $cloverControl->addPt(10,10);
  188.     $cloverControl->addPt(45,45);
  189.  
  190.     $clover = $cloverControl->toSpline();
  191.  
  192.     # note that the three following transformations
  193.     # could have been called on $cloverControl, instead,
  194.     # followed by the above call
  195.  
  196.     $clover->offset($clover->centroid(-1));
  197.     $clover->scale(3, 3);
  198.     $clover->offset(250, 150);
  199.  
  200.     $image->filledPolygon($clover,$green);
  201.  
  202.     SampleImage($image, "polyline-clover.png", "Clover", "Sample image generated by GD::Polygon");
  203.  
  204. }
  205.  
  206. if (1) {
  207.     $image = NewImage();
  208.  
  209.     $polyline = new GD::Polyline;
  210.  
  211.     for (0..15) {
  212.         $polyline->addPt(30 * $_ + 10, rand(90) + 5);
  213.     }
  214.  
  215.     $image->polyline($polyline,$green);
  216.  
  217.     $ctrlline = $polyline->addControlPoints();
  218.     $ctrlline->offset(0,100);
  219.     $image->polyline($ctrlline,$cyan);
  220.  
  221.     $spline = $ctrlline->toSpline();
  222.     $spline->offset(0,100);
  223.     $image->polyline($spline,$black);
  224.  
  225.     SampleImage($image, "polyline-zigzag.png", "Zigzag", "Spline fit to random function.  $splinekey");
  226.  
  227. }
  228.  
  229. if (1) {
  230.     $image = NewImage();
  231.  
  232.     $ring_network = new GD::Polygon;
  233.  
  234.     $num_nodes = 10;
  235.     $randfactor = 80;
  236.  
  237.     for (1..$num_nodes) {
  238.         $x = 250 + 150 * cos($TWO_PI * $_/$num_nodes);
  239.         $y = 150 + 100 * sin($TWO_PI * $_/$num_nodes);
  240.         $x += rand($randfactor)-$randfactor/2;
  241.         $y += rand($randfactor)-$randfactor/2;
  242.         $ring_network->addPt($x, $y);
  243.     }
  244.  
  245.     $image->setBrush($brush2);
  246.     $image->polyline($ring_network->addControlPoints->toSpline,gdBrushed);
  247.  
  248.     $ring_node = new GD::Polygon;
  249.  
  250.     $ring_node->addPt( 0, 0);
  251.     $ring_node->addPt(10, 0);
  252.     $ring_node->addPt(10,10);
  253.     $ring_node->addPt( 0,10);
  254.  
  255.     for $ring_vertex ($ring_network->vertices()) {
  256.         $ring_node->offset($ring_node->centroid(-1));
  257.         $ring_node->offset(@$ring_vertex);
  258.         $image->filledPolygon($ring_node,$grey);
  259.     }
  260.  
  261.     SampleImage($image, "polyline-ring-network.png", "Ring Network", "Closed spline fit to nodes at somewhat random positions.");
  262.  
  263. }
  264.  
  265. WriteToFile("polyline-example.html", theHTML());
  266.  
  267. print "\n";
  268. print "open 'polyline-example.html' in your favorite browser that supports PNG.\n";
  269. print "\n";
  270.  
  271. print "done! " . localtime() . "\n";
  272.  
  273. ##########################
  274. #
  275. # helper functions
  276. #
  277.  
  278. sub NewImage {
  279.     $image = new GD::Image (500,300);
  280.  
  281.     $white  = $image->colorAllocate(255,255,255);
  282.     $black  = $image->colorAllocate(  0,  0,  0);
  283.     $grey   = $image->colorAllocate(128,128,128);
  284.     $red    = $image->colorAllocate(255,  0,  0);
  285.     $orange = $image->colorAllocate(255,196,  0);
  286.     $green  = $image->colorAllocate(  0,255,  0);
  287.     $blue   = $image->colorAllocate(  0,  0,255);
  288.     $cyan   = $image->colorAllocate(  0,255,255);
  289.     $purple = $image->colorAllocate(206,  0,165);
  290.  
  291.     $brush_width = 2;
  292.     $brush_color = [255,128,0];
  293.         $brush = new GD::Image($brush_width,$brush_width);
  294.         $brush->transparent($brush->colorAllocate(255,255,255));
  295.         $brush->filledRectangle(0,0,$brush_width,$brush_width,$brush->colorAllocate(@$brush_color));
  296.     $brush1 = $brush;
  297.  
  298.     $brush_width = 3;
  299.     $brush_color = [206,0,165];
  300.         $brush = new GD::Image($brush_width,$brush_width);
  301.         $brush->transparent($brush->colorAllocate(255,255,255));
  302.         $brush->filledRectangle(0,0,$brush_width,$brush_width,$brush->colorAllocate(@$brush_color));
  303.     $brush2 = $brush;
  304.  
  305.     $image->setBrush($brush1);
  306.  
  307.     $image;
  308. }
  309.  
  310. my $html;
  311.  
  312. sub SampleImage {
  313.     my $image = shift;
  314.     my $file  = shift;
  315.     my $title = shift;
  316.     my $text  = shift;
  317.  
  318.     WriteToBinaryFile($file, $image->png());
  319.  
  320.     $html .= "<IMG SRC='$file'><BR>\n";
  321.     $html .= "<B>$title</B> - $file<BR>\n";
  322.     $html .= "<P>$text</P><HR>\n";
  323.  
  324. }
  325.  
  326. sub theHTML {
  327.     $html;
  328. }
  329.  
  330. sub WriteToFile {
  331.     my $file = shift || return 0;
  332.     my $contents = shift || "";
  333.  
  334.     open (NEWFILE, ">" . $file) or die "couldn't write to file $file";
  335.     print NEWFILE $contents;
  336.     close(NEWFILE);
  337.  
  338.     print "created file $file\n";
  339. }
  340.  
  341. sub WriteToBinaryFile {
  342.     my $file = shift || return 0;
  343.     my $contents = shift || "";
  344.  
  345.     open (NEWFILE, ">" . $file) or die "couldn't write to file $file";
  346.     binmode NEWFILE;
  347.     print NEWFILE $contents;
  348.     close(NEWFILE);
  349.  
  350.     print "created file $file\n";
  351. }
  352.  
  353. sub genHTMLTable {
  354.     my $array_of_arrays = shift;
  355.     my $border = shift;
  356.     my $html_table;
  357.  
  358.     $html_table .= "<TABLE BORDER='$border'>";
  359.     for my $array_of_items (@$array_of_arrays) {
  360.         $html_table .= "<TR><TD>";
  361.         $html_table .= join("</TD><TD>", @$array_of_items);
  362.         $html_table .= "</TD></TR>";
  363.     }
  364.     $html_table .= "</TABLE>";
  365.     $html_table;
  366. }
  367.