Checking pixels on a line

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
fmra
Prole
Posts: 11
Joined: Fri Nov 29, 2013 9:39 pm

Checking pixels on a line

Post by fmra »

I'm working on a pathfinding routine and am trying to do something similar to raycasting to to have an agent look ahead to the goal and see if it is clear or obstructed. I've successfully been using a matched imagedata to check for object collision using img:getPixel. I'm wondering if there is a way to ":getPixel" along a line (without drawing the line) to get the color data for each point along the line.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Checking pixels on a line

Post by davisdude »

If it's a straight line, you can do something like this:

Code: Select all

function getLine( x1, y1, x2, y2 ) -- Get two points that you know are on the line- your start and end points. 
    -- ( x1, y1 ) should always be the start.
    -- y = mx + b
    local m = ( y1 - y2 ) / ( x1 - x2 )
    local b = y1 - m * x1
    
    local function sign( x )
        return ( x > 0 and 1 ) or ( x < 0 and -1 ) or 0
    end

    local addx = sign( x1 - x2 )
    for x = x1, x2, addx do
        local y = m * x + b
        -- Put code here. Use 'y' and 'x' as the values.
    end
end
Warning: code is untested.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Checking pixels on a line

Post by Plu »

Keep in mind that using getPixel constantly is probably (a lot) slower than pulling the whole image into a lua-table and accessing that.
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Checking pixels on a line

Post by zorg »

Plu wrote:Keep in mind that using getPixel constantly is probably (a lot) slower than pulling the whole image into a lua-table and accessing that.
On the other hand, tables have some overhead in memory, while imagedata is (to my knowledge) just a simple c array holding byte quadruplets for the pixel colors; so that should consume less memory as a tradeoff... if i am right :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Checking pixels on a line

Post by Robin »

In general, yes, but LuaJIT can make space optimizations like that (or better) when you use Lua tables, without any extra effort. And recent LÖVE builds are linked to LuaJIT by default, so...
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: No registered users and 97 guests