# Rotated line item in a scene along its center point

• Hello everyone.
First of all thanks a lot for helping me and reading this post.

I would like to rotate a line item in a scene along its center point.
I have tried this:

QPointF originPtoTransform;
originPtoTransform.setX(XEnd-((XEnd-XStart)/2)); //Set the oirigin in line center point
originPtoTransform.setY(YEnd-((YEnd-YStart)/2));
line_rotated->setTransformOriginPoint(0,0);
line_rotated->setRotation(angle);

The line is rotated along a unknow point... not along its center point.
Any ideas to do that¿

Thanks a lot!!

• Hi
line_rotated->setTransformOriginPoint( line_rotated->boundingRect().center());

• @mrjj Yes!! That works!!!
how can I set the orientation? Clockwise or counterclockwise to do rotation?

Thanks again my friend

• @AlvaroS said:
Super :)

To go the other way cant u just make it negative ?
setRotation( -angle);

• @AlvaroS negative value maybe?

• @mrjj Yes thanks!!!! I mark as solved :)))

• @mrjj just a question my friend.
I realize that If I rotate the line, the x1,x2,y1,y2 parameters of line do not change. Is it true? Maybe I have to calculate and then setline?

• @AlvaroS
I have not tested it
but I would assume you apply a transformation
to the Line so the points stay the same
but its projected to scene as rotated.

• @mrjj Yes It works like that. So i try to calculate the point rotated and then setLine!!

• @mrjj Good morning friend.
I trie to use setLine like that:

line_rotated->setTransformOriginPoint(line_rotated->boundingRect().center()); //Rotated line along center line.
line_rotated->setRotation(-ui->Degrees_to_Rotate->text().toDouble()); //Set counterclockwise rotation in degrees (counterclockwise=negative)
QLineF newline= line_rotated->line();
line_rotated->setLine(newline);
line_rotated ->setFlag(QGraphicsItem::ItemIsSelectable, true); // Every items can be selected
line_rotated->setSelected(true);
line_rotated ->setCursor(Qt::PointingHandCursor); // When the cursor is on a line item it change to a hand cursor instead of a tipical arrow.

In this code, line is rotated in the scene but its point does not change...

So tried this next code:

line_rotated->setTransformOriginPoint(line_rotated->boundingRect().center()); //Rotated line along center line.
line_rotated->setRotation(-ui->Degrees_to_Rotate->text().toDouble()); //Set counterclockwise rotation in degrees (counterclockwise=negative)
QLineF newline= line_rotated->line();
newline.setAngle(0); //To change the line points
line_rotated->setLine(newline);
line_rotated ->setFlag(QGraphicsItem::ItemIsSelectable, true); // Every items can be selected
line_rotated->setSelected(true);
line_rotated ->setCursor(Qt::PointingHandCursor); // When the cursor is on a line item it change to a hand cursor instead of a tipical arrow.

So in this case, the line change its point and is rotated, but it is rotated along its second point, not along its center point...

Could you help me to do that using setLine¿

Thanks a lot!

• @AlvaroS said:

Hi
The docs says it change second point
so its normal for it to use first point as center.
I dont think u can change that.
http://doc.qt.io/qt-5/qlinef.html#setAngle

• @mrjj So I can not use setLine for doing that. It is rare that with

line_rotated->setTransformOriginPoint(line_rotated->boundingRect().center()); //Rotated line along center line.
line_rotated->setRotation(-ui->Degrees_to_Rotate->text().toDouble()); //Set counterclockwise rotation in degrees (counterclockwise=negative)
```

The line rotated how I want but the points line does not change..

• Well if you want to rotate the line around center and rotate the actual points then
you have to calculate the x,y yourself using the center as base.

POINT rotate_point(float cx,float cy,float angle,POINT p)
{
float s = sin(angle);
float c = cos(angle);

// translate point back to origin:
p.x -= cx;
p.y -= cy;

// rotate point
float xnew = p.x * c - p.y * s;
float ynew = p.x * s + p.y * c;

// translate point back:
p.x = xnew + cx;
p.y = ynew + cy;
return p;
}

Disclaimer: There might be a way to apply the rotate transformation and then
get modified points in some more easy way but I dont know it.

• @mrjj Yes!! Now I got it!
I just had to rotate point start and point end along center point using these equations. so now it works!!
Thanks a lot!

• @AlvaroS
Super. Good work :)
If possible please mark as solved.

• @mrjj Yes! It is marked as solved :)!! Thanks again!