Finding Perimeter of Triangle
in
Programming Questions
•
9 months ago
Hello,
I'm trying to get my particles to bounce off of the triangles that are created with each new sound. I know I need to somehow get the perimeter of the whole triangle so that i can calculate the distance between that and the particles, but I'm not sure how to do that. Any help would be greatly appreciated.
//1
import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import processing.opengl.*;
ArrayList triparts;
//declare arraylist
ArrayList parts;
//2
Minim minim;
AudioInput input;
float a, a2;
void setup()
{
size(1000, 1000, OPENGL);
triparts = new ArrayList();
//construct arraylist
parts = new ArrayList();
//3
minim = new Minim(this);
//512 works as an array of ampllitude
input = minim.getLineIn(Minim.MONO, 360);
}
void draw()
{
background(67);
//4
a = abs(input.left.get(0))*20;
if (a > a2) {
a2 = a;
}
a2*=.3;
if (a2 > .9) {
for (int i = 0; i<1; i++)
{
triparts.add(new Peep());
}
}
for (int i = 0; i < triparts.size(); i++)
{
//pull the particle out of the arraylist - cast it as type "Particle"
Peep tri = (Peep) triparts.get(i);
tri.display();
tri.update();
}
}
noFill();
stroke(255, 100);
if (a2 > .5) {
for (int i = 0; i<30; i++)
{
parts.add(new Particle());
}
}
//use parts.size to get everything in arraylist
for (int i = 0; i < parts.size(); i++)
{
//pull the particle out of the arraylist - cast it as type "Particle"
Particle p = (Particle) parts.get(i);
p.update();
p.display();
p.bounce();
}
}
}
class Particle
{
float x, y, vx, vy, angle, drag, grav;
float alf;
float inc = 0.0;
Particle()
{
x = random(width);
y = height/4;
angle = random(0, 2*PI);
vx = sin(angle)*a2;
vy = cos(angle)*a2;
drag = random(.93,.98);
//to change how it falls
grav = .05;
alf = 150;
}
void update()
{
alf-=.5;
x+=vx;
y+=vy;
//slow the velocity
vx*=drag;
vy*=drag;
///add on the gravity to the velocity of Y
vy+=grav;
}
void display() {
stroke(255);
pushMatrix();
translate(x,y);
ellipse(0,0,10,10);
popMatrix();
}
void bounce() {
if((y > height)) {
stroke(0);
vy = (vy * -2) *(random(2,3));
inc += 0.01;
}
}
}
class Peep
{
float x, y, z;
float alf = 100;
Peep()
{
x = random(0, width);
y = 0;
}
void update()
{
alf-=.5;
}
void display()
{
//triangle coordinate
float ty2;
ty2 = (x+200);
noStroke();
fill(0,0,0,alf);
triangle(0, (x+50), 0, (x+75), (x+100), ty2);
}
}
1