#ifndef LEVELEDGE_H_ #define LEVELEDGE_H_ #include "LevelObject.h" #include "EdgeProvider.h" #include class LevelEdge : public LevelObject { private: EdgeProvider *provider; size_t id; static const float width = 1; public: LevelEdge(EdgeProvider *p, size_t i, LevelObject *parent) : LevelObject(parent), provider(p), id(i) {} virtual bool hit(const Vertex &v, float scale) const { const Edge &edge = **this; const Vertex &v1 = *edge.getVertex1(), &v2 = *edge.getVertex2(); float width = this->width/scale; if(v.getX() < fminf(v1.getX(), v2.getX())-width) return false; if(v.getX() > fmaxf(v1.getX(), v2.getX())+width) return false; if(v.getY() < fminf(v1.getY(), v2.getY())-width) return false; if(v.getY() > fmaxf(v1.getY(), v2.getY())+width) return false; Vertex r = v2 - v1, p = v - v1; r *= (r.getX()*p.getX() + r.getY()*p.getY())/v1.distanceSq(v2); return (r.distanceSq(p) <= width*width); } virtual int getPriority() const {return 500;} virtual const char* getType() const { return "LevelEdge"; } virtual void move(float x, float y) { provider->moveEdge(id, x, y); } virtual void rotate(float a) { provider->rotateEdge(id, a); } virtual Vertex getCenter() const { Line l = provider->getEdge(id)->toLine(); return (l.getVertex1()+l.getVertex2()) / 2; } const Edge* operator->() const {return provider->getEdge(id);} const Edge& operator*() const {return *provider->getEdge(id);} }; #endif /*LEVELEDGE_H_*/