Drehe die Linie um den Mittelpunkt mit zwei Eckpunkten

Ich habe versucht, ein paar Linien um 90 Grad zu drehen (die zusammen eine Polylinie bilden). Jede Zeile enthält zwei Eckpunkte, z. B. (x1, y1) und (x2, y2). Was ich gerade versuche, ist, mich um den Mittelpunkt der Linie zu drehen, gegebene Mittelpunkte | x1 - x2 | und | y1 - y2 |. Aus irgendeinem Grund (ich bin nicht sehr mathematisch versiert) kann ich die Linien nicht richtig drehen.

Könnte jemand überprüfen, ob die Mathematik hier korrekt ist? Ich denke jedoch, dass es richtig sein könnte, wenn ich die Scheitelpunkte der Linie auf die neuen gedrehten Scheitelpunkte setze. Die nächste Linie greift möglicherweise nicht nach dem neuen (x2, y2) Scheitelpunkt der vorherigen Linie, wodurch die Linien falsch gedreht werden .

Folgendes habe ich geschrieben:

def rotate_lines(self, deg=-90):
    # Convert from degrees to radians
    theta = math.radians(deg)

    for pl in self.polylines:
        self.curr_pl = pl
        for line in pl.lines:
            # Get the vertices of the line
            # (px, py) = first vertex
            # (ox, oy) = second vertex
            px, ox = line.get_xdata()
            py, oy = line.get_ydata()

            # Get the center of the line
            cx = math.fabs(px-ox)
            cy = math.fabs(py-oy)

            # Rotate line around center point
            p1x = cx - ((px-cx) * math.cos(theta)) - ((py-cy) * math.sin(theta))
            p1y = cy - ((px-cx) * math.sin(theta)) + ((py-cy) * math.cos(theta))

            p2x = cx - ((ox-cx) * math.cos(theta)) - ((oy-cy) * math.sin(theta))
            p2y = cy - ((ox-cx) * math.sin(theta)) + ((oy-cy) * math.cos(theta))

            self.curr_pl.set_line(line, [p1x, p2x], [p1y, p2y])

Antworten auf die Frage(2)

Ihre Antwort auf die Frage