在线路功能中添加缩放机制的最佳方法



我制作了一个脚本,可以在较大的矩形内制作矩形,我将其调用框。现在,我正在尝试实现一种机制,以使其看起来像您正在放大。im使用line((函数绘制盒子。有人有建议将所有内容延续吗?

代码(很长(:

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);

相关内容

最新更新