极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 22879|回复: 3

Processing之旅-----【给鱼插上翅膀,Processing基础扩展库4】

[复制链接]
发表于 2013-7-8 18:00:25 | 显示全部楼层 |阅读模式
下面我们开始学习Processing自带库-----PDF。
这个库实际上是用来导出生成PDF的,导出的可以是文件,也可以是图片甚至可以是连续动画的每一帧。
下面我们看一个官方自动的例程。
  1. import processing.pdf.*;

  2. void setup() {
  3.   size(400, 400, PDF, "filename.pdf");
  4. }

  5. void draw() {
  6.   // Draw something good here
  7.   line(0, 0, width/2, height);

  8.   // Exit the program
  9.   println("Finished.");
  10.   exit();
  11. }
复制代码



这个就是成生产的PDF

  1. /**
  2. * PDF Complex
  3. * by Marius Watz (workshop.evolutionzone.com).
  4. *
  5. * Example using PDF to output complex 3D geometry for print.
  6. * Press "s" to save a PDF.
  7. */


  8. import processing.opengl.*;
  9. import processing.pdf.*;

  10. // Trig lookup tables borrowed from Toxi. Cryptic but effective
  11. float sinLUT[];
  12. float cosLUT[];
  13. float SINCOS_PRECISION=1.0;
  14. int SINCOS_LENGTH= int((360.0/SINCOS_PRECISION));

  15. // System data
  16. boolean dosave=false;
  17. int num;
  18. float pt[];
  19. int style[];


  20. void setup() {
  21.   size(600, 600, OPENGL);
  22.   frameRate(24);
  23.   background(255);
  24.   
  25.   // Fill the tables
  26.   sinLUT=new float[SINCOS_LENGTH];
  27.   cosLUT=new float[SINCOS_LENGTH];
  28.   for (int i = 0; i < SINCOS_LENGTH; i++) {
  29.     sinLUT[i]= (float)Math.sin(i*DEG_TO_RAD*SINCOS_PRECISION);
  30.     cosLUT[i]= (float)Math.cos(i*DEG_TO_RAD*SINCOS_PRECISION);
  31.   }

  32.   num = 150;
  33.   pt = new float[6*num]; // rotx, roty, deg, rad, w, speed
  34.   style = new int[2*num]; // color, render style

  35.   // Set up arc shapes
  36.   int index=0;
  37.   float prob;
  38.   for (int i=0; i<num; i++) {
  39.     pt[index++] = random(PI*2); // Random X axis rotation
  40.     pt[index++] = random(PI*2); // Random Y axis rotation

  41.     pt[index++] = random(60,80); // Short to quarter-circle arcs
  42.     if(random(100)>90) pt[index]=(int)random(8,27)*10;

  43.     pt[index++] = int(random(2,50)*5); // Radius. Space them out nicely

  44.     pt[index++] = random(4,32); // Width of band
  45.     if(random(100)>90) pt[index]=random(40,60); // Width of band

  46.     pt[index++] = radians(random(5,30))/5; // Speed of rotation

  47.     // get colors
  48.     prob = random(100);
  49.     if(prob<30) style[i*2]=colorBlended(random(1), 255,0,100, 255,0,0, 210);
  50.     else if(prob<70) style[i*2]=colorBlended(random(1), 0,153,255, 170,225,255, 210);
  51.     else if(prob<90) style[i*2]=colorBlended(random(1), 200,255,0, 150,255,0, 210);
  52.     else style[i*2]=color(255,255,255, 220);

  53.     if(prob<50) style[i*2]=colorBlended(random(1), 200,255,0, 50,120,0, 210);
  54.     else if(prob<90) style[i*2]=colorBlended(random(1), 255,100,0, 255,255,0, 210);
  55.     else style[i*2]=color(255,255,255, 220);

  56.     style[i*2+1]=(int)(random(100))%3;
  57.   }
  58. }

  59. void draw() {

  60.   if(dosave) {
  61.     // set up PGraphicsPDF for use with beginRaw()
  62.     PGraphicsPDF pdf = (PGraphicsPDF)beginRaw(PDF, "pdf_complex_out.pdf");

  63.     // set default Illustrator stroke styles and paint background rect.
  64.     pdf.strokeJoin(MITER);
  65.     pdf.strokeCap(SQUARE);
  66.     pdf.fill(0);
  67.     pdf.noStroke();
  68.     pdf.rect(0,0, width,height);
  69.   }

  70.   background(0);

  71.   int index=0;
  72.   translate(width/2,height/2,0);
  73.   rotateX(PI/6);
  74.   rotateY(PI/6);

  75.   for (int i=0; i<num; i++) {
  76.     pushMatrix();

  77.     rotateX(pt[index++]);
  78.     rotateY(pt[index++]);

  79.     if(style[i*2+1]==0) {
  80.       stroke(style[i*2]);
  81.       noFill();
  82.       strokeWeight(1);
  83.       arcLine(0,0, pt[index++],pt[index++],pt[index++]);
  84.     }
  85.     else if(style[i*2+1]==1) {
  86.       fill(style[i*2]);
  87.       noStroke();
  88.       arcLineBars(0,0, pt[index++],pt[index++],pt[index++]);
  89.     }
  90.     else {
  91.       fill(style[i*2]);
  92.       noStroke();
  93.       arc(0,0, pt[index++],pt[index++],pt[index++]);
  94.     }

  95.     // increase rotation
  96.     pt[index-5]+=pt[index]/10;
  97.     pt[index-4]+=pt[index++]/20;

  98.     popMatrix();
  99.   }

  100.   if(dosave) {
  101.     endRaw();
  102.     dosave=false;
  103.   }
  104. }


  105. // Get blend of two colors
  106. public int colorBlended(float fract,
  107. float r, float g, float b,
  108. float r2, float g2, float b2, float a) {

  109.   r2 = (r2 - r);
  110.   g2 = (g2 - g);
  111.   b2 = (b2 - b);
  112.   return color(r + r2 * fract, g + g2 * fract, b + b2 * fract, a);
  113. }


  114. // Draw arc line
  115. public void arcLine(float x,float y,float deg,float rad,float w) {
  116.   int a=(int)(min (deg/SINCOS_PRECISION,SINCOS_LENGTH-1));
  117.   int numlines=(int)(w/2);

  118.   for (int j=0; j<numlines; j++) {
  119.     beginShape();
  120.     for (int i=0; i<a; i++) {
  121.       vertex(cosLUT[i]*rad+x,sinLUT[i]*rad+y);
  122.     }
  123.     endShape();
  124.     rad += 2;
  125.   }
  126. }

  127. // Draw arc line with bars
  128. public void arcLineBars(float x,float y,float deg,float rad,float w) {
  129.   int a = int((min (deg/SINCOS_PRECISION,SINCOS_LENGTH-1)));
  130.   a /= 4;

  131.   beginShape(QUADS);
  132.   for (int i=0; i<a; i+=4) {
  133.     vertex(cosLUT[i]*(rad)+x,sinLUT[i]*(rad)+y);
  134.     vertex(cosLUT[i]*(rad+w)+x,sinLUT[i]*(rad+w)+y);
  135.     vertex(cosLUT[i+2]*(rad+w)+x,sinLUT[i+2]*(rad+w)+y);
  136.     vertex(cosLUT[i+2]*(rad)+x,sinLUT[i+2]*(rad)+y);
  137.   }
  138.   endShape();
  139. }

  140. // Draw solid arc
  141. public void arc(float x,float y,float deg,float rad,float w) {
  142.   int a = int(min (deg/SINCOS_PRECISION,SINCOS_LENGTH-1));
  143.   beginShape(QUAD_STRIP);
  144.   for (int i = 0; i < a; i++) {
  145.     vertex(cosLUT[i]*(rad)+x,sinLUT[i]*(rad)+y);
  146.     vertex(cosLUT[i]*(rad+w)+x,sinLUT[i]*(rad+w)+y);
  147.   }
  148.   endShape();
  149. }

  150. void keyPressed() {
  151.   if (key == 's') {
  152.     dosave=true;
  153.   }
  154. }

  155. void mouseReleased() {
  156.   background(255);
  157. }
复制代码

上面是官方自带的PDF程序中比较COOL的一个

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2013-7-9 08:45:23 | 显示全部楼层
很~~cool~~~
回复 支持 反对

使用道具 举报

发表于 2013-7-9 11:08:29 | 显示全部楼层
厉害 processing!
回复 支持 反对

使用道具 举报

发表于 2016-6-22 17:07:17 | 显示全部楼层
谢谢楼主,学习。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-28 23:29 , Processed in 0.045443 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表