help, draw quad point fill - without polygon

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
luislasonbra
Citizen
Posts: 60
Joined: Sun Jun 24, 2012 1:57 pm

help, draw quad point fill - without polygon

Post by luislasonbra »

Hi, I need help with fill four points, without the use of "love.graphics.polygon".

the code that I have works in the following way.
I get the top line and the bottom line and I'm going to trace lines vertically, from the top line and the bottom line, but the way I use to get the points index of the bottom line is not giving me the desired results and produces several graphic problems , that until now I have not been able to correct.

Here is the code

Code: Select all

io.stdout:setvbuf("no");

-- Returns a table of vectors representing all of the points between the two supplied points 
function pointsBetween(first, second)
	-- http://en.wikipedia.org/wiki/Bresenham's_line_algorithm
	local x0 = first.x;
	local y0 = first.y;
	local x1 = second.x;
	local y1 = second.y;

	local points = {};

	local steep = false;
	if math.abs(y1 - y0) > math.abs(x1 - x0) then steep = true; end

	if steep then
		x0, y0 = y0, x0;
		x1, y1 = y1, x1;
	end

	if x0 > x1 then
		x0, x1 = x1, x0;
		y0, y1 = y1, y0;
	end

	local deltax = (x1 - x0);
	local deltay = math.abs(y1 - y0);
	local err = deltax / 2;
	local ystep = 0;
	local y = y0;

	if y0 < y1 then 
		ystep = 1;
	else 
		ystep = -1;
	end

	for x = x0, x1 do
		if steep then
			table.insert(points, {x=y, y=x});
		else
			table.insert(points, {x=x, y=y});
		end

		err = err - deltay;

		if err < 0 then
			y = y + ystep;
			err = err + deltax;
		end
	end

	return points;
end

local log = "";

local lg = love.graphics;
local sw = lg.getWidth();
local sh = lg.getHeight();

local halfw = sw/2;
local halfh = sh/2;

-- http://www.sunshine2k.de/coding/java/TriangleRasterization/TriangleRasterization.html
--- Get the cross product of two vectors.
-- @tparam vec2 a Left hand operand
-- @tparam vec2 b Right hand operand
-- @treturn number magnitude
function crossProduct(a, b) return a.x * b.y - a.y * b.x; end
function draw_triangle(vt1,vt2,vt3)
	-- get the bounding box of the triangle
	local maxX = math.max(vt1.x, math.max(vt2.x, vt3.x));
	local minX = math.min(vt1.x, math.min(vt2.x, vt3.x));
	local maxY = math.max(vt1.y, math.max(vt2.y, vt3.y));
	local minY = math.min(vt1.y, math.min(vt2.y, vt3.y));
	
	-- spanning vectors of edge (v1,v2) and (v1,v3)
	local vs1 = { x = (vt2.x - vt1.x), y = (vt2.y - vt1.y) };
	local vs2 = { x = (vt3.x - vt1.x), y = (vt3.y - vt1.y) };
	
	for x = minX, maxX do
		for y = minY, maxY do
			local q = { x = (x - vt1.x), y = (y - vt1.y) };
			local s = crossProduct(q, vs2) / crossProduct(vs1, vs2);
			local t = crossProduct(vs1, q) / crossProduct(vs1, vs2);

			if (s >= 0) and (t >= 0) and (s + t <= 1) then -- inside triangle
				-- drawPixel(x, y);
				lg.rectangle("fill", x, y, 1, 1);
			end
		end
	end
end

function dist_two_point(a, b)
	local dx = math.abs(b.x - a.x);
	local dy = math.abs(b.y - a.y);
	return math.sqrt(dx*dx+dy*dy);
end

function draw_quad(p1, p2, p3, p4)
	local oTopLine = pointsBetween(p1, p2);
	-- local oLeftLine = pointsBetween(p1, p3);
	-- local oRightLine = pointsBetween(p2, p4);
	local oBottomLine = pointsBetween(p3, p4);
	
	local tem = oTopLine;
	if #oTopLine < #oBottomLine then
		tem = oTopLine;
		oTopLine = oBottomLine;
		oBottomLine = tem;
	end
	
	local f = (#oBottomLine / #oTopLine)
	for i = 1, #oTopLine do
		local j = math.floor(i * f);
		j = j == 0 and 1 or j >= #oBottomLine and #oBottomLine or j;
		lg.line(oTopLine[i].x, oTopLine[i].y, oBottomLine[j].x, oBottomLine[j].y);
		
		log=
			"j: "..tostring(j)..", #oBottomLine: "..#oBottomLine..
			", i: "..tostring(i)..", #oTopLine: "..#oTopLine;
	end

	-- lines top
	lg.setColor({255,0,0,255});
	lg.line({ p1.x, p1.y, p2.x, p2.y });
	lg.setColor({255,255,255,255});

	-- lines bottom
	lg.setColor({255,0,0,255});
	lg.line({ p3.x, p3.y, p4.x, p4.y });
	lg.setColor({255,255,255,255});
	
	-- lines right
	lg.setColor({255,0,0,255});
	lg.line({ p2.x, p2.y, p4.x, p4.y });
	lg.setColor({255,255,255,255});
	
	-- lines left
	lg.line(p1.x,p1.y,p3.x,p3.y);
end

function love.draw()
	local mx, my = love.mouse.getPosition();

	local xpos=200;
	local ypos=200;
	draw_quad(
		{x=xpos,y=ypos}, 			-- top left
		{x=mx,y=my}, 				-- top right
		{x=xpos,y=ypos+100}, 		-- bottom left
		{x=xpos+100,y=ypos+150}  	-- bottom right
	);

	local fps = love.timer.getFPS();
	lg.setColor({100,255,100,255});
	lg.print("fps: " .. tostring(fps), 10, 10);
	lg.setColor({255,255,255,255});
	
	-- draw log
	lg.setColor({100,255,100,255});
	lg.print("log: " .. tostring(log), 10, 30);
	lg.setColor({255,255,255,255});
end
Here I leave some images of the problem
Captura de pantalla 2018-04-15 a la(s) 12.02.28 a. m..png
Captura de pantalla 2018-04-15 a la(s) 12.02.28 a. m..png (268.94 KiB) Viewed 1758 times
Captura de pantalla 2018-04-15 a la(s) 12.02.44 a. m..png
Captura de pantalla 2018-04-15 a la(s) 12.02.44 a. m..png (246.51 KiB) Viewed 1758 times
I hope you can help me

translated by Google Translation
Post Reply

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 59 guests