A point is inside a triangle if it's to the left (right) of every side. You can calculate the cross products (just one component of it, actually) of a vector constructed of a point to be tested and one of the triangle vertices and the 3 vectors lying on the sides of the triangle (all in clock-wise or all in counter-clock-wise direction). See if the computed component of all 3 has the same sign (all 3 negative or all 3 positive). That'll tell you the point is in. Fast, no problems with precision, at least, if you use integers for the task.
You may stop further calculations for every point once you see it's to the wrong side of one of the triangle sides.
Sample code in C:
#include <stdio.h>
#define SCREEN_HEIGHT 22
#define SCREEN_WIDTH 78
// Simulated frame buffer
char Screen[SCREEN_HEIGHT][SCREEN_WIDTH];
void SetPixel(int x, int y, char color)
{
if ((x < 0) || (x >= SCREEN_WIDTH) ||
(y < 0) || (y >= SCREEN_HEIGHT))
return;
Screen[y][x] = color;
}
void Visualize(void)
{
int x, y;
for (y = 0; y < SCREEN_HEIGHT; y++)
{
for (x = 0; x < SCREEN_WIDTH; x++)
printf("%c", Screen[y][x]);
printf("\n");
}
}
typedef struct
{
int x, y;
} Point2D;
int main(void)
{
// triangle vertices
Point2D vx0 = { SCREEN_WIDTH / 2, SCREEN_HEIGHT / 7 };
Point2D vx1 = { SCREEN_WIDTH * 6 / 7, SCREEN_HEIGHT * 2 / 3 };
Point2D vx2 = { SCREEN_WIDTH / 7, SCREEN_HEIGHT * 6 / 7 };
// vectors lying on triangle sides
Point2D v0, v1, v2;
// current point coordinates
int x, y;
// calculate side vectors
v0.x = vx1.x - vx0.x;
v0.y = vx1.y - vx0.y;
v1.x = vx2.x - vx1.x;
v1.y = vx2.y - vx1.y;
v2.x = vx0.x - vx2.x;
v2.y = vx0.y - vx2.y;
// process all points
for (y = 0; y < SCREEN_HEIGHT; y++)
for (x = 0; x < SCREEN_WIDTH; x++)
{
int z1 = (x - vx0.x) * v0.y - (y - vx0.y) * v0.x;
int z2 = (x - vx1.x) * v1.y - (y - vx1.y) * v1.x;
int z3 = (x - vx2.x) * v2.y - (y - vx2.y) * v2.x;
if ((z1 * z2 > 0) && (z1 * z3 > 0))
SetPixel(x, y, '+'); // point is to the right (left) of all vectors
else
SetPixel(x, y, '-');
}
// draw triangle vertices
SetPixel(vx0.x, vx0.y, '0');
SetPixel(vx1.x, vx1.y, '1');
SetPixel(vx2.x, vx2.y, '2');
// visualize the result
Visualize();
return 0;
}
Output (ideone):
------------------------------------------------------------------------------
------------------------------------------------------------------------------
------------------------------------------------------------------------------
---------------------------------------0--------------------------------------
--------------------------------------++++------------------------------------
------------------------------------++++++++----------------------------------
----------------------------------+++++++++++++-------------------------------
--------------------------------+++++++++++++++++-----------------------------
------------------------------++++++++++++++++++++++--------------------------
----------------------------++++++++++++++++++++++++++------------------------
--------------------------+++++++++++++++++++++++++++++++---------------------
-------------------------++++++++++++++++++++++++++++++++++-------------------
-----------------------+++++++++++++++++++++++++++++++++++++++----------------
---------------------+++++++++++++++++++++++++++++++++++++++++++--------------
-------------------+++++++++++++++++++++++++++++++++++++++++++++++1-----------
-----------------++++++++++++++++++++++++++++++++++++-------------------------
---------------++++++++++++++++++++++++---------------------------------------
-------------++++++++++++-----------------------------------------------------
-----------2------------------------------------------------------------------
------------------------------------------------------------------------------
------------------------------------------------------------------------------
------------------------------------------------------------------------------