Posted 01 February 2015 - 12:07 AM
I'm looking for a way to draw a filled triangle, given any three points. I'll give you credit if I use it, don't worry. :)/>
local function getPoint( p1, p2, y ) --#p1, p2 are tables containing x,y. y is... well y
local a, b = p2.y-p1.y, p1.x-p2.x
local c = a*p1.x+b*p1.y
return (c-b*y)/a
end
local p1 = tPoints[ i ] --#tPoints a table of coordinates(x,y)
local p2 = tPoints[ i + 1 ] or tPoints[ 1 ]
local p3 = { x = maxx/2, y = maxy/2 }
for y = math.min( p1.y, p2.y, p3.y ), math.max( p1.y, p2.y, p3.y ), 0.1 do --#iterate from minimum y to maximum y
local a, b, c = getPoint( p1, p2, y ), getPoint( p2, p3, y ), getPoint( p3, p1, y ) --#get the intersections of all three lines on that x coord
--#here is problem section: I'm not sure how to figure out which lines I want to intersect...
end
local function getPoint( p1, p2, y ) --#p1, p2 are tables containing x,y. y is... well y
local a, b = p2.y-p1.y, p1.x-p2.x
local c = a*p1.x+b*p1.y
local x = (c-b*y)/a
if x > math.max( p1.x, p2.x ) or x < math.min( p1.x, p2.x ) then return nil end -- check if the intersection is outside the line segment.
return x
end
local p1 = tPoints[ i ] --#tPoints a table of coordinates(x,y)
local p2 = tPoints[ i + 1 ] or tPoints[ 1 ]
local p3 = { x = maxx/2, y = maxy/2 }
for y = math.min( p1.y, p2.y, p3.y ), math.max( p1.y, p2.y, p3.y ), 0.1 do --#iterate from minimum y to maximum y
local a, b, c = getPoint( p1, p2, y ), getPoint( p2, p3, y ), getPoint( p3, p1, y ) --#get the intersections of all three lines on that x coord
local points = {} -- I just do this because Im too lazy to have an if checking which one is nil.
table.insert( points, a )
table.insert( points, b )
table.insert( points, c )
if #points > 1 then -- make sure the number of intersections are greater than 1, just in case.
for x = math.min( unpack( points ) ), math.max( unpack( points ) ) do -- draw between the intersections.
-- do stuff
end
end
end