Hallo
ich möchte den Radius zwei Ellipsen berechnen, um herauszufinden, ob sie miteinander kollidiert sind.
ich kann den Abstand mittels dist Methode herausfinden, allerdings weiß es nicht, wie man den Radius berechnen kann.
Hier ist der Code:
_________________________________________________
ich möchte den Radius zwei Ellipsen berechnen, um herauszufinden, ob sie miteinander kollidiert sind.
ich kann den Abstand mittels dist Methode herausfinden, allerdings weiß es nicht, wie man den Radius berechnen kann.
Hier ist der Code:
Java:
import processing.core.PGraphics;
import java.util.Random;
class Ball {
int x, y;
int fillColor;
float xSpeed = 5;
float ySpeed = 6;
int widthBound, heightBound;
Random random = new Random();
Ball(int fillColor, int widthBound, int heightBound) {
this.fillColor = fillColor;
this.widthBound = widthBound;
this.heightBound = heightBound;
this.randomPos();
}
void randomPos() {
this.x = random.nextInt(20, 581);
this.y = random.nextInt(20, 581);
}
void draw(PGraphics g) {
g.fill(this.fillColor);
g.ellipse(this.x, this.y, 40, 40);
x += xSpeed;
y += ySpeed;
if (x < 20 || x > widthBound-20) {
xSpeed *= -1;
}
if (y < 20 || y > heightBound-20) {
ySpeed *= -1;
}
}
}
_________________________________________________
Code:
import processing.core.PApplet;
public class Billiardtisch extends PApplet {
public static void main(String[] args) {
PApplet.runSketch(new String[]{""}, new Billiardtisch());
}
Ball b1, b2;
Ball[] balls = new Ball[10];
public void settings() {
super.size(600, 600);
}
public void setup() {
float d = dist(b1.x,b1.y,b2.x,b2.y);
b1 = new Ball(color(234,56,21),width,height);
b2 = new Ball(color(2,55,221),width,height);
frameRate(20);
super.background(0);
for(int i = 0; i < balls.length; i++) {
balls[i] = new Ball((color((int) (Math.random()*(255)),(int) (Math.random()*(255)),(int) (Math.random()*(255)))),super.width,super.height);
}
}
public void draw() {
super.background(0,102,51);
b1.draw(super.g);
b2.draw(super.g);
//for (Ball elem : balls) elem.draw(super.g);
}
}