Thread: linear question

  1. #16
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    ive already tried that method and yes it was way too slow. isnt there a formula for drawing lines? that would probably have what im looking for.

  2. #17
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  3. #18
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Here is another way of writing a solution to the problem. Use the parametric form of a line.

    Code:
    // Assuming you have startPos and endPos as the two
    // points in your line segment
    
    // Range of [0.0, 1.0]
    float t = 0.0f;
    point2d newPos = startPos + (endPos - startPos) * t;
    I hope you see what this code is doing. You start at the first point and move t units along the vector direction given by (endPos - startPos). t is a real number in the range of [0.0, 1.0]. It should be quite clear that plugging in 0 for t will yield your start point and 1 will yield the end point. So any fractional value will give you somewhere in between. 0.5 will give you the half way point. Hope this helps you. You can google parametric line form for more information if you need it.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #19
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    If you notice what Mr. Wizard posted it is essentially linear interpolation.

    value=value1+interp_value*(value2-value1);

    or

    finalpos=startpos+interp_value*(endpos-startpos);

    Note that a line though has 2 dimensions so a line is really a bilinear interpolation. You interpolate on X and interpolate on Y to achieve the final point.

    Bilinear interpolation essentially will find any point on that line between startpos and endpos using an interp factor of T or as Mr. Wizard was using it, a time factor. But it is still a basic interpolation between A and B.

    Graphics cards use this same type of function to filter pixels and create the blotches of color instead of texels as big as your fist when you get close to them. They bilinear interpolate component wise on the horizontal and vertical to achieve the final outcome.


    What Shakti posted is basically what you must use if you do not know the endpoint of the line. Like if you want to fire a bullet from point A at point B in 2D you would subtract the x and y components, normalize them, and use those normalized values as your xincrement and yincrement. To interpolate along these
    then you would use xincrement and yincrement in a bilinear interpolation to find where a point is a time T or you can integrate over time using xincrement and yincrement to achieve a smooth movement from startpos to endpos.


    However since a bilinear interpolation could be quite slow you can also simply find the slope of the line and use that as the T factor in the parametric form of the line.
    Last edited by VirtualAce; 12-02-2005 at 04:03 PM.

  5. #20
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    thanks for the replies, i had a question about what mr wizard said:

    Code:
    point2d newPos = startPos + (endPos - startPos) * t;
    is that supposed to be read as:

    Code:
    int newPos.x = startPos.x + (endPos.x - startPos.x) * t;
    int newPos.y = startPos.y + (endPos.y - startPos.y) * t;
    i realize that Bubba tried to explain, but ive never done bilinear interpolation so im kinda lost.

  6. #21
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    nevermind, i just read through his post again and understood it.

    heres a link to a few similar questions for anyone whos interested: http://www.cc.gatech.edu/gvu/multime...faq_lines.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linear Programming
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 12-20-2007, 09:53 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. linear search for structure (record) array
    By jereland in forum C Programming
    Replies: 3
    Last Post: 04-21-2004, 07:31 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM