Я приведу пример прямоугольной формы 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
:
//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);
}
}