I have drawn diamond on the 2D view. I want to rotate diamond on a particular axis(x-axis). If I have entered 20 degrees in textbox, then the diamond should be rotated on x-axis 20 degrees. Currently, I have top view of diamond and I want to rotate that diamond on a particular axis if I have entered 90 degrees, then diamond should be like side view. I have top view like
image and I want to rotate diamond like
image.
Here is a screenshot of the diamond in the project.
What I have tried:
I tried this code but didn't work as expected. It's rotating like clock wise on the same point. I want to rotate diamond on particular axis.
void MainWindow::on_drawDiamondbtn_clicked()
{
if (isDimond_Drawn)
{
int centerX = ui->CameraGraphicsView->width() / 2;
int centerY = ui->CameraGraphicsView->height() / 2;
double RectSize = Rect_size;
double PixelFact = (1.00 * userenterMM) / RectSize;
double diaMMtoPX = dia_meter * 1.00 / PixelFact;
double TablePX = tbl / 100.00 * (diaMMtoPX);
int circleRadius = diaMMtoPX;
QGraphicsItemGroup *diamondGroup = new QGraphicsItemGroup;
QGraphicsEllipseItem *circle = new QGraphicsEllipseItem
(centerX - circleRadius, centerY - circleRadius, 2 *
circleRadius, 2 * circleRadius);
circle->setPen(QPen(Qt::green)); scene->addItem(circle);
diamondGroup->addToGroup(circle);
ui->drawDiamondbtn->setStyleSheet
("background-color: green;");
QPen linePen(Qt::green); linePen.setWidth(1);
int lineLength = TablePX; int numLines = 8;
double x1, y1, x2, y2;
double x3, y3, x4, y4;
double x5, y5, x6, y6;
double x7, y7, x8, y8;
for (int i = 0; i < numLines; i++) {
double angle = 2 * M_PI * i / numLines; double startX = centerX + lineLength * qCos(angle);
double startY = centerY + lineLength * qSin(angle);
double endX = centerX - lineLength * qCos(angle);
double endY = centerY - lineLength * qSin(angle);
if (i == 0) {
x1 = startX;
y1 = startY;
x2 = endX;
y2 = endY;
} else if (i == 1) {
x3 = startX;
y3 = startY;
x4 = endX;
y4 = endY;
} else if (i == 2) {
x5 = startX;
y5 = startY;
x6 = endX;
y6 = endY;
} else if (i == 3) {
x7 = startX;
y7 = startY;
x8 = endX;
y8 = endY;
}
}
diamondGroup->addToGroup(DrawCrawnLine(x1,y1,x3,y3));
diamondGroup->addToGroup(DrawCrawnLine(x3,y3,x5,y5));
diamondGroup->addToGroup(DrawCrawnLine(x5,y5,x7,y7));
diamondGroup->addToGroup(DrawCrawnLine(x7,y7,x2,y2));
diamondGroup->addToGroup(DrawCrawnLine(x2,y2,x4,y4));
diamondGroup->addToGroup(DrawCrawnLine(x4,y4,x6,y6));
diamondGroup->addToGroup(DrawCrawnLine(x6,y6,x8,y8));
diamondGroup->addToGroup(DrawCrawnLine(x8,y8,x1,y1));
diamondGroup->addToGroup(DrawCrawnAngle
(centerX,centerY, circleRadius, x1, y1));
diamondGroup->addToGroup(DrawCrawnAngle
(centerX,centerY, circleRadius, x2, y2));
diamondGroup->addToGroup(DrawCrawnAngle
(centerX,centerY, circleRadius, x3, y3));
diamondGroup->addToGroup(DrawCrawnAngle
(centerX,centerY, circleRadius, x4, y4));
diamondGroup->addToGroup(DrawCrawnAngle
(centerX,centerY, circleRadius, x5, y5));
diamondGroup->addToGroup(DrawCrawnAngle
(centerX,centerY, circleRadius, x6, y6));
diamondGroup->addToGroup(DrawCrawnAngle
(centerX,centerY, circleRadius, x7, y7));
diamondGroup->addToGroup( DrawCrawnAngle
(centerX,centerY, circleRadius, x8, y8));
scene->addItem(diamondGroup);
}
else {
QList<QGraphicsItem *> items = scene->items();
for (QGraphicsItem *item : items) {
if (item->type() == QGraphicsEllipseItem::Type ||
item->type() == QGraphicsLineItem::Type) {
scene->removeItem(item);
delete item;
}
}
ui->drawDiamondbtn->setStyleSheet
("background-color: red;");
}
isDimond_Drawn = !isDimond_Drawn;
}
QGraphicsLineItem* MainWindow::DrawCrawnLine
(double startX, double startY, double endX, double endY)
{
QGraphicsLineItem *line = new QGraphicsLineItem
(startX, startY, endX, endY);
line->setPen(QPen(Qt::green));
return line;
}
QGraphicsLineItem* MainWindow::DrawCrawnAngle(int centerX, int centerY,
int circleRadius, double startX, double startY)
{
QPen CrawnAngleLinePen(Qt::green); CrawnAngleLinePen.setWidth(1);
double angleToCircumference = qAtan2(startY - centerY, startX - centerX);
double angleForPinkLine = angleToCircumference -
qDegreesToRadians(static_cast<double>(0.01));
double endXOnCircumference = centerX +
circleRadius * qCos(angleForPinkLine);
double endYOnCircumference = centerY + circleRadius *
qSin(angleForPinkLine);
QGraphicsLineItem *CrawnAngleLine = new QGraphicsLineItem
(startX, startY, endXOnCircumference, endYOnCircumference);
CrawnAngleLine->setPen(CrawnAngleLinePen);
return CrawnAngleLine;
}
void MainWindow::on_XrotationLineEdit_textChanged(const QString &text)
{
bool conversionOk;
int rotationAngle = text.toInt(&conversionOk);
if (conversionOk) {
QGraphicsItemGroup *diamondGroup = nullptr;
QList<QGraphicsItem *> items = scene->items();
for (QGraphicsItem *item : items) {
if (item->type() == QGraphicsItemGroup::Type) {
diamondGroup = dynamic_cast<QGraphicsItemGroup *>(item);
break;
}
}
if (diamondGroup) {
diamondGroup->setTransformOriginPoint
(diamondGroup->boundingRect().center());
diamondGroup->setRotation(rotationAngle);
}
}
}