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

相关推荐
程序猿乐锅1 小时前
从0-1一文详解RabbitMQ
java·分布式·后端·中间件·rabbitmq·ruby
考虑考虑4 小时前
JDK26中的List.ofLazy()
java·后端·java ee
小蒜学长4 小时前
基于SpringBoot的公寓报修管理系统的设计与实现(代码+数据库+LW)
java·spring boot·后端·公寓报修管理系统·多角色协同
for_ever_love__4 小时前
MySQL 主从复制讲透:binlog 原理、搭建步骤与读写分离
java·数据库·mysql·binlog·读写分离·原理·主从复制
小π军7 小时前
操作系统面试题
java·linux·服务器
倔强的石头1067 小时前
应用账号最小权限实践:读写账号、报表账号、运维账号分层
java·运维·开发语言
NeilYuen7 小时前
【C++】STL源码仿写(二):vector
开发语言·c++
Shaoshing7 小时前
ThreadLocal
java·jvm·threadlocal
写后端的胖头鱼8 小时前
一文详解CAS
java·jvm·spring·cas·乐观锁·自旋