Processing练习之自动绘制

案例代码如下:

import processing.pdf.*;

import java.util.Calendar;

boolean savePDF = false;

int maxCount = 5000;

int currentCount = 1;

float\[\] x=new floatmaxCount;

float\[\] y=new floatmaxCount;

float\[\] r=new floatmaxCount;

void setup(){

size(600,600);

smooth();

frameRate(10);

x0 = width/2;

y0 = height/2;

r0 = 10;

}

void draw(){

if(savePDF)beginRecord(PDF,timestamp()+".pdf");

background(255);

strokeWeight(0.5);

float newR = random(1,7);

float newX = random(0+newR,width-newR);

float newY = random(0+newR,height-newR);

float closestDist = 100000000;

int closestIndex =0;

for(int i=0;i<currentCount;i++){

float newDist = dist(newX,newY,xi,yi);

if(newDist <closestDist){

closestDist =newDist;

closestIndex=i;

}

}

float angle =atan2(newY-yclosestIndex,newX-xclosestIndex);

xcurrentCount =xclosestIndex+cos(angle)*(rclosestIndex+newR);

ycurrentCount =yclosestIndex+sin(angle)*(rclosestIndex+newR);

rcurrentCount = newR;

currentCount++;

for(int i=0;i<currentCount;i++){

fill(50);

ellipse(xi,yi,ri*2,ri*2);

}

if(currentCount++>=maxCount) noLoop();

if(savePDF){

savePDF =false;

endShape();

}

}

void keyReleased(){

if(key=='s'||key=='S') saveFrame(timestamp()+"_##.png");

if(key=='p'||key=='P') savePDF=true;

}

String timestamp(){

Calendar now=Calendar.getInstance();

return String.format("%1ty%1tm%1td_%1tH%1tM%1tS",now);

}

保存并运行如图1

图1

相关推荐
个 人 练 习 生2 小时前
C++:从零基础到深入了解vector的使用
开发语言·c++·经验分享·学习·程序人生·stl
ShineWinsu2 小时前
对于Qt窗口:QMainWindow、QMenuBar、Qmenu、QAction的解析
c++·qt·菜单·qmainwindow·qaction·qmenu·qmenubar
兴趣互联3 小时前
jeecgboot nacos2.3.2 使用pgsql
java
H.莓飛3 小时前
【C++】【STL】stack_OJ题
服务器·c语言·开发语言·数据结构·c++·算法·visual studio
hudd6683 小时前
JavaEE 初阶 - 文件操作和IO
java·开发语言·java-ee
Rabitebla3 小时前
【Linux操作系统】 文件篇(一):文件描述符与 Linux 文件管理机制
linux·网络·c++·笔记·学习
Flynn Liang4 小时前
HikariCP 连接池不是越大越好:I/O 密集型 Java 服务的数据库容量工程
java·数据库·mysql·高并发·连接池·hikaricp·sql 优化
学长毕设4 小时前
基于SpringBoot社区快递驿站物流配送系统-附源码
java·spring boot·后端
Flynt4 小时前
Java4Shell 实测:EPYC 一跑就中招,关掉 Base64 硬件加速,1KB 解码慢 14 倍
java·安全·性能优化
Resean02235 小时前
Spring 源码分析(七)AOP 原理(上):代理的创建
java·后端·spring