I have been working on a graphics library for some time now and have gotten to the point where I have to draw Bezier and line based fonts. Up to this point I am stuck with this:
The green lines are the Bezier paths, and the white part is what gets rendered.
The code I use for Beziers is here. The one for lines is here. For those who don't know that is Lua.
Path rendering (lines) : 32 - 39 The algorithm is as follows:
- Iterating from 0 to 1 at certain intervals
- calculating the x and y with this formula:
(1-index)^2*x1+2*(1-index)*index*x2+index^2*x3
Up to this point everything works fine. The green lines are generated using the path method.
The white part is rendered in a completely different way:
- I get the x coordinates of the Beziers and lines at a particular Y, the put them into a table.
- I iterate through the table and each time I encounter a point I change the value of state. In the same for loop is also check whether state is on. If it is, I draw a pixel to the screen.
To find the x values of a y, I use the getX method (line 46 in Bezier and line 31 in Line).
The code I use for the drawing itself is this one:
local xBuffer = {}
local state = false
for i=0,500 do
for k,v in pairs(beziers) do
a,b = v.getX(i)
if a then
xBuffer[round(a)] = 1
if b then
xBuffer[round(a)] = 1
end
end
end
for k,v in pairs(lines) do
a = v.getX(i)
if a then
xBuffer[round(a)] = 1
end
end
state = false
for x=0,600 do
if xBuffer[x] then
state = not state
end
if state then
love.graphics.points(x,i)
end
end
end
Quick explanation: for i,v in pairs iterates through the table given as an argument to pairs. love.graphics.points(x,y) sets a point at x,y.
Thanks in advance.