[Python] pyqtgraph (pyqt6) line representation disparity

Discussão em 'Python' iniciado por Stack, Março 18, 2025.

  1. Stack

    Stack Membro Participativo

    Here you have two images of my attempt at representing two parallel lines defined by three points. The extremes of these two lines have been calculated with the same entry data (the points' coordinates) and the exact same functions (the outcome data, the coordinates of the extremes of the lines, is exactly the same in both images).

    [​IMG]:

    [​IMG]

    The first image belongs to an isolated test of the calculations and the second to those same calculations integrated in my code (as stated, all the data is the same in both).

    The biggest difference in the two codes (or, at least, the one that I consider more meaningful to this case) is:

    In my code, the lines are Graphic Line Items with a cosmetic pen an the dots are Graphic Elliptic Items with a Cosmetic Flag (those "points" are well positioned according to their data and the axis). On the other code, both the lines and the points are plots.

    As you can see, in my integration of those calculations (second image) the projected parallel line that should contain the isolated point is visually positioned far from where it should be (even though the data is correct).

    Here you have the functions I've used to calculate the lines positions:

    def orthogonal_projection(P, A, B):
    """Compute the orthogonal projection of P onto the infinite line AB."""
    x, y = P
    x1, y1 = A
    x2, y2 = B
    dx, dy = x2 - x1, y2 - y1
    denom = dx * dx + dy * dy
    if denom == 0:
    return A # Degenerate case: A and B are the same point
    t = ((x - x1) * dx + (y - y1) * dy) / denom
    return (x1 + t * dx, y1 + t * dy)

    def adjusted_segment(A, B, P):
    """Adjusts the reference segment so that one of its endpoints is the orthogonal projection of P if needed."""
    P_proj = orthogonal_projection(P, A, B)
    if min(A[0], B[0]) <= P_proj[0] <= max(A[0], B[0]) and min(A[1], B[1]) <= P_proj[1] <= max(A[1], B[1]):
    return A, B # Projection falls within segment, no need to extend
    if np.linalg.norm(np.array(A) - np.array(P_proj)) > np.linalg.norm(np.array(B) - np.array(P_proj)):
    return A, P_proj # Extend A->P'
    else:
    return P_proj, B # Extend B->P'

    def parallel_segment(A, B, P):
    """Computes the parallel segment passing through P where endpoints are the orthogonal projections."""
    A_proj, B_proj = adjusted_segment(A, B, P)
    offset_x, offset_y = P[0] - orthogonal_projection(P, A, B)[0], P[1] - orthogonal_projection(P, A, B)[1]
    A_prime = (A_proj[0] + offset_x, A_proj[1] + offset_y)
    B_prime = (B_proj[0] + offset_x, B_proj[1] + offset_y)
    return A_prime, B_prime, A_proj, B_proj


    My question is: what could be messing the representation of the projected parallel line in my chart?

    I've been looking at this for two days and I cannot wrap my head about why the calculation of the projected line might be messing with the way the viewport handles it (because the data is the same as the other chart where those calculations are isolated).

    Another hint is: the more I distort the view, the more random the positioning of the line seems to be (but not the point, the point and the reference line are always well positioned).

    Any idea would be highly appreciated.

    Cheers.

    Continue reading...

Compartilhe esta Página