left details out first time posting but this problem requires C++ program langua
ID: 666269 • Letter: L
Question
left details out first time posting but this problem requires C++ program language;
create a program that makes two rectangular shapes and animates them. the shapes must start on opposite ends of the screen and move toward eachother. when they meet in the middle each shape reverses course moving towards the edge of the screen. the two shapes bounce off eachother in the middle of the screen. terminate program when the shapes meet in the middle ten times. document the program and use two colors to show rectangular shape.
Explanation / Answer
We can write the above program by adding Shape and Bouncer and MoleBouncer.
DrawRectangle (const Point& p1, const Point& p2);// How to draw a Rectangle
========================================================
Programme to Repeat the collision on screen
class MoleBouncer : public Bouncer
{
public:
MoleBouncer(Shape& s, double angle, double v)
: Bouncer(s,angle,v)
{ }
virtual void update(AnimatedCanvas& ac)
{
RandGen rgen;
Iterator<Shape> it(ac.makeIterator());
bool collided = false; // collision or still bouncing?
Point p = getLocation();
double angle = getAngle();
for(it.Init(); it.HasMore(); it.Next())
{ // check for collision, but not with myself
if (it.Current().id() != this>id() && it.Current().overlaps(this))
{ ac.removeShape(this);
ac.addShape(
new MoleBouncer(
RectangleShape(Point(rgen.RandReal(0,ac.width()/10),
rgen.RandReal(0,ac.height()/10)),
RADIUS, CanvasColor::BLUE), 2PI angle, 4));
collided = true;
break;
}
}
if (!collided) // no collision, update
{ Bouncer::update(ac);
}
}
};
==================================================
Draw two rectangles
#include "canvas.h"
#include "prompt.h"
#include "randgen.h"
#include "dice.h"
Point getPoint(Canvas& c)
// postcondition: return a random point in Canvas c
{
RandGen gen;
return Point(gen.RandReal(0,c.width()), gen.RandReal(0,c.height()));
}
void drawShape(Canvas & c)
// postcondition: random shape/random size drawn on c
{
const int NUM_SHAPES = 2; // #
const int MAX_SIZE = 2; // max size of a shape
Dice shapeDie(NUM_SHAPES); // for randomizing selections
Dice sizeDie(MAX_SIZE); // for randomizing size
Point p1(getPoint(c));
Point p2(p1.x + sizeDie.Roll(), p1.y + sizeDie.Roll());
switch (shapeDie.Roll())
{
case 1 :
c.DrawRectangle(p1,p2);
}
}
int main()
{
const int WIDTH= 200, HEIGHT= 200;
RandGen rnd;
Canvas c(WIDTH,HEIGHT,20,20);
int numSquares = PromptRange("# of shapes: ",1,1000);
int k;
for(k=0; k < numSquares; k++)
{ c.SetFrame();
c.SetColor(CanvasColor(rnd.RandInt(0,255), rnd.RandInt(0,255),
rnd.RandInt(0,255)));
drawShape(c);
}
c.runUntilEscape();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.