带椭圆的冒泡排序
Bubble Sort with ellipse
目标:
我想将随机生成的椭圆排序到它们的半径。从最小 -> 最大
问题:
每次我运行,都会出现这个错误。 ArrayIndexOutOfBunds
.
这是我的代码:
Ellipse[] e = new Ellipse[100];
int j;
void setup() {
fullScreen();
for (int i = 0; i<e.length; i++) {
e[i] = new Ellipse(random(10, width-10), height/2, random(10, 80), color(random(1, 255)));
}
}
void draw() {
background(255);
for (int i = 0; i<e.length; i++) {
e[i].show();
}
if (j<e.length) {
for (int i = 0; i<e.length-1; i++) {
float a = e[i].r;
float b = e[i+1].r;
if (a>b) {
swap( int(e[i].x), int(e[i+1].x));
}
}
}
j++;
}
void swap(int a, int b) {
float zwischen = e[a].x;
e[a].x = e[b].x;
e[b].x = zwischen;
}
class Ellipse {
float x, y, r;
color c;
Ellipse(float x_, float y_, float r_, color c_) {
x = x_;
y = y_;
r = r_;
c = c_;
}
void show() {
fill(c);
ellipse(x, y, r, r);
}
}
您想交换索引为 i
的元素和索引为 i+1
的元素。函数 swap
交换 2 个元素。函数的参数是元素的索引。所以它必须是:
swap( int(e[i].x), int(e[i+1].x));
swap(i, i+1);
为了使算法工作,列表中椭圆的索引必须对应于椭圆沿 x 轴的位置。如果 x 轴和 y 轴是随机的,椭圆将按其大小排序到随机位置。你如何根据椭圆沿 x 轴的半径对椭圆进行排序。
通过使用随机半径提升 x 坐标来创建椭圆:
for (int i = 0; i<e.length; i++) {
int x = 10 + (width-20) * i / e.length;
e[i] = new Ellipse(x, height/2, random(10, 80), color(random(1, 255)));
}
此外,swap
需要交换椭圆,但必须保持沿x轴的位置:
void swap(int a, int b) {
float ax = e[a].x;
float bx = e[b].x;
Ellipse temp = e[a];
e[a] = e[b];
e[b] = temp;
e[a].x = ax;
e[b].x = bx;
}
看例子:
Ellipse[] e = new Ellipse[100];
int j=0;
void setup() {
fullScreen();
for (int i = 0; i<e.length; i++) {
int x = 80 + (width-160) * i / e.length;
e[i] = new Ellipse(x, height/2, random(10, 80), color(random(1, 255)));
}
}
void draw() {
background(255);
for (int i = 0; i<e.length; i++) {
e[i].show();
}
if (j<e.length) {
for (int i = 0; i < e.length-1-j; i++) {
float a = e[i].r;
float b = e[i+1].r;
if (a > b) {
swap(i, i+1);
}
}
}
j++;
}
void swap(int a, int b) {
float ax = e[a].x;
float bx = e[b].x;
Ellipse temp = e[a];
e[a] = e[b];
e[b] = temp;
e[a].x = ax;
e[b].x = bx;
}
class Ellipse {
float x, y, r;
color c;
Ellipse(float x_, float y_, float r_, color c_) {
x = x_;
y = y_;
r = r_;
c = c_;
}
void show() {
fill(c);
ellipse(x, y, r, r);
}
}
目标: 我想将随机生成的椭圆排序到它们的半径。从最小 -> 最大
问题:
每次我运行,都会出现这个错误。 ArrayIndexOutOfBunds
.
这是我的代码:
Ellipse[] e = new Ellipse[100];
int j;
void setup() {
fullScreen();
for (int i = 0; i<e.length; i++) {
e[i] = new Ellipse(random(10, width-10), height/2, random(10, 80), color(random(1, 255)));
}
}
void draw() {
background(255);
for (int i = 0; i<e.length; i++) {
e[i].show();
}
if (j<e.length) {
for (int i = 0; i<e.length-1; i++) {
float a = e[i].r;
float b = e[i+1].r;
if (a>b) {
swap( int(e[i].x), int(e[i+1].x));
}
}
}
j++;
}
void swap(int a, int b) {
float zwischen = e[a].x;
e[a].x = e[b].x;
e[b].x = zwischen;
}
class Ellipse {
float x, y, r;
color c;
Ellipse(float x_, float y_, float r_, color c_) {
x = x_;
y = y_;
r = r_;
c = c_;
}
void show() {
fill(c);
ellipse(x, y, r, r);
}
}
您想交换索引为 i
的元素和索引为 i+1
的元素。函数 swap
交换 2 个元素。函数的参数是元素的索引。所以它必须是:
swap( int(e[i].x), int(e[i+1].x));
swap(i, i+1);
为了使算法工作,列表中椭圆的索引必须对应于椭圆沿 x 轴的位置。如果 x 轴和 y 轴是随机的,椭圆将按其大小排序到随机位置。你如何根据椭圆沿 x 轴的半径对椭圆进行排序。
通过使用随机半径提升 x 坐标来创建椭圆:
for (int i = 0; i<e.length; i++) {
int x = 10 + (width-20) * i / e.length;
e[i] = new Ellipse(x, height/2, random(10, 80), color(random(1, 255)));
}
此外,swap
需要交换椭圆,但必须保持沿x轴的位置:
void swap(int a, int b) {
float ax = e[a].x;
float bx = e[b].x;
Ellipse temp = e[a];
e[a] = e[b];
e[b] = temp;
e[a].x = ax;
e[b].x = bx;
}
看例子:
Ellipse[] e = new Ellipse[100];
int j=0;
void setup() {
fullScreen();
for (int i = 0; i<e.length; i++) {
int x = 80 + (width-160) * i / e.length;
e[i] = new Ellipse(x, height/2, random(10, 80), color(random(1, 255)));
}
}
void draw() {
background(255);
for (int i = 0; i<e.length; i++) {
e[i].show();
}
if (j<e.length) {
for (int i = 0; i < e.length-1-j; i++) {
float a = e[i].r;
float b = e[i+1].r;
if (a > b) {
swap(i, i+1);
}
}
}
j++;
}
void swap(int a, int b) {
float ax = e[a].x;
float bx = e[b].x;
Ellipse temp = e[a];
e[a] = e[b];
e[b] = temp;
e[a].x = ax;
e[b].x = bx;
}
class Ellipse {
float x, y, r;
color c;
Ellipse(float x_, float y_, float r_, color c_) {
x = x_;
y = y_;
r = r_;
c = c_;
}
void show() {
fill(c);
ellipse(x, y, r, r);
}
}