Checking pixels on a line
Checking pixels on a line
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.
Re: Checking pixels on a line
If it's a straight line, you can do something like this:
Warning: code is untested.
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
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
Re: Checking pixels on a line
Keep in mind that using getPixel constantly is probably (a lot) slower than pulling the whole image into a lua-table and accessing that.
- zorg
- Party member
- Posts: 3470
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Checking pixels on a line
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 rightPlu 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.
Me and my stuff True 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.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Checking pixels on a line
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.
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 2 guests