Обработка - Как изменить размер фигуры - PullRequest
2 голосов
/ 15 апреля 2020

Используя мышь, я бы хотел изменить размер фигур, которые есть в моем коде (это круг и прямоугольник), щелкнув по углам и перетащив его, чтобы изменить ширину и высоту формы.

Вот мой основной код:

circleShape [] objs;
int numObjs = 1;
int pickedItem = -1;
rectShape [] objss;
int numObjss = 1;
int itemPicked = -1;
void setup() {
  size(600, 600);  
  objs = new circleShape[numObjs];  
  for (int i = 0; i < numObjs; i++) {
    int radius = (int)(random(20, 70));
    color c = (int)(random(0, 255));
    objs[i] = new circleShape(radius, c);
  }
  objss = new rectShape[numObjss];
  for (int i = 0; i < numObjss; i++) {
    int Radius = (int)(random(20, 70));
    color C = (int)(random(0, 255));
    objss[i] = new rectShape(Radius, C);
  }
}

void draw() { 
  background(255);
  for (int i = 0; i < numObjs; i++) {
    objs[i].drawShape();
  }
  for (int i =0; i < numObjss; i++) {
   objss[i].shapeDraw(); 
  }
}  

// return the index number of the picked item from the array
// return -1 otherwise
int getPickedObj(int x, int y) {
  for (int i = 0; i < numObjs; i++) {
    if (objs[i].isOnShape(x, y)) {
      return i;
    }
  }
  return -1;
}

int pickedObj(int X, int Y) {
  for (int i = 0; i < numObjss; i++) {
    if (objss[i].onShape(X, Y)) {
      return i;
    }
  }
  return -1;
}

void mousePressed() {
  pickedItem = getPickedObj(mouseX, mouseY);
  itemPicked = pickedObj(mouseX, mouseY);
}

void mouseDragged() {
  if (pickedItem >= 0) {
    objs[pickedItem].move(mouseX, mouseY);
    objs[pickedItem].drawShape();
  }
  if (itemPicked >= 0) {
    objss[itemPicked].move(mouseX, mouseY);
    objss[itemPicked].shapeDraw();
  }
}

А вот мой код формы:

// a shape is simply as a circle
class circleShape {
  int posX, posY, radius;
  color c;

  circleShape(int radius, color c) {
    this.radius = radius;
    this.c = c;
    posX = (int)random(0, 200);
    posY = (int)random(0, 200);
  }

  // return true if the position x, y is on the shape
  boolean isOnShape(int x, int y) {
    float d = (posX - x)*(posX - x) + (posY - y)*(posY - y);
    d = sqrt(d);
    if (d < radius)  return true;
    else             return false; 
  }

  void move (int newPosX, int newPosY) {
    this.posX = newPosX;
    this.posY = newPosY;
  }

  void drawShape() {
    noStroke();
    fill(c);
    ellipse(posX, posY, 2*radius, 2*radius);
  }
}
//Rectangle Shape
class rectShape {
  int xPos, yPos, Radius;
  color C;

  rectShape(int Radius, color C) {
    this.Radius = Radius;
    this.C = C;
    xPos = (int)random(0, 200);
    yPos = (int)random(0, 200);
  }

  boolean onShape(int X, int Y) {
    float D = (xPos - X)*(xPos - X) + (yPos - Y)*(yPos - Y);
    D = sqrt(D);
    if (D < Radius)  return true;
    else             return false; 
  }

  void move (int posXnew, int posYnew) {
    this.xPos = posXnew;
    this.yPos = posYnew;
  }

  void shapeDraw() {
    noStroke();
    fill(C);
    rect(xPos, yPos, 2*Radius+60, 2*Radius);
  }
}

Мой код позволяет мыши перемещать фигуры из одного места в другое. Просто нужно уметь изменять их размер.

1 Ответ

1 голос
/ 15 апреля 2020

Я приведу пример прямоугольной формы angular.

Прямоугольник определяется положением, шириной и высотой. Измените атрибуты и конструктор прямоугольника:

class rectShape {
    int xPos, yPos, Width, Height;
    color C;

    rectShape(int X, int Y, int Width, int Height, color C) {
        this.xPos = X;
        this.yPos = Y;
        this.Width = Width;
        this.Height = Height;
        this.C = C;
    }

    // [...]

    void shapeDraw() {
        noStroke();
        fill(C);
        rect(xPos, yPos, this.Width, this.Height);
    }
}

Создайте случайный прямоугольник:

