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

已知圆心、半径和角度,C语言求圆上点坐标及三角形旋转问题排查

2D三角形旋转坐标计算问题

我需要在2D平面中旋转一个三角形,已计算出其质心作为圆心,将三角形的第一个顶点作为0度角的圆上点。思路是通过循环增加角度来旋转每个顶点,并计算对应角度下顶点的新坐标。以下是编写的C语言代码:

#include <stdio.h>
#include <math.h>

// 计算旋转后顶点坐标的函数
int calculate_new_vertex_for_trangle(int center_x, int center_y,int x_at_0,int y_at_0,int angle)
{
    double radius = sqrt(pow(x_at_0-center_x,2)+pow(y_at_0-center_y,2));

    float angle_to_radian = ((22.0/7.0)/180)*angle;

    // 旋转公式
    float new_x = center_x + radius*cos(angle_to_radian);
    float new_y = center_y + radius*sin(angle_to_radian);

    printf("x : %f, y : %f \nradius : %f\n",  new_x, new_y,radius);

    return 0;
}

实际计算结果和预期不符,需要排查问题。


问题排查与修复
  • 圆周率近似值精度不足
    用22.0/7.0近似圆周率和真实π(≈3.1415926535)误差较大,会导致弧度转换偏差,进而让旋转坐标计算出错。应直接使用math.h提供的M_PI常量,精度更高。
  • 变量类型混用导致精度损耗
    代码中混用double和float,而cos、sin默认接收double类型参数,传入float会隐式转换,额外损失精度,建议统一用double类型。

修复后的代码:

#include <stdio.h>
// Windows平台MSVC编译需添加此行,GCC可省略
#define _USE_MATH_DEFINES
#include <math.h>

int calculate_new_vertex_for_trangle(int center_x, int center_y, int x_at_0, int y_at_0, int angle)
{
    double radius = sqrt(pow(x_at_0 - center_x, 2) + pow(y_at_0 - center_y, 2));
    // 标准弧度转换公式
    double angle_to_radian = angle * M_PI / 180.0;

    double new_x = center_x + radius * cos(angle_to_radian);
    double new_y = center_y + radius * sin(angle_to_radian);

    printf("x : %lf, y : %lf \nradius : %lf\n", new_x, new_y, radius);

    return 0;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 21:24:28