yangfanconan 发表于 2013-7-8 18:00:25

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

下面我们开始学习Processing自带库-----PDF。
这个库实际上是用来导出生成PDF的,导出的可以是文件,也可以是图片甚至可以是连续动画的每一帧。
下面我们看一个官方自动的例程。
import processing.pdf.*;

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

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

// Exit the program
println("Finished.");
exit();
}


这个就是成生产的PDF

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


import processing.opengl.*;
import processing.pdf.*;

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

// System data
boolean dosave=false;
int num;
float pt[];
int style[];


void setup() {
size(600, 600, OPENGL);
frameRate(24);
background(255);

// Fill the tables
sinLUT=new float;
cosLUT=new float;
for (int i = 0; i < SINCOS_LENGTH; i++) {
    sinLUT= (float)Math.sin(i*DEG_TO_RAD*SINCOS_PRECISION);
    cosLUT= (float)Math.cos(i*DEG_TO_RAD*SINCOS_PRECISION);
}

num = 150;
pt = new float; // rotx, roty, deg, rad, w, speed
style = new int; // color, render style

// Set up arc shapes
int index=0;
float prob;
for (int i=0; i<num; i++) {
    pt = random(PI*2); // Random X axis rotation
    pt = random(PI*2); // Random Y axis rotation

    pt = random(60,80); // Short to quarter-circle arcs
    if(random(100)>90) pt=(int)random(8,27)*10;

    pt = int(random(2,50)*5); // Radius. Space them out nicely

    pt = random(4,32); // Width of band
    if(random(100)>90) pt=random(40,60); // Width of band

    pt = radians(random(5,30))/5; // Speed of rotation

    // get colors
    prob = random(100);
    if(prob<30) style=colorBlended(random(1), 255,0,100, 255,0,0, 210);
    else if(prob<70) style=colorBlended(random(1), 0,153,255, 170,225,255, 210);
    else if(prob<90) style=colorBlended(random(1), 200,255,0, 150,255,0, 210);
    else style=color(255,255,255, 220);

    if(prob<50) style=colorBlended(random(1), 200,255,0, 50,120,0, 210);
    else if(prob<90) style=colorBlended(random(1), 255,100,0, 255,255,0, 210);
    else style=color(255,255,255, 220);

    style=(int)(random(100))%3;
}
}

void draw() {

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

    // set default Illustrator stroke styles and paint background rect.
    pdf.strokeJoin(MITER);
    pdf.strokeCap(SQUARE);
    pdf.fill(0);
    pdf.noStroke();
    pdf.rect(0,0, width,height);
}

background(0);

int index=0;
translate(width/2,height/2,0);
rotateX(PI/6);
rotateY(PI/6);

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

    rotateX(pt);
    rotateY(pt);

    if(style==0) {
      stroke(style);
      noFill();
      strokeWeight(1);
      arcLine(0,0, pt,pt,pt);
    }
    else if(style==1) {
      fill(style);
      noStroke();
      arcLineBars(0,0, pt,pt,pt);
    }
    else {
      fill(style);
      noStroke();
      arc(0,0, pt,pt,pt);
    }

    // increase rotation
    pt+=pt/10;
    pt+=pt/20;

    popMatrix();
}

if(dosave) {
    endRaw();
    dosave=false;
}
}


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

r2 = (r2 - r);
g2 = (g2 - g);
b2 = (b2 - b);
return color(r + r2 * fract, g + g2 * fract, b + b2 * fract, a);
}


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

for (int j=0; j<numlines; j++) {
    beginShape();
    for (int i=0; i<a; i++) {
      vertex(cosLUT*rad+x,sinLUT*rad+y);
    }
    endShape();
    rad += 2;
}
}

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

beginShape(QUADS);
for (int i=0; i<a; i+=4) {
    vertex(cosLUT*(rad)+x,sinLUT*(rad)+y);
    vertex(cosLUT*(rad+w)+x,sinLUT*(rad+w)+y);
    vertex(cosLUT*(rad+w)+x,sinLUT*(rad+w)+y);
    vertex(cosLUT*(rad)+x,sinLUT*(rad)+y);
}
endShape();
}

// Draw solid arc
public void arc(float x,float y,float deg,float rad,float w) {
int a = int(min (deg/SINCOS_PRECISION,SINCOS_LENGTH-1));
beginShape(QUAD_STRIP);
for (int i = 0; i < a; i++) {
    vertex(cosLUT*(rad)+x,sinLUT*(rad)+y);
    vertex(cosLUT*(rad+w)+x,sinLUT*(rad+w)+y);
}
endShape();
}

void keyPressed() {
if (key == 's') {
    dosave=true;
}
}

void mouseReleased() {
background(255);
}
上面是官方自带的PDF程序中比较COOL的一个

nust_奔跑 发表于 2013-7-9 08:45:23

很~~cool~~~

zhengqiang 发表于 2013-7-9 11:08:29

厉害 processing!

laozjx 发表于 2016-6-22 17:07:17

谢谢楼主,学习。
页: [1]
查看完整版本: Processing之旅-----【给鱼插上翅膀,Processing基础扩展库4】