Elements used in the Balls and Things games for the RETRO.

Dependents:   RETRO_BallsAndPaddle RETRO_BallAndHoles

Shapes.cpp

Committer:
maxint
Date:
2015-02-06
Revision:
0:3d0db4e183ee
Child:
3:441dc90d10ce

File content as of revision 0:3d0db4e183ee:

#include "mbed.h"
#include "Shapes.h"



///////////////////
// POINT
///////////////////
Point::Point()
{   // constructor
    x1=0;
    y1=0;
}

Point::Point(int x, int y)
{   // constructor
    x1=x;
    y1=y;
}

int Point::getX()
{
    return x1;
}

int Point::getY()
{
    return y1;
}

void Point::set(int x, int y)
{
    x1=x;
    y1=y;
}


///////////////////
// RECTANGLE
///////////////////

Rectangle::Rectangle(int x,int y, int xx, int yy)
{
    x1 = x;
    x2 = xx;
    y1 = y;
    y2 = yy;
}

Rectangle::Rectangle(Point pt1, Point pt2)
{
    x1 = pt1.getX();
    x2 = pt2.getX();
    y1 = pt1.getY();
    y2 = pt2.getY();
}


bool Rectangle::collides(Point pt)
{
    if(pt.getX() >= x1 && pt.getX() <= x2) {
        if(pt.getY() >= y1 && pt.getY() <= y2) {
            return true;
        }
    }
    return false;
}

/*
Rectangle Rectangle::intersection(Rectangle r)
{   // return the intersection of two rectangles or null
    Rectangle rResult;
    rResult=Rectangle()
    if(this->x2 >= 
}
*/

bool Rectangle::collides(Rectangle r)
{   // check if two rectangles collide
    // method 1: check if corners of eithter rectangle collide
    // method 2: compare sides
    if(this->collides(r.get1()) || this->collides(r.get2())) return(true);
    if(this->collides(r.get3()) || this->collides(r.get4())) return(true);
    if(r.collides(this->get1()) || r.collides(this->get2())) return(true);
    if(r.collides(this->get3()) || r.collides(this->get4())) return(true);
    // TODO: check other corners
    return(false);
}


Point Rectangle::get1()
{
    return(Point(x1, y1));
}

Point Rectangle::get2()
{
    return(Point(x2, y2));
}

Point Rectangle::get3()
{
    return(Point(x2, y1));
}

Point Rectangle::get4()
{
    return(Point(x1, y2));
}
 

 
int Rectangle::getX1()
{
    return x1;
}
 
int Rectangle::getX2()
{
    return x2;
}
 
int Rectangle::getY1()
{
    return y1;
}
 
int Rectangle::getY2()
{
    return y2;
}
 
int Rectangle::getCenterX()
{
    return x1 + (x2-x1)/2;
}
 
int Rectangle::getCenterY()
{
    return y1 + (y2-y1)/2;
}

Point Rectangle::getCenter()
{
    return(Point(x1 + (x2-x1)/2, y1 + (y2-y1)/2));
}

void Rectangle::set(Rectangle rNew)
{
    x1=rNew.getX1();
    y1=rNew.getY1();
    x2=rNew.getX2();
    y2=rNew.getY2();
}

void Rectangle::move(Vector v)
{
    x1+=rint(v.x);
    y1+=rint(v.y);
    x2+=rint(v.x);
    y2+=rint(v.y);
}


///////////////////
// CIRCLE
///////////////////
Circle::Circle(int x,int y, int r)
{
    x1=x;
    y1=y;
    r1=r;
}

Point Circle::getCenter()
{
    return(Point(x1, y1));
}

int Circle::getRadius()
{
    return(r1);
}

int Circle::getX()
{
    return(x1);
}

int Circle::getY()
{
    return(y1);
}

void Circle::setX(int x)
{
    x1=x;
}

void Circle::setY(int y)
{
    y1=y;
}

void Circle::setXY(int x, int y)
{
    x1=x;
    y1=y;
}

void Circle::move(int x, int y)
{
    x1+=x;
    y1+=y;
}

void Circle::move(Vector v)
{
    x1+=rint(v.x);
    y1+=rint(v.y);
}


Rectangle Circle::getBoundingRectangle()
{
    return(Rectangle(x1-r1, y1-r1, x1+r1, y1+r1));
}