Results 1 to 6 of 6

Thread: Shortenning a line

  1. #1
    Join Date
    Feb 2008
    Posts
    19
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Shortenning a line

    Hello

    I have 2 QPoints(P,Q) from which i construct a straight line(inclined/horizontal/vertical).
    In some scenarios the length of the line should be reduced.Please do suggest a method to shift the point P so that i can reduce the length of the line.

    1. We have P(x1,y1) Q(x2,y2)
    2. Currently we use the formula, y = mx + c and get the values of y(slope) and c intercept.
    3. Then assuming that the new point is 10 pixels horizontally from one end of the line.
    4. Determine the new y` which also lies on the line y=mx+c.
    5. We get the new P`(p.x()+10,y`).


    This method does not work for all cases.
    Please do let me know a good way to reduce the length of the line.

    Thanks in advance.

  2. #2
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Shortenning a line

    You are right, this method is not working for all the cases. The problem is with step #3. What you should do first is try to determine the kind of your line, it means to figure whether P is to the left from Q, or to the right, and is P above Q or below.

    For example, let's say we have P(p1, p2) and Q(q1, q2).
    Qt Code:
    1. Case 1:
    2. p1 < q1 and p2 < q2
    To copy to clipboard, switch view to plain text mode 
    Your line will look like this

    Qt Code:
    1. . * Q
    2. /
    3. /
    4. /
    5. * P
    To copy to clipboard, switch view to plain text mode 

    It means that your new point P` will be above P. That;s why you take P.x() + some_value and then calculate new value for P.y() from that equation.

    Qt Code:
    1. Case 2:
    2. p1 > q1 and p2 < q2
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. Q *
    2. \
    3. \
    4. \
    5. * P
    To copy to clipboard, switch view to plain text mode 
    in this case you'll have to take P.x() - some_value.


    And so on, and so forth...

    I hope you get the idea.
    I'm a rebel in the S.D.G.

  3. #3
    Join Date
    Feb 2008
    Posts
    19
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Shortenning a line

    Hi

    Consider the following case:
    P(135,191) Q(140,59)
    The slope = -26.4
    c = 3755

    Now calculating P`y= -26.4(135+5) +3755
    will give you P`y=-59, which happens to be in the opposite direction.

    PS:We paint on a canvas where the top left corner is considered as the origin.


    Thank in advance.

  4. #4
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Shortenning a line

    Yes, you are right, my last post was written taking into consideration that the points are positioned on regular co-ordinates, but not on screen. For screen these coordinates should be flipped.

    Here's your case
    Quote Originally Posted by archanasubodh View Post
    Hi
    P(135,191) Q(140,59)
    The slope = -26.4
    c = 3755

    Now calculating P`y= -26.4(135+5) +3755
    will give you P`y=-59, which happens to be in the opposite direction.

    Thank in advance.
    Let's calculate P`y which is P`y=-26.4*(135+5)+3755=-26.4*140+3755=-3696+3755=59 but not -59. So it works.
    I'm a rebel in the S.D.G.

  5. #5
    Join Date
    Feb 2008
    Posts
    19
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Shortenning a line

    Consider this example,where i get a line in the opposite direction.
    P(400,267) and Q(401,119)
    Here calculating the slope "m",we get -148.
    The y intercept "c" = 59467.
    Then deducing P`y=-148(403) + 59467
    59467 - 59644
    P`y = -177.

    Hence i get a line in the opposite direction as that of the original.

    PS:With the variation of the angle,the length of reduction also changes.
    For instance,
    When the line is inclined at an angle less than 60 the spacing is less, for lines whose inclination > 60 to 90 the spacing increases and for angles close to 90 or 270 and line goes in the opposite direction stretching to infinity.
    The behavior is similar to that of the tangent of an angle.
    Please do give me a convincing solution.


    Thanks in advance.

  6. #6
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Shortenning a line

    Again, in this case your Q point is above P and to the right from P (on a screen)

    and when you do P`y=-148(403) + 59467 you, in fact, are not shortenning it. You are making it longer, since you are taking a new point with x=403 which will be above Q and to the right from Q.

    The first thing you should do before applying your shortening algorithm is find out the orientation of your line.
    I'm a rebel in the S.D.G.

Similar Threads

  1. QTcpSocket exception.
    By Fastman in forum Qt Programming
    Replies: 9
    Last Post: 29th January 2008, 13:51
  2. Some very weird compilation warnings
    By MarkoSan in forum Qt Programming
    Replies: 21
    Last Post: 23rd January 2008, 16:48
  3. Qwizard crashed when created in a slot
    By joshlareau in forum Qt Programming
    Replies: 9
    Last Post: 15th January 2008, 09:16
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  5. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.