Click here to Skip to main content
15,844,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C++
void MainWindow::on_drawDiamondbtn_clicked()
{
     if (isDimond_Drawn)
        {
            //  *****  8Facet  *****

            // Draw a circle in the center of the QGraphicsView
            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);

            // Set the circle's properties
            int circleRadius = diaMMtoPX;  // Set your desired radius

            QGraphicsItemGroup *diamondGroup = new QGraphicsItemGroup;

            QGraphicsEllipseItem *circle = new QGraphicsEllipseItem
                 (centerX - circleRadius, centerY - circleRadius, 2 * 
                  circleRadius, 2 * circleRadius);
            circle->setPen(QPen(Qt::green));  // Set the pen color
            scene->addItem(circle);
            diamondGroup->addToGroup(circle);

            // Set the button color to green
            ui->drawDiamondbtn->setStyleSheet
                      ("background-color: green;");

            // Draw lines inside the circle
            QPen linePen(Qt::green);  // Set the line color
            linePen.setWidth(1);

            int lineLength = TablePX;  // Set your desired line length
            int numLines = 8;          // Number of lines to draw

            // Declare variables for line coordinates
            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;  // Use M_PI for Pi
                double startX = centerX + lineLength * qCos(angle);
                double startY = centerY + lineLength * qSin(angle);
                double endX = centerX - lineLength * qCos(angle);
                double endY = centerY - lineLength * qSin(angle);

                // Assign values to variables based on line index
                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;
                }

                //            QGraphicsLineItem *line = 
                // new QGraphicsLineItem(startX, startY, endX, endY);
                //            line->setPen(linePen);
                //            scene->addItem(line);
            }

            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 {
            // Remove the circle from the scene
            QList<QGraphicsItem *> items = scene->items();
            for (QGraphicsItem *item : items) {
                if (item->type() == QGraphicsEllipseItem::Type || 
                    item->type() == QGraphicsLineItem::Type) {
                    scene->removeItem(item);
                    delete item;
                }
            }
            // Set the button color back to red
            //ui->circlebtn->setStyleSheet("background-color: red;");
            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)
{
    // Draw a CrawnAngle line degrees above the circle's circumference
    QPen CrawnAngleLinePen(Qt::green);  // Set the CrawnAngle line color
    CrawnAngleLinePen.setWidth(1);

    // Calculate the angle 5 degrees above the circle's circumference
    double angleToCircumference = qAtan2(startY - centerY, startX - centerX);
    double angleForPinkLine = angleToCircumference - 
    qDegreesToRadians(static_cast<double>(0.01)); // Convert degrees 
                                                        // to radians

    // Calculate the line's end point on the circle's circumference
    double endXOnCircumference = centerX + 
                      circleRadius * qCos(angleForPinkLine);
    double endYOnCircumference = centerY + circleRadius * 
                      qSin(angleForPinkLine);

    QGraphicsLineItem *CrawnAngleLine = new QGraphicsLineItem
        (startX, startY, endXOnCircumference, endYOnCircumference);
    CrawnAngleLine->setPen(CrawnAngleLinePen);
    //scene->addItem(CrawnAngleLine);
    return CrawnAngleLine;
}	

void MainWindow::on_XrotationLineEdit_textChanged(const QString &text)
{
    bool conversionOk;
    int rotationAngle = text.toInt(&conversionOk);

    if (conversionOk) {
        // Find the diamond group in the scene
        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) {
            // Apply rotation to the diamond group
            diamondGroup->setTransformOriginPoint
                     (diamondGroup->boundingRect().center());
            diamondGroup->setRotation(rotationAngle);
        }
    }
}
Posted
Updated 27-Oct-23 8:29am
v7

That is not a 2D problem, but a 3D one.
You simply cannot rotate like this in 2 dimensions.

You will need to define the diamond shape in 3D, and rotate it along one axis about the model origin, which should e.g. be the barycenter of the model.
Your 2D image will then be a projection of the rotated model onto a viewing plane defined by the virtual camera position.
Whether you only calculate in 3D and actually draw in 2D like you're doing here, or use e.g. OpenGL to
actually render the whole thing in 3D is not that important; the important bit is you cannot solve this
in 2 dimensions.
 
Share this answer
 
Comments
Richard MacCutchan 26-Oct-23 3:45am    
Please delete your duplicate answer.
Sebastian Porstendorfer 26-Oct-23 3:48am    
Done. Unfortunately, when I first submitted nothing showed up anymore, so I could not edit the earlier one.
Richard MacCutchan 26-Oct-23 3:52am    
For some strange reason your messages were picked up by the spam filter and went to the moderation queue, which is why they did not appear here. Once I had accepted them as valid messages they were correctly posted. There does not seem to be anything in either message that could be mistaken for spam, so maybe a false positive.
You are drawing the shape in two dimensions so with the data you have it is impossible. You have to compute the vertexes of facets of the diamond in three dimensions for this to be possible. Then you need to use a transformation matrix to rotate the vertexes and then project that shape onto a plane for display. This is actually fairly complex and much easier to do with a 3D graphics library like Direct3D, OpenGL, or Vulkan. With a 3D library you feed it your points and the transformation matrix and the library will do all of the rendering calculations for you.

This reminds that I made something like this many years ago and it generated the points in POVray format that I used to render the images with the ray-tracer. My experience lately is with OpenGL and this inspired me. I'm going to see if I can port that old program. I gave it a 2D description of two adjacent edges of the diamond and then it rotated that around the Y axis to generate the full shape.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900