C语言根据点的坐标求出N个点间的N-1条线段分别长

用的Visual Studio2019所以scanf用了scanf_s

点与点之间坐标求距离  知道点之间的距离是亮点横纵坐标差的平方和开根号就很好算了,代码实现这逻辑就好了

#include
#include
#define max 20
double distance(int x1, int y1, int  x2, int y2) {
	return sqrt(pow((double)x1 - (double)x2, 2) + pow((double)y1 - (double)y2, 2));
}
int main() {
	int n = 0;
	int point[max];
	printf("输入点的个数(n(2<=n<=10)):");
	scanf_s("%d", &n);
	while (1) {
		if(n<2||n>10){
			printf("输入点的个数(n(2<=n<=10)):");
			scanf_s("%d", &n);
		}
		else 
			break;
	}
	for(int i= 0; i < 2 * n; i++) {
		scanf_s("%d", &point[i]);
	}
	for(int i = 0; i < n-1; i++) {
		printf("%.2lf\t", distance(point[2 * i], point[2 * i + 1], point[2 * i + 2],point[2 * i + 3]));
	}
	printf("\n");
	return 0;
}

你可能感兴趣的