// inthetriangle (c) rmrubin@darkertechnologies.com 2007

#include <stdio.h>

int inthetriangle(float ax, float ay, float bx, float by, \
    float cx, float cy, float px, float py); 

int main() {

// text splash
	
    printf("\n\
 inthetriangle-0.2   2007.09.30\n\
 rmrubin@darkertechnologies.com\n");

// triangle and test point x y coordinates
	    
    float ax;
    float ay;
    float bx;
    float by;
    float cx;
    float cy;
    float px;
    float py;

    printf("\n enter triangle pt a\n x: ");
    scanf("%f", &ax);

    printf(" y: ");
    scanf("%f", &ay);

    printf("\n enter triangle pt b\n x: ");
    scanf("%f", &bx);

    printf(" y: ");
    scanf("%f", &by);
			 
    printf("\n enter triangle pt c\n x: ");
    scanf("%f", &cx);
	
    printf(" y: ");
    scanf("%f", &cy);	
	 
    printf("\n enter test pt\n x: ");
    scanf("%f", &px);
	
    printf(" y: ");
    scanf("%f", &py);

    int result = inthetriangle(ax,ay,bx,by,cx,cy,px,py);
    
    switch (result) {
	case 0: printf("\n  =(  test point is NOT within triangle.\n\n"); break;
	case 1: printf("\n  =D  test point is INSIDE the triangle.\n\n"); break;
	case 2: printf("\n  >[  this is a line segment, not a triangle.\n\n"); break;
	case 3: printf("\n  >[  please enter 3 unique points.\n\n"); break;
	case 5: printf("\n  8D  test point is ON the triangle.\n\n"); break;
    }
    
    return(1);
}

int inthetriangle(float ax, float ay, float bx, float by, \
	float cx, float cy, float px, float py)
{

// test for unique points
	
    if ((ax == bx && ay == by)||(ax == cx && ay == cy)||(bx == cx && by == cy)) return(3);
    
    float xmin;
    float xmax;
    
// sort triangle points by y, point c highest.
 
    if (ay > by) {
	xmax = by;
	xmin = bx;
	by = ay;
	bx = ax;
	ay = xmax;
	ax = xmin;
    }

    if (by > cy) {
	xmax = cy;
	xmin = cx;
	cy = by;
	cx = bx;
	by = xmax;
	bx = xmin;
    }
   
    if (ay > by) {
	xmax = by;
	xmin = bx;
	by = ay;
	bx = ax;
	ay = xmax;
	ax = xmin;
    }

// print sorted points    

    printf("\n");
    printf(" [ %f %f ] triangle pt a\n", ax, ay);
    printf(" [ %f %f ] triangle pt b\n", bx, by);
    printf(" [ %f %f ] triangle pt c\n", cx, cy);
    printf(" [ %f %f ] test pt\n", px, py);

// calculate trangle side slopes    

    double mab = (ay-by)/(ax-bx);
    printf(" [ %lf ] slope side ab\n", mab);
    
    double mac = (ay-cy)/(ax-cx);
    printf(" [ %lf ] slope side ac\n", mac);
 
    double mbc = (by-cy)/(bx-cx);
    printf(" [ %lf ] slope side bc\n", mbc);

// test for equal slopes (line segment)    

    if (mab == mac && mab == mbc) return(2);

// test for triangle xmax/xmin    

    if (ax > bx) xmax = ax;
    else xmax = bx;
    if (cx > xmax) xmax = cx;
	
    if (ax < bx) xmin = ax;
    else xmin = bx;
    if (cx < xmin) xmin = cx;

// test if point is within triangle bounding box.

    if (px > xmax || px < xmin || py > cy || py < ay) {
	return(0);
    }

// test for test point on horizontal sides
    
    if (ay == by && by == py) {

	if (ax > bx) {
	    xmin = bx;
	    xmax = ax;
	}

	else {
	    xmin = ax;
	    xmax = bx;
	}
      
	if (px >= xmin && px <= xmax) return(5);
    }

    else if (by == cy && by == py) {

	if (bx > cx) {
	    xmin = cx;
	    xmax = bx;
	}

	else {
	    xmin = bx;
	    xmax = cx;
	}
      
	if (px >= xmin && px <= xmax) return(5);
    }

    
// select which line to use with line ac 
// (test point will always horizontally intercept line ac)

    double dpa = py-ay;
    printf(" [ %lf ] y axis diff test pt from pt a\n", dpa);
    double dpb = py-by;
    printf(" [ %lf ] y axis diff test pt from pt b\n", dpb);

    if (ax == cx) xmin = ax;
    else xmin = ax+(dpa/mac); 
    printf(" [ %lf ] horizontal intercept test pt and side ac\n", xmin);
    
    if (py >= by) {

// if test point horizontal intercept is at or above point b 
// use line bc with ac

	if (bx == cx) xmax = bx;
	else xmax = bx+(dpb/mbc); 
	printf(" [ %lf ] horizontal intercept test pt and side bc\n", xmax);
    }

    else {

// if test point horizontal intercept is below point b 
// use lines ab with ac

	if (ax == bx) xmax = ax;
	else xmax = bx+(dpb/mab); 
	printf(" [ %lf ] horizontal intercept test pt and side bc\n", xmax);
    }

// sort xmin/xmax pair
    
    if (xmin > xmax) { 
	dpa = xmin;
	xmin = xmax;
	xmax = dpa; 
    }

// test for test point on a triangle side
    
    if (px == xmax || px == xmin) return(5);

// test for test point inside the triangle

    if (px <= xmax && px >= xmin) return(1);

// test point is not within the triangle

    else return(0);
}

// end of program.

