You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言循环中如何找出点间最大距离及对应坐标点?

问题描述

我写了代码用来计算不同点之间的距离,现在需要找出其中的最大距离及对应的坐标点,但一直无法实现这个功能。尝试用变量distance2处理时出了更多问题,求解决方法。

原代码
#include <stdio.h>
#include <math.h>

/* Function proto-type declarations */
void where_is_xy(float x, float y);
float compute_distance(float x1, float y1, float x2, float y2);
float saveit, xmax, ymax, x2max, y2max, saveit1,prime;


/* Function definitions */
void where_is_xy(float x, float y) {
    if (x > 0 && y > 0)
    {
        printf("quadrant 1st\n");
    }
    else if (x < 0 && y < 0)
    {
        printf("quadrant 3rd\n");
    }
    else if (x > 0 && y < 0)
    {
        printf("quadrant 4th\n");
    }
    else if (x < 0 && y>0)
    {
        printf(" quadrant 2nd\n");
    }
    else if (x == 0 && (y > 0 || y < 0))
    {
        printf("Y axis\n");
    }
    else if (y == 0 && (x > 0 || x < 0))
    {
        printf("x axis\n");
    }
}

float compute_distance(float x1, float y1, float x2, float y2)
{
    float calculation = sqrt((pow((x1 - x2), 2) + pow((y1 - y2), 2)));
    printf(" The between distance is %0.2f\n", calculation );
    return calculation; 
}

int main(void)
{
    float x1, y1, x2, y2;
    printf("\nEnter (x, y): ");
    scanf_s("%f %f", &x2, &y2);
    where_is_xy(x2, y2);

    float distance1 = 0;
    
    for (int i =1; i < 10; ++i)
    {
        printf("\nEnter (x, y): ");
        scanf_s("%f %f", &x1, &y1);
        where_is_xy(x1, y1);
        
        float distance2 = compute_distance(x1, y1, x2, y2);
        
        if (distance2 > distance1)
        {
        }
        x2 = x1; y2 = y1;
        //distance1 = distance2;
    }
}
解决方案

要找出最大距离及对应坐标,只需修改main函数的逻辑,添加变量记录最大值和对应点即可:

  • 初始化记录变量:在main函数开头添加变量,专门保存最大距离和产生该距离的两个点坐标:

    float max_distance = 0.0f;
    float prev_x_max, prev_y_max, curr_x_max, curr_y_max;
    
  • 更新最大值逻辑:替换循环内的空判断块,当当前计算的距离大于max_distance时,更新最大值并记录对应坐标:

    if (distance2 > max_distance) {
        max_distance = distance2;
        prev_x_max = x2;
        prev_y_max = y2;
        curr_x_max = x1;
        curr_y_max = y1;
    }
    
  • 循环结束后输出结果:在循环末尾添加打印语句,输出最终的最大距离和对应坐标点:

    printf("\n=====================\n");
    printf("最大距离: %.2f\n", max_distance);
    printf("对应的两个点: (%.2f, %.2f) 和 (%.2f, %.2f)\n", prev_x_max, prev_y_max, curr_x_max, curr_y_max);
    
修改后的完整main函数
int main(void)
{
    float x1, y1, x2, y2;
    printf("\nEnter (x, y): ");
    scanf_s("%f %f", &x2, &y2);
    where_is_xy(x2, y2);

    float max_distance = 0.0f;
    float prev_x_max, prev_y_max, curr_x_max, curr_y_max;
    
    for (int i =1; i < 10; ++i)
    {
        printf("\nEnter (x, y): ");
        scanf_s("%f %f", &x1, &y1);
        where_is_xy(x1, y1);
        
        float distance2 = compute_distance(x1, y1, x2, y2);
        
        if (distance2 > max_distance)
        {
            max_distance = distance2;
            prev_x_max = x2;
            prev_y_max = y2;
            curr_x_max = x1;
            curr_y_max = y1;
        }
        x2 = x1; y2 = y1;
    }

    printf("\n=====================\n");
    printf("最大距离: %.2f\n", max_distance);
    printf("对应的两个点: (%.2f, %.2f) 和 (%.2f, %.2f)\n", prev_x_max, prev_y_max, curr_x_max, curr_y_max);
}

内容的提问来源于stack exchange,提问作者Obiick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 09:26:16