?
* area3.c
#include <stdio.h>
#define DET2(a) (a[0][0]*a[1][1] - a[0][1]*a[1][0])
#define POINT2_TYPE(type) struct point2##type
#define STRUCT_POINT2(type) \
POINT2_TYPE(type) { \
type x; \
type y; \
}
#define AREA_3POINT2_NAME(type) area_3point2##type
#define AREA_3POINT2_FUNC(type) \
type AREA_3POINT2_NAME(type)(POINT2_TYPE(type) a, \
POINT2_TYPE(type) b, \
POINT2_TYPE(type) c) { \
type mat[2][2] = { \
{b.x - a.x, c.x - a.x}, \
{b.y - a.y, c.y - a.y}, \
}; \
return DET2(mat)/2.0; \
}
typedef STRUCT_POINT2(float) Point2f_t;
AREA_3POINT2_FUNC(float);
int main(int argc, char *argv[]) {
Point2f_t pa = {2, 0}, pb = {-1,3}, pc = {0, 0};
float (*get_area)(Point2f_t, Point2f_t, Point2f_t);
get_area = AREA_3POINT2_NAME(float);
printf("area=%.2f\n", get_area(pa, pb, pc));
return 0;
}
cc -g area3.c
./a.out