void setup() {
    size(600, 600);  

    // [...]

    objss = new rectShape[numObjss];
    for (int i = 0; i < numObjss; i++) {
        int xPos = (int)random(0, 200);
        int yPos = (int)random(0, 200);
        int Width = (int)(random(20, 70));
        int Height = (int)(random(20, 70));
        color C = (int)(random(0, 255));
        objss[i] = new rectShape(xPos, yPos, Width, Height, C);
    }
}

Создайте метод в классе rectShape, который определяет, включена ли мышь прямоугольник и, если он находится слева, справа, сверху, снизу или по центру:

String onShape(int X, int Y) {

    String where = "";
    Boolean isOn = X > xPos && X < xPos + Width && Y > yPos && Y < yPos + Height;
    if (isOn) {
        int left = abs(X - xPos);
        int right = abs(X - (xPos + Width));
        int top = abs(Y - yPos);
        int bottom = abs(Y - (yPos + Height));

        if (min (left, right) < min (top, bottom)) {
            if (left < 10)
                where = "left";
            else if (right < 10)
                where = "right";
            else
                where = "center";
        }
        else {
            if (top < 10)
                where = "top";
            else if (bottom < 10)
                where = "bottom";
            else
                where = "center";
        }
    }
    return where;
}

Создайте метод, который может изменять прямоугольник в зависимости от команды действия:

void update(String what, int posXnew, int posYnew) {

    if (what == "left") {
        if (posXnew < this.xPos + this.Width) {
            this.Width = this.xPos + this.Width - posXnew;
            this.xPos = posXnew;
        }
    }
    else if (what == "right") {
        if (posXnew > this.xPos) {
            this.Width = posXnew - this.xPos;
        }
    }
    else if (what == "top") {
        if (posYnew < this.yPos + this.Height) {
            this.Height = this.yPos + this.Height - posYnew;
            this.yPos = posYnew;
        }
    }
    else if (what == "bottom") {
        if (posYnew > this.yPos) {
            this.Height = posYnew - this.yPos;
        }
    }
    else {
        this.xPos = posXnew - this.Width / 2;
        this.yPos = posYnew - this.Height / 2;
    }
}

Определите действие при нажатии на прямоугольник:

String itemAction = "";
int pickedObj(int X, int Y) {
    for (int i = 0; i < numObjss; i++) {
        itemAction = objss[i].onShape(X, Y);
        if (itemAction != "") {
             return i;
        }
    }
    return -1;
}

void mousePressed() {
    // [...]
    itemPicked = pickedObj(mouseX, mouseY);
}

Передайте действие методу update при перетаскивании мыши:

void mouseDragged() {

    if (itemPicked >= 0) {
        objss[itemPicked].update(itemAction, mouseX, mouseY);
    }
}

Завершите занятие rectShape:

enter image description here

//Rectangle Shape
class rectShape {
    int xPos, yPos, Width, Height;
    color C;

    rectShape(int X, int Y, int Width, int Height, color C) {
        this.xPos = X;
        this.yPos = Y;
        this.Width = Width;
        this.Height = Height;
        this.C = C;
    }

    String onShape(int X, int Y) {

        String where = "";
        Boolean isOn = X > xPos && X < xPos + Width && Y > yPos && Y < yPos + Height;
        if (isOn) {
            int left = abs(X - xPos);
            int right = abs(X - (xPos + Width));
            int top = abs(Y - yPos);
            int bottom = abs(Y - (yPos + Height));

            if (min (left, right) < min (top, bottom)) {
                if (left < 10)
                    where = "left";
                else if (right < 10)
                    where = "right";
                else
                    where = "center";
            }
            else {
                if (top < 10)
                    where = "top";
                else if (bottom < 10)
                    where = "bottom";
                else
                    where = "center";
            }
        }
        return where;
    }

    void update(String what, int posXnew, int posYnew) {

        if (what == "left") {
            if (posXnew < this.xPos + this.Width) {
                this.Width = this.xPos + this.Width - posXnew;
                this.xPos = posXnew;
            }
        }
        else if (what == "right") {
            if (posXnew > this.xPos) {
                this.Width = posXnew - this.xPos;
            }
        }
        else if (what == "top") {
            if (posYnew < this.yPos + this.Height) {
                this.Height = this.yPos + this.Height - posYnew;
                this.yPos = posYnew;
            }
        }
        else if (what == "bottom") {
            if (posYnew > this.yPos) {
                this.Height = posYnew - this.yPos;
            }
        }
        else {
            this.xPos = posXnew - this.Width / 2;
            this.yPos = posYnew - this.Height / 2;
        }
    }

    void shapeDraw() {
        noStroke();
        fill(C);
        rect(xPos, yPos, this.Width, this.Height);
    }
}
...