2D triangle area

2023-12-18 13:02:29

?

* 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

文章来源:https://blog.csdn.net/fareast_mzh/article/details/135057661
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。