Need help with a problem using Processing language: ClassAssignment2.zip include
ID: 3829467 • Letter: N
Question
Need help with a problem using Processing language:
ClassAssignment2.zip includes 4 files (copy/paste them):
ClassAssignment2:
ArrayList<BouncingBall> bouncingList;
ArrayList<OrbitBall> orbitList;
ArrayList<Car> carList;
void setup()
{
size(500, 500);
bouncingList = new ArrayList<BouncingBall>();
orbitList = new ArrayList<OrbitBall>();
carList = new ArrayList<Car>();
bouncingList.add(new BouncingBall(random(100, width-100), random(100, height-100), new PVector(random(-2, 2), random(-2, 2))));
orbitList.add(new OrbitBall(random(0, 0.1), random(100, width-100), random(100, height-100), random(10, 100)));
carList.add(new Car(random(100, width-100), random(100, height-100), random(1, 5)));
}
void draw()
{
background(200);
for(int i = 0; i < bouncingList.size(); i++)
{
BouncingBall b = bouncingList.get(i);
b.Update();
}
for(int i = 0; i < orbitList.size(); i++)
{
OrbitBall b = orbitList.get(i);
b.Update();
}
for(int i = 0; i < carList.size(); i++)
{
Car c = carList.get(i);
c.Update();
}
}
BouncingBall:
class BouncingBall
{
float x,y,w,h;
PVector speed;
BouncingBall(float inX, float inY, PVector inSpeed)
{
x = inX;
y = inY;
w = 25;
h = 25;
speed = inSpeed;
}
void Update()
{
x += speed.x;
y += speed.y;
if(x - w/2 < 0 || x + w/2 > width)
{
speed.x *= -1;
}
if(y - h/2 < 0 || y + h/2 > height)
{
speed.y *= -1;
}
fill(0, 255, 0);
ellipse(x, y, w, h);
}
}
Car:
class Car
{
float x,y,w,h;
float speedX;
Car(float inX, float inY, float inSpeedX)
{
x = inX;
y = inY;
w = 50;
h = 20;
speedX = inSpeedX;
}
void Update()
{
x += speedX;
if(x > width)
{
x = 0 - w;
}
fill(255, 0, 0);
rect(x, y, w, h);
fill(0);
ellipse(x, y, 10, 10);
ellipse(x+w, y, 10, 10);
ellipse(x, y+h, 10, 10);
ellipse(x+w, y+h, 10, 10);
}
}
OrbitBall:
class OrbitBall
{
float x,y,w,h;
float angle;
float speed;
float orbitX, orbitY;
float distance;
OrbitBall(float inSpeed, float inOrbitX, float inOrbitY, float inDistance)
{
x = 0;
y = 0;
w = 25;
h = 25;
distance = inDistance;
speed = inSpeed;
orbitX = inOrbitX;
orbitY = inOrbitY;
}
void Update()
{
angle -= speed;
x = orbitX + (distance * cos(angle));
y = orbitY + (distance * sin(angle));
fill(0, 0, 255);
ellipse(x,y,w,h);
}
}
Explanation / Answer
#include<dos.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
void main()
{
int d=DETECT,m;
initgraph(&d,&m,"e: ccgi");
float x=1,y=0.00000,j=.5,count=.1;
float r=15;
setcolor(10);
line(0,100,150,350);
sleep(1);
for(int k=0;k<=7;k++)
{
for(float i=90;i<400;i+=10)
{
y=cos(((i*22/7)/170))/j;
if(y>0)
y=-y;
x+=5;
setcolor(10);
setfillstyle(1,10);
circle(x,y*100+200,r);
floodfill(x,y*100+200,14);
delay(100);
setcolor(0);
setfillstyle(1,0);
circle(x,y*100+150,r);
floodfill(x,y*100+150,0);
}
j+=count;
count+=.1;
}
getch();
}