1v13d/src/FxEngine/face.c

74 lines
2.5 KiB
C
Raw Normal View History

2019-07-25 13:23:41 +02:00
#include "face.h"
#include "zbuffer.h"
#include <stdbool.h>
#include <gint/display.h>
#define min(x,y) (x<y?x:y)
#define max(x,y) (x>y?x:y)
2019-07-25 13:23:41 +02:00
2019-07-26 14:39:08 +02:00
static bool sens_horaire(FE_face const * face)
{
2019-07-26 14:39:08 +02:00
int area = 0;
area+=face->s1->translated.x*face->s2->translated.y;
area-=face->s2->translated.x*face->s1->translated.y;
area+=face->s2->translated.x*face->s3->translated.y;
area-=face->s3->translated.x*face->s2->translated.y;
area+=face->s3->translated.x*face->s1->translated.y;
area-=face->s1->translated.x*face->s3->translated.y;
return (area < 0);
}
2019-07-25 13:23:41 +02:00
void FE_draw_face(FE_face const * face)
{
if (face->s1->translated.x==face->s2->translated.x && face->s1->translated.x==face->s3->translated.x)
return;
if (face->s1->translated.y==face->s2->translated.y && face->s1->translated.y==face->s3->translated.y)
return;
if (face->s1->translated.x<0 && face->s2->translated.x<0 && face->s3->translated.x<0)
return;
if (face->s1->translated.x>127 && face->s2->translated.x>127 && face->s3->translated.x>127)
return;
if (face->s1->translated.y<0 && face->s2->translated.y<0 && face->s3->translated.y<0)
return;
if (face->s1->translated.y>63 && face->s2->translated.y>63 && face->s3->translated.y>63)
return;
2019-07-26 14:39:08 +02:00
if (sens_horaire(face)!=face->visible)
return;
const int xmin=max(min(face->s1->translated.x,min(face->s2->translated.x,face->s3->translated.x)),0);
const int xmax=min(max(face->s1->translated.x,max(face->s2->translated.x,face->s3->translated.x)),127);
const int ymin=max(min(face->s1->translated.y,min(face->s2->translated.y,face->s3->translated.y)),0);
const int ymax=min(max(face->s1->translated.y,max(face->s2->translated.y,face->s3->translated.y)),63);
const int xAB=face->s2->translated.x-face->s1->translated.x, yAB=face->s2->translated.y-face->s1->translated.y;
const int xAC=face->s3->translated.x-face->s1->translated.x, yAC=face->s3->translated.y-face->s1->translated.y;
const int diviseur_commun=xAB*yAC-yAB*xAC;
for (int tx=xmin; tx<=xmax; tx++)
{
for (int ty=ymin; ty<=ymax; ty++)
{
if (FE_zbuffer_set_dist(tx, ty, 1))
dpixel(tx,ty,C_BLACK);
}
}
// x=Det(AM,AC)/det(AB,AC)
// y=det(AM,AB)/det(AC,AB)
// x=(xAM*yAC-yAM*xAC)/(xAB*yAC-yAB*xAC)
// y=(xAM*yAB-yAM*xAB)/(xAC*yAB-yAC*xAB)
// soit diviseur_commun = (xAB*yAC-yAB*xAC)
// x=xAM*yAC/diviseur_commun-yAM*xAC/diviseur_commun
// y=yAM*xAB/diviseur_commun-xAM*yAB/diciseur_commun
2019-07-25 13:23:41 +02:00
}