nottelling
nottelling

Reputation: 11

Rotating QGraphicsLineItems around a circle and not a point

So, I have a scene with two graphics circles, and a custom line item. I created two line items, where a boolean “foo” is passed in to determine how to separate the lines, as I want the circles connected via two lines like ||.

When I move the destination circle around the source, I want the lines to stay the same distance apart, but right now they move closer to each other as they approach y=x and flip on that line.

I have tried to implement a custom adjust function which gets called in the line item’s init:

def adjust(self, foo: bool):
   self.prepareGeometryChange()
   adjust = 4
   if not foo:
       adjust *= -1
   self.setLine(
      QtCore.QLineF(
           self.source.pos().x()+adjust,
           self.source.pos().y()+adjust,
           self.dest.pos().x()+adjust,
           self.dest.pos().y()+adjust,
       )
    )

What can I do to make them swivel around the circles instead of around a single point?

Upvotes: 0

Views: 31

Answers (1)

nottelling
nottelling

Reputation: 11

I finally came back to this after months with an answer:

def adjust(self, foo: bool):
   self.prepareGeometryChange()

   delta = QtCore.QPointF(self.source.pos() - self.dest.pos())
   length = delta.manhattanLength()
   if length == 0.0:
      return self.setLine(
         QtCore.QLineF(
              self.source.pos(),
              self.dest.pos()
        )
     )

   direction = QtCore.QPointF(delta.x()/length, delta.y()/length)
   perpendicular = QtCore.QPointF(-direction.y(), direction.x())
   line_offset = 5

   sign = -1 if foo else 1

   x_adjust = (sign*perpendicular.x()) * (sign*line_offset)
   y_adjust = (sign*perpendicular.y()) * (sign*line_offset)

   self.setLine(
      QtCore.QLineF(
           self.source.pos().x()-x_adjust,
           self.source.pos().y()-y_adjust,
           self.dest.pos().x()-x_adjust,
           self.dest.pos().y()-y_adjust,
       )
    )

Upvotes: 0

Related Questions