c++ - Qt rotating shapes -
i'm experimenting drawing , rotating shapes in qt, i'm @ loss how works. have code draws rectangle small triangle on top of it. want rotate shape 35 degrees, try this:
void window::paintevent(qpaintevent *event) { qpainter painter(this); qbrush brush; brush.setstyle(qt::solidpattern); brush.setcolor(qt::white); painter.setbrush(brush); painter.setpen(qt::nopen); painter.fillrect(0,0,800,800,brush); brush.setcolor(qt::red); painter.translate(s.getx()-5,s.gety()-8); painter.rotate(35); painter.fillrect(0,0,10,16,brush); qpolygon pol; pol.setpoints(3,0,0,10,0,5,10); qpainterpath p; p.addpolygon(pol); painter.fillpath(p,brush); } (ignore s.getx/y() calls, x 150 , y 750.)
without rotating , translating code works fine , draws shape. current code rectangle displayed, not polygon. how rotate these shapes?
you need understand how affine transformations work. without proper understanding have hard times achieving needed.
rotaterotates around center of coordinates: (0,0)translatemoves center of coordinates new position
your code rotates around point (s.getx() - 5, s.gety() - 8).
so here's code rotate both shapes 35 degrees around center of red rectangle:
qpainter painter(this); qbrush brush; brush.setstyle(qt::solidpattern); brush.setcolor(qt::white); painter.setbrush(brush); painter.setpen(qt::nopen); painter.fillrect(0, 0, 800, 800, brush); brush.setcolor(qt::red); painter.translate(150, 750); painter.translate(5, 8); // move center of coordinates center of red rectangle painter.rotate(35); // rotate around center of red rectangle painter.translate(-5, -8); // move center of coordinates painter.fillrect(0, 0, 10, 16, brush); qpolygon pol; pol.setpoints(3, 0, 0, 10, 0, 5, 10); qpainterpath p; p.addpolygon(pol); brush.setcolor(qt::blue); painter.fillpath(p, brush); without transformations:
with transformations:


Comments
Post a Comment