c++ - How do I get nearest grid intersection? -
i'm trying draw circle @ closest line intersection on grid relative mouse. draw grid center outward varying x , y separations. attempted right coord off. how right coordinate?
here's code:
class grid : public sf::drawable, public sf::transformable { public: grid(unsigned int xsep, unsigned int ysep, unsigned int canvasw, unsigned int canvash); virtual ~grid() {}; void setfillcolor(const sf::color &color); void setsize(unsigned int xsep, unsigned int ysep, unsigned int canvasw, unsigned int canvash); unsigned int xsep = 0; unsigned int ysep = 0; private: virtual void draw(sf::rendertarget& target, sf::renderstates states) const { // apply entity's transform -- combine 1 passed caller states.transform *= gettransform(); // gettransform() defined sf::transformable // apply texture states.texture = &m_texture; // may override states.shader or states.blendmode if want states.blendmode = sf::blendmode(sf::blendmode::srcalpha, sf::blendmode::oneminusdstcolor,sf::blendmode::add); // draw vertex array target.draw(m_vertices, states); } sf::vertexarray m_vertices; sf::texture m_texture; }; grid::grid(unsigned int xsep, unsigned int ysep, unsigned int canvasw, unsigned int canvash) { xsep = xsep; ysep = ysep; m_vertices.setprimitivetype(sf::lines); m_vertices.clear(); (int i=((canvasw/2)-xsep); > 0; i-=xsep) { m_vertices.append(sf::vector2f(i,0)); m_vertices.append(sf::vector2f(i,canvash)); m_vertices.append(sf::vector2f(canvasw-i,0)); m_vertices.append(sf::vector2f(canvasw-i,canvash)); } (int i=((canvash/2)-ysep); > 0; i-=ysep) { m_vertices.append(sf::vector2f(0,i)); m_vertices.append(sf::vector2f(canvasw,i)); m_vertices.append(sf::vector2f(0,canvash-i)); m_vertices.append(sf::vector2f(canvasw,canvash-i)); } m_vertices.append(sf::vector2f(0,canvash / 2)); m_vertices.append(sf::vector2f(canvasw,canvash / 2)); m_vertices.append(sf::vector2f(canvasw / 2, 0)); m_vertices.append(sf::vector2f(canvasw / 2,canvash)); } int roundnum(int num, int difference) { int rem = num % difference; homecoming rem >= 5 ? (num - rem + difference) : (num - rem); } sf::circleshape point(5); point.setorigin(point.getradius()/2,point.getradius()/2); sf::vector2f mousepos = mappixeltocoords(sf::mouse::getposition(*this)); point.setposition(roundnum(mousepos.x,grid.xsep),roundnum(mousepos.y,grid.ysep)); draw(point);
i see @ to the lowest degree 2 things into.
first, set grid lines uniformly spaced center outward. roundnum(mousepos.x,grid.xsep)
snaps position line of grid spaced uniformly wherever x
zero. presumably point not @ center of rectangle. @ corner of rectangle? since width of rectangle not multiple of 2 * grid.xsep
, grid aligned corner offset grid aligned corner. true y-coordinates well.
edited in response comment: 1 way solve problem subtract width/2 x-coordinate, round it, add together width/2. alternatively, find grid.xsep - ((width/2)%grid.xsep)
; add together value x-coordinate, round result, subtract value. sec way more complicated avoids having think happens if x
in x%n
negative. of course of study similar y-coordinates.
second, works when difference
9 or 10:
homecoming rem >= 5 ? (num - rem + difference) : (num - rem);
it work values of difference
if replace 5
difference/2
. create work correctly values of difference
(even or odd), replace 5
(difference + 1)/2
.
it may worth confirming want:
point.setorigin(point.getradius()/2,point.getradius()/2);
it appears correct, it's easy check (just draw circleshape
@ known grid point, such center) might create sure.
c++ sfml
No comments:
Post a Comment