Results 1 to 7 of 7

Thread: Regarding Painting

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

    Default Regarding Painting

    There exists a line with the coordinates of the 2 end points known(say P and Q)

    For a shortened line the following steps were used

    1. Determine and slope(m) and intercept(c)
      m and c represent the standard notations in the equation of a line y=mx+c.
    2. Then P.x()+20(pixels)
    3. Determine relevant "y" on the same line using the equation y= mx+c where
      m and c are known from previous calculations.
    4. There is a serious problem in this approach because the slop is prone to "tangent" behavior i.e as the slope increases from 0 to 90 it increases and at 90 it becomes infinity.

    Please do help me calculate the coordinate of a point on the line such that the line is shortened.I should be able to draw a shortened line from the new position.


    Thank You.
    Last edited by archanasubodh; 6th August 2008 at 13:41. Reason: Bullet aligment

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Regarding Painting

    Try the vector approach:
    Qt Code:
    1. u.x = Q.x - P.x
    2. u.y = Q.y - P.y
    3. if( u.x != 0 ) {
    4. u *= 20 / u.x
    5. P += u
    6. }
    7. else {
    8. // impossible
    9. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Regarding Painting

    Hello

    I'm not sure how to approach this problem using vectors.Please do let me know in a little detailed manner.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Regarding Painting

    Which part you don't understand?

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

    Default Re: Regarding Painting

    How to use Vectors in my case?Is it to represent QPoints?
    How will it help me solve the tangent behavior?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Regarding Painting

    Quote Originally Posted by archanasubodh View Post
    How to use Vectors in my case?Is it to represent QPoints?
    I'm talking about mathematical vectors (like the "u" from the pseudocode snippet above).

    Quote Originally Posted by archanasubodh View Post
    How will it help me solve the tangent behavior?
    You can't avoid it, but computations with vector approach will be easier.

  7. #7
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Regarding Painting

    well of course if your line is vertical the slope is infinite and you cannot "shorten" it the way you're trying to do... You can check that just like jacek suggests.

    mathematically speaking a vector is a "difference" of two points or a "movement" to go from a point to another (purely graphical explanation here. my math teacher would strangle me if he were to read this )

    code-wise, a vector and a point are exactly the same thing : a bunch of coordinates. The only difference is the way you use them.

    proceed like this :

    1. compute the vector that directs your line by substracting the two extremities
    2. normalize it (i.e divide it by its length)
    3. multiply it by the desired length (here 20 pix)
    4. add it back to the starting point of your line to obtain the new interpolated point on the same line

    Qt Code:
    1. QPointF vec = end - start;
    2. vec /= sqrt(pow(vec.x(), 2), pow(vec.y(), 2));
    3. vec *= 20;
    4. QPoint interpolated = start + vec.toPoint();
    To copy to clipboard, switch view to plain text mode 
    The use of a QPointF here is only *recommended* for more precision. A less accurate but faster version would be :
    Qt Code:
    1. QPoint vec = end - start;
    2. vec /= vec.manhattanLength();
    3. vec *= 20;
    4. QPoint interpolated = start + vec;
    To copy to clipboard, switch view to plain text mode 

    edit : the sample code uses Qt but the method described here is not dependent on Qt (I'd have written this in plain C++ but I didn't feel like declaring my own Point class before )
    Last edited by fullmetalcoder; 8th August 2008 at 18:28.
    Current Qt projects : QCodeEdit, RotiDeCode

Similar Threads

  1. Qt 4.4.0-rc1 painting performance
    By thomaspu in forum Qt Programming
    Replies: 0
    Last Post: 9th April 2008, 18:13
  2. Slow painting in QGraphicsView
    By JonathanForQT4 in forum Qt Programming
    Replies: 12
    Last Post: 16th July 2007, 09:54
  3. Painting Faded away when alt key pressed,
    By rajeshs in forum Qt Programming
    Replies: 2
    Last Post: 4th July 2007, 09:45
  4. Painting in QCanvasView
    By JimBrown in forum Qt Programming
    Replies: 1
    Last Post: 11th May 2007, 22:29
  5. About painting
    By Pang in forum Qt Programming
    Replies: 3
    Last Post: 28th March 2007, 17:21

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.