I wrote this code for someone ... can't remember who though.
function FilledTriangle(x0,y0,x1,y1,x2,y2)
local tx as integer
local ty as integer
local NewX as integer
` Sort the points so that they are in order of their y coords
if y1 < y0
ty=y1 : y1=y0 : y0=ty
tx=x1 : x1=x0 : x0=tx
endif
if y2 < y0
ty=y2 : y2=y0 : y0=ty
tx=x2 : x2=x0 : x0=ty
endif
if y2 < y1
ty=y2 : y2=y1 : y1=ty
tx=x2 : x2=x1 : x1=tx
endif
if y0 = y1
FlatTopTriangle(x0,y0,x1,y1,x2,y2)
else
if y1 = y2
FlatBottomTriangle(x0,y0,x1,y1,x2,y2)
else
NewX=x0+( ( ((y1+0.0)-y0) * ((x2+0.0)-x0) ) / ((y2+0.0)-y0) )+0.5
FlatBottomTriangle(x0,y0,x1,y1,NewX,y1)
FlatTopTriangle(x1,y1,NewX,y1,x2,y2)
endif
endif
endfunction
function FlatBottomTriangle(x0,y0,x1,y1,x2,y2)
local dxy_left as float
local dxy_right as float
local xs as float
local xe as float
local y as integer
if x2 > x1 then tx=x1 : x1=x2 : x2=tx
dxy_left=((x2+0.0)-x0)/((y2+0.0)-y0)
dxy_right=((x1+0.0)-x0)/((y1+0.0)-y0)
xs=x0+0.5
xe=x0+1.0
for y=y0 to y1
box xs,y,xe,y+1
xs=xs+dxy_left
xe=xe+dxy_right
next y
endfunction
function FlatTopTriangle(x0,y0,x1,y1,x2,y2)
local dxy_left as float
local dxy_right as float
local xs as float
local xe as float
local y as integer
if x1 < x0 then tx=x0 : x0=x1 : x1=tx
dxy_left=((x2+0.0)-x0)/((y2+0.0)-y0)
dxy_right=((x2+0.0)-x1)/((y2+0.0)-y1)
xs=x0+0.5
xe=x1+1.0
for y=y0 to y2
box xs,y,xe,y+1
xs=xs+dxy_left
xe=xe+dxy_right
next y
endfunction
There are 3 functions:
1 - Draw triangles with flat top
2 - Draw triangle with flat bottom
3 - Use the above two routines to draw a general triangle
*** Coming soon - Network Plug-in - Check my site for info ***
For free Plug-ins, source and the Interface library for Visual C++ 6, .NET and now for Dev-C++
http://www.matrix1.demon.co.uk