Processing 向线函数添加缩放机制的最佳方法?
我制作了一个脚本,可以在更大的矩形中创建矩形,我称之为长方体。现在我正在尝试实现一种机制,使它看起来像是在放大。我正在使用line()函数绘制方框。有没有人建议让每件事都能连续地扩展 代码(相当长):Processing 向线函数添加缩放机制的最佳方法?,processing,line,scale,Processing,Line,Scale,我制作了一个脚本,可以在更大的矩形中创建矩形,我称之为长方体。现在我正在尝试实现一种机制,使它看起来像是在放大。我正在使用line()函数绘制方框。有没有人建议让每件事都能连续地扩展 代码(相当长): import java.util.ArrayList; //*全局变量 //点的随机点 int随机点; int随机点; //侧窗偏移 公共最终整数窗口_偏移=10; //HSB中的框和点颜色 公共静态int[]boxColor={0,255,255}; 公共int[]pointcolor={0,0
import java.util.ArrayList;
//*全局变量
//点的随机点
int随机点;
int随机点;
//侧窗偏移
公共最终整数窗口_偏移=10;
//HSB中的框和点颜色
公共静态int[]boxColor={0,255,255};
公共int[]pointcolor={0,0,255};
//存储盒和点
公共箱第一箱;
公共数组列表boxArray;
公共阵列列表pointPos;
//*功能
//颜色变换器,从红色变为红色
公共空间颜色(int[]颜色){
如果(颜色[0]>=255){
颜色[0]=0;
}
否则{
颜色[0]++;
}
}
//箱类
公共类箱{
公共布尔值isRealBox=true;
公共整数x,y,w,h;
公共int cH、cS、cB;
私人信箱;
公用方框(整数x、整数y、整数w、整数h、整数[]颜色){
这个.x=x;
这个。y=y;
这个.w=w;
这个,h=h;
this.cH=颜色[0];
this.cS=颜色[1];
this.cB=颜色[2];
}
公共框(整数x,整数y,整数w,整数h,整数[]颜色,框父框){
这个.x=x;
这个。y=y;
这个.w=w;
这个,h=h;
this.cH=颜色[0];
this.cS=颜色[1];
this.cB=颜色[2];
this.parentBox=parentBox;
}
公共布尔值isInsideBox(整数x,整数y){
如果((x>this.x)和&(xthis.y)和&(ythis.x))&((ythis.y))返回0;
否则如果((xthis.x+this.w/2))和((ythis.y))返回1;
否则如果((xthis.x))和((ythis.y+this.h/2))返回2;
否则如果((xthis.x+this.w/2))和((ythis.y+this.h/2))返回3;
否则返回-1;
}
//如果(x,y)在框内,则运行
公用箱ifInsideBox(int x,int y){
交换机(扇区(x,y)){
案例0:
变色(boxcolor);
退回新的盒子(这个.x,这个.y,这个.w/2,这个.h/2,BoxColor,这个);
案例1:
变色(boxcolor);
返回新框(此.x+此.w/2,此.y,此.w/2,此.h/2,BoxColor,此);
案例2:
变色(boxcolor);
返回新框(此.x,此.y+此.h/2,此.w/2,此.h/2,BoxColor,此);
案例3:
变色(boxcolor);
返回新框(此.x+此.w/2,此.y+此.h/2,此.w/2,此.h/2,BoxColor,此);
违约:
盒子温度=新盒子(这个.x,这个.y,这个.w,这个.h,盒子颜色);
temp.isRealBox=false;
返回温度;
}
}
//用于绘制长方体的函数。使用4行
公共储物箱(){
冲程(cH、cS、cB);
行(this.x,this.y,this.x+this.w,this.y);
行(this.x+this.w,this.y,this.x+this.w,this.y+this.h);
行(this.x+this.w,this.y+this.h,this.x,this.y+this.h);
行(this.x,this.y+this.h,this.x,this.y);
}
公共空抽屉(整数x,整数y){
冲程(cH、cS、cB);
开关(parentBox.getSector(x,y)){
案例0:
行(parentBox.x+parentBox.w/2,parentBox.y,parentBox.x+parentBox.w/2,parentBox.y+parentBox.h/2);
行(parentBox.x+parentBox.w/2,parentBox.y+parentBox.h/2,parentBox.x,parentBox.y+parentBox.h/2);
打破
案例1:
行(parentBox.x+parentBox.w/2,parentBox.y,parentBox.x+parentBox.w/2,parentBox.y+parentBox.h/2);
行(parentBox.x+parentBox.w/2,parentBox.y+parentBox.h/2,parentBox.x+parentBox.w,parentBox.y+parentBox.h/2);
打破
案例2:
行(parentBox.x,parentBox.y+parentBox.h/2,parentBox.x+parentBox.w/2,parentBox.y+parentBox.h/2);
行(parentBox.x+parentBox.w/2,parentBox.y+parentBox.h/2,parentBox.x+parentBox.w/2,parentBox.y+parentBox.h);
打破
案例3:
行(parentBox.x+parentBox.w/2,parentBox.y+parentBox.h/2,parentBox.x+parentBox.w,parentBox.y+parentBox.h/2);
行(parentBox.x+parentBox.w/2,parentBox.y+parentBox.h/2,parentBox.x+parentBox.w/2,parentBox.y+parentBox.h);
打破
}
}
}
无效设置(){
尺寸(810810);
彩色模式(HSB);
冲程重量(2);
帧率(60);
//初始化从侧面偏移的firstBox
firstBox=新框(窗偏移/2,窗偏移/2,宽度-窗偏移,高度-窗偏移,BoxColor);
boxArray=newarraylist();
添加(firstBox);
pointPos=新的ArrayList();
}
作废提款(){
背景(0,0,0);
for(int-loop=0;loop<3;loop++){
随机点=(int)随机(窗口偏移/2,宽度-窗口偏移/2);
随机点=(int)随机(窗口偏移/2,高度-窗口偏移/2);
布尔addedBox=false;
对于(int i=boxArray.size()-1;i>=0;i--){
如果(!addedBox){
if(boxArray.get(i).isInsideBox(randomXPoint,randomYPoint)){
add(boxArray.get(i).ifInsideBox(randomXPoint,randomYPoint));
addedBox=true;
}
}
}
//点(随机点,随机点);
点位置添加(新PVector(随机点,随机点));
//画每一个盒子
对于(int i=boxArray.size()-1;i>=1;i--){
if(boxArray.get(i).isRealBox==true)
get(i).drawBox();
}
get(0.drawBox();
斯特罗
import java.util.ArrayList;
// *Gloabal Variables
// Random Points for dots
int randomXPoint;
int randomYPoint;
// Window offset on sides
public final int WINDOW_OFFSET = 10;
// Box and dot colour in HSB
public static int[] boxColour = { 0, 255, 255 };
public int[] pointColour = { 0, 0, 255 };
// Storages for boxes and points
public Box firstBox;
public ArrayList<Box> boxArray;
public ArrayList<PVector> pointPos;
// *Functions
// Colour changer, going from red to red
public void ChangeColour(int[] colour) {
if (colour[0] >= 255) {
colour[0] = 0;
}
else {
colour[0]++;
}
}
// Box class
public class Box {
public boolean isRealBox = true;
public int x, y, w, h;
public int cH, cS, cB;
private Box parentBox;
public Box(int x, int y, int w, int h, int[] colour) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.cH = colour[0];
this.cS = colour[1];
this.cB = colour[2];
}
public Box(int x, int y, int w, int h, int[] colour, Box parentBox) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.cH = colour[0];
this.cS = colour[1];
this.cB = colour[2];
this.parentBox = parentBox;
}
public boolean isInsideBox(int x, int y) {
if (((x > this.x) && (x < this.x + this.w)) && (y > this.y) && (y < this.y + this.h))
return true;
else
return false;
}
// Gets sector in box, enabling to know which way to shorten
public int getSector(int x, int y) {
if (((x < this.x + this.w / 2) && (x > this.x)) && ((y < this.y + this.h / 2) && (y > this.y))) return 0;
else if (((x < this.x + this.w) && (x > this.x + this.w / 2)) && ((y < this.y + this.h / 2) && (y > this.y))) return 1;
else if (((x < this.x + this.w / 2) && (x > this.x)) && ((y < this.y + this.h) && (y > this.y + this.h / 2))) return 2;
else if (((x < this.x + this.w) && (x > this.x + this.w / 2)) && ((y < this.y + this.h) && (y > this.y + this.h / 2))) return 3;
else return -1;
}
// Runs if (x, y) is inside the box
public Box ifInsideBox(int x, int y) {
switch (getSector(x, y)) {
case 0:
ChangeColour(boxColour);
return new Box(this.x, this.y, this.w / 2, this.h / 2, boxColour, this);
case 1:
ChangeColour(boxColour);
return new Box(this.x + this.w / 2, this.y, this.w / 2, this.h / 2, boxColour, this);
case 2:
ChangeColour(boxColour);
return new Box(this.x, this.y + this.h / 2, this.w / 2, this.h / 2, boxColour, this);
case 3:
ChangeColour(boxColour);
return new Box(this.x + this.w / 2, this.y + this.h / 2, this.w / 2, this.h / 2, boxColour, this);
default:
Box temp = new Box(this.x, this.y, this.w, this.h, boxColour);
temp.isRealBox = false;
return temp;
}
}
// Function for drawing the box. Uses 4 lines
public void drawBox() {
stroke(cH, cS, cB);
line(this.x, this.y, this.x + this.w, this.y);
line(this.x + this.w, this.y, this.x + this.w, this.y + this.h);
line(this.x + this.w, this.y + this.h, this.x, this.y + this.h);
line(this.x, this.y + this.h, this.x, this.y);
}
public void drawBox(int x, int y) {
stroke(cH, cS, cB);
switch (parentBox.getSector(x, y)) {
case 0:
line(parentBox.x + parentBox.w / 2, parentBox.y, parentBox.x + parentBox.w / 2, parentBox.y + parentBox.h / 2);
line(parentBox.x + parentBox.w / 2, parentBox.y + parentBox.h / 2, parentBox.x, parentBox.y + parentBox.h / 2);
break;
case 1:
line(parentBox.x + parentBox.w / 2, parentBox.y, parentBox.x + parentBox.w / 2, parentBox.y + parentBox.h / 2);
line(parentBox.x + parentBox.w / 2, parentBox.y + parentBox.h / 2, parentBox.x + parentBox.w, parentBox.y + parentBox.h / 2);
break;
case 2:
line(parentBox.x, parentBox.y + parentBox.h / 2, parentBox.x + parentBox.w / 2, parentBox.y + parentBox.h / 2);
line(parentBox.x + parentBox.w / 2, parentBox.y + parentBox.h / 2, parentBox.x + parentBox.w / 2, parentBox.y + parentBox.h);
break;
case 3:
line(parentBox.x + parentBox.w / 2, parentBox.y + parentBox.h / 2, parentBox.x + parentBox.w, parentBox.y + parentBox.h / 2);
line(parentBox.x + parentBox.w / 2, parentBox.y + parentBox.h / 2, parentBox.x + parentBox.w / 2, parentBox.y + parentBox.h);
break;
}
}
}
void setup() {
size(810, 810);
colorMode(HSB);
strokeWeight(2);
frameRate(60);
// Initialising firstBox, which offsets from the side
firstBox = new Box(WINDOW_OFFSET / 2, WINDOW_OFFSET / 2, width - WINDOW_OFFSET, height - WINDOW_OFFSET, boxColour);
boxArray = new ArrayList<Box>();
boxArray.add(firstBox);
pointPos = new ArrayList<PVector>();
}
void draw() {
background(0, 0, 0);
for (int loop = 0; loop < 3; loop++) {
randomXPoint = (int)random(WINDOW_OFFSET / 2, width - WINDOW_OFFSET / 2);
randomYPoint = (int)random(WINDOW_OFFSET / 2, height - WINDOW_OFFSET / 2);
boolean addedBox = false;
for (int i = boxArray.size() - 1; i >= 0; i--) {
if (!addedBox) {
if (boxArray.get(i).isInsideBox(randomXPoint, randomYPoint)) {
boxArray.add(boxArray.get(i).ifInsideBox(randomXPoint, randomYPoint));
addedBox = true;
}
}
}
//point(randomXPoint, randomYPoint);
pointPos.add(new PVector(randomXPoint, randomYPoint));
// Draws every box
for (int i = boxArray.size() - 1; i >= 1; i--) {
if (boxArray.get(i).isRealBox == true)
boxArray.get(i).drawBox();
}
boxArray.get(0).drawBox();
stroke(pointColour[0], pointColour[1], pointColour[2]);
for (PVector pos : pointPos) {
point(pos.x, pos.y);
}
}
}
float scaleFactor = 5 * (mouseY/(float)height) - 1; // max-scale 500%
translate(-width/2 * scaleFactor, -height/2 * scaleFactor);
scale(scaleFactor + 1);