I have a task to reflect one or more QGraphicsItems relatively to a line. And this operation should be performed correctly again and again to the mirrored items.
I've read a lot about QTransform and affine transformations and realized that I need 1) rotate; 2) scale (1;-1) 3) translate..somewhere
that's what I have now:
double x1 = line->line().x1();
double y1 = line->line().y1();
double x2 = line->line().x2();
double y2 = line->line().y2();
double angle = atan((y2 - y1)/(x2 - x1));
group->rotate(angle*180/acos(-1.0));
group->scale(1,-1);
group->rotate(-angle*180/acos(-1.0));
item->setTransform (group->transform(),true);
destroyItemGroup(group);
removeItem(line);
delete line;
curItem = NULL;
QGraphicsLineItem* line = qgraphicsitem_cast<QGraphicsLineItem*>(curItem);
double x1 = line->line().x1();
double y1 = line->line().y1();
double x2 = line->line().x2();
double y2 = line->line().y2();
double angle = atan((y2 - y1)/(x2 - x1));
QMessageBox::warning(0, "Scribble", "angle "+QString::number(angle*180/acos(-1.0)));
group->rotate(angle*180/acos(-1.0));
group->scale(1,-1);
group->rotate(-angle*180/acos(-1.0));
foreach (QGraphicsItem* item,group->children())
item->setTransform (group->transform(),true);
destroyItemGroup(group);
removeItem(line);
delete line;
curItem = NULL;
To copy to clipboard, switch view to plain text mode
This is steps 1-2 as I don't know how to translate items correctly regardless of their pos() and previous mirror operations. At least the resulting angle of mirrored item seems to be right, but not after the second reflection
"curItem" holds the line of the mirror (it's temporal and is erased after the operation), "group" of items to be reflected is created elsewhere.
Thanks in advance for all your help. I attached the whole project (Qt 4.3.2, Visual Studio 2005 .vcproj, but without a .pro, sorry)
Bookmarks