C로 만들어 보았다. 아는게 C밖에 없으니...-_-;

#define TRUE 1
#define FALSE 0

typedef struct {
    double x;
    double y;
} point;

typedef struct {
    double a;
    double b;
    double c;
} line;

typedef struct {
    point t1;
    point t2;
    point t3;
} triangle;

line lineDeclaration(point p,point q){
    line l;
    l.a=q.y-p.y;
    l.b=p.x-q.x;
    l.c=p.y*q.x-p.x*q.y;
    return l;
}

double lineSubstitution(point p, line l){
    return l.a*p.x+l.b*p.y+l.c;
}

double lineMultiplication(point p, point q, line l){
    return lineSubstitution(p, l)*lineSubstitution(q,l);
}

point mediumPoint(point p, point q){
    point r={(p.x-q.x)/2.0,(p.y-q.y)/2.0};
    return r;
}

int triangleInteriorPoints(point p, triangle t){
    point side[3];
    side[0].x=t.t1.x;
    side[0].y=t.t1.y;
    side[1].x=t.t2.x;
    side[1].y=t.t2.y;
    side[2].x=t.t3.x;
    side[2].y=t.t3.y;
    
    int i=2;
    int justify=0;
    line l;
    line m;
    while(i){
        l=lineDeclaration(side[i%3],side[(i+1)%3]);
        m=lineDeclaration(side[(i+1)%3],side[(i+2)%3]);
        if(
        lineMultiplication(p, mediumPoint(side[(i+2)%3],side[i]), l)>0 &&
        lineMultiplication(p, mediumPoint(side[(i+2)%3],side[i]), m)>0){
            justify++;
        }
        i--;
    }
    if(justify==3){
        return TRUE;
    }
    else{
        return FALSE;
    }
}
이건 헤더 파일이다. 테스트용 코드는 다음과 같다.
#include<stdio.h>
#include<stdlib.h>
#include"triangle.h"
#include <glib.h>
#include <glib/gprintf.h>


int main(int argc, char* argv[]){
    if(argc==1){
        printf("ERROR. There are no inputs.\n Usage : %s file1 file2\n file1 includes triangle data\n file2 includes points data\n", argv[0]);
        exit(1);
    }
    FILE *TRIDATA;
    FILE *POINTDATA;
    if(!(TRIDATA=fopen(argv[1], "rt"))){
        printf("ERROR. %s does not exists.\n",argv[1]);
        exit(1);
    }
    if(!(POINTDATA=fopen(argv[2],"rt"))){
            printf("ERROR. %s does not exists.\n",argv[2]);
        exit(1);
    }

    char buf1[200];
    char **buffer1;
    char buf2[200];
    char **buffer2;

    int i=0;

while(!feof(TRIDATA) && !feof(POINTDATA)){
        fgets(buf1,200, TRIDATA);
        fgets(buf2,200,POINTDATA);
        buffer1=g_strsplit(buf1, ",", 6);

        triangle tri;
        tri.t1.x=atof(buffer1[0]);
        tri.t1.y=atof(buffer1[1]);
        tri.t2.x=atof(buffer1[2]);
        tri.t2.y=atof(buffer1[3]);
        tri.t3.x=atof(buffer1[4]);
        tri.t3.y=atof(buffer1[5]);

        buffer2=g_strsplit(buf2, ",",2);
        point po;
        po.x=atof(buffer2[0]);
        po.y=atof(buffer2[1]);

        printf("%d,%d\n",triangleInteriorPoints(po, tri),i++);
        g_strfreev(buffer1);
        g_strfreev(buffer2);

    }
    return 1;
}
그리고 컴파일 명령어는 다음과 같다.
gcc `pkg-config --cflags --libs glib-2.0` -o triangle triangle.c
glib2가 있어야 컴파일 된다는 점에 주의하여야 한다.
사용법은 다음과 같다.
 Usage : ./triangle file1 file2
 file1 includes triangle data
 file2 includes points data
triangle data는 콤마(,)로 구분된 6개의 실수가 들어가고, points data는 콤마로 구분된 2개의 실수가 들어간다.


by snowall 2008. 9. 5. 22:45