Aufgrund eines Versionsupdates wird GitLab am 01.04. zwischen 9:00 und 9:30 Uhr kurzzeitig nicht zur Verfügung stehen. / Due to a version upgrade, GitLab won't be accessible at 01.04. between 9:00 and 9:30 a.m.

DrawableVehicle.cpp 1.58 KB
Newer Older
Martin Kröning's avatar
Martin Kröning committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include "DrawableVehicle.hpp"
#include "DrawableRoad.hpp"
#include "cairomm/context.h"
#include "cairomm/refptr.h"
#include <cmath>
#include <utility>

const double DrawableVehicle::radius = 5;

DrawableVehicle::DrawableVehicle(std::string carName, std::string laneName,
                                 const DrawableRoad &road,
                                 const double positionOnLane,
                                 const double speed, const double remainingFuel,
                                 const double red, const double green,
                                 const double blue)
    : xc(0), yc(0), carName(std::move(carName)), laneName(std::move(laneName)),
      positionOnLane(positionOnLane), speed(speed),
      remainingFuel(remainingFuel), red(red), green(green), blue(blue) {
  road.getCoordinatesOf(this->laneName, positionOnLane, xc, yc);
}

void DrawableVehicle::draw(const Cairo::RefPtr<Cairo::Context> &cr) const {
  cr->save();

  cr->set_source_rgb(red, green, blue);
  cr->arc(xc, yc, radius, 0, 2 * std::acos(-1));
  cr->fill();

  cr->restore();
}

void DrawableVehicle::getRegion(int &x, int &y, int &width, int &height) const {
  x = static_cast<int>(std::floor(xc - radius)) - 1;
  y = static_cast<int>(std::floor(yc - radius)) - 1;
  width = height = static_cast<int>(std::ceil(2 * radius)) + 2;
}

std::string DrawableVehicle::getLaneName() const { return laneName; }

double DrawableVehicle::getPositionOnLane() const { return positionOnLane; }

double DrawableVehicle::getSpeed() const { return speed; }

double DrawableVehicle::getRemainingFuel() const { return remainingFuel; }