Points on Cycle

本文介绍了一种计算给定圆上一点时,如何找出该圆内接等边三角形另外两点坐标的算法。利用向量和坐标几何原理,通过公式推导解决了这一问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

There is a cycle with its center on the origin. 
Now give you a point on the cycle, you are to find out the other two points on it, to maximize the sum of the distance between each other
you may assume that the radius of the cycle will not exceed 1000.
 
Input

There are T test cases, in each case there are 2 decimal number representing the coordinate of the given point.
 
Output

For each testcase you are supposed to output the coordinates of both of the unknow points by 3 decimal places of precision 
Alway output the lower one first(with a smaller Y-coordinate value), if they have the same Y value output the one with a smaller X. 

NOTE
when output, if the absolute difference between the coordinate values X1 and X2 is smaller than 0.0005, we assume they are equal.
 
Sample Input

 2
1.500 2.000
563.585 1.251 
 
Sample Output

 0.982 -2.299 -2.482 0.299
-280.709 -488.704 -282.876 487.453 

给出一个以原点为圆心的圆上的一点,问其内接等边三形另外两个点的坐标
刚开始想直接利用三角函数角度的偏移来计算,但是在c中通过反三角函数求出来的角度只能是在-90到90度之间,并不是0到360度,所以无法准确定位角在哪一个向限。所以只能通过向量a向量b,a*b=1/2*|a||b|;a*a+b*b=r*r  x^2+y^2=r^2,这三个公式进行计算


#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
 
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        double x1,y1,r;
        scanf("%lf%lf",&x1,&y1);
        r = x1*x1+y1*y1;
        double a = 1;
        double b = y1;
        double c = r/4-x1*x1;
        double delta = b*b-4*a*c;
        double ansy1 = (-b-sqrt(delta))/2/a;
        double ansy2 = (-b+sqrt(delta))/2/a;
        double ansx1,ansx2;
        if(fabs(x1)<=1e-7){
            ansx1 = -sqrt(r-ansy1*ansy1);
            ansx2 =  sqrt(r-ansy2*ansy2);
        }
        else{
            ansx1 = (-r/2-y1*ansy1)/x1;
            ansx2 = (-r/2-y1*ansy2)/x1;
        }
        printf("%.3lf %.3lf %.3lf %.3lf\n",ansx1,ansy1,ansx2,ansy2);
    }
    return 0;
}

 

这是原有的代码 请结合一下#include "led.h" #include "stm32f10x.h" #include <math.h> //#define BREATH_CYCLE 400 // 呼吸周期(ms) //#define SAMPLE_POINTS 85 // 每个周期采样点数 //#define PHASE_SHIFT 25 // 相邻LED相位差(采样点数) //#define BRIGHTNESS_FACTOR1 1.0f // 第一个LED亮度因子(降低最大亮度) //#define BRIGHTNESS_FACTOR2 1.0f // 第二个LED亮度因子 //#define BRIGHTNESS_FACTOR3 1.0f // 第三个LED亮度因子 #define BREATH_CYCLE 450 // 呼吸周期(ms) #define SAMPLE_POINTS 80 // 每个周期采样点数 #define PHASE_SHIFT 17 // 相邻LED相位差(采样点数),约120度 void delay_ms(int32_t ms); void breathing_led(void); int main() { LED_Init(); while(1) { GPIO_ResetBits(GPIOA,GPIO_Pin_1); delay_ms(1000); GPIO_ResetBits(GPIOB,GPIO_Pin_8); delay_ms(1000); GPIO_ResetBits(GPIOB,GPIO_Pin_9); delay_ms(1000); GPIO_SetBits(GPIOA,GPIO_Pin_1); GPIO_SetBits(GPIOB,GPIO_Pin_8); GPIO_SetBits(GPIOB,GPIO_Pin_9); delay_ms(1000); GPIO_ResetBits(GPIOA,GPIO_Pin_1); GPIO_ResetBits(GPIOB,GPIO_Pin_8); GPIO_ResetBits(GPIOB,GPIO_Pin_9); delay_ms(1000); GPIO_SetBits(GPIOA,GPIO_Pin_1); GPIO_SetBits(GPIOB,GPIO_Pin_8); GPIO_SetBits(GPIOB,GPIO_Pin_9); delay_ms(1000); breathing_led(); } } void delay_ms(int32_t ms) {int32_t i; while(ms--) {i=14430; while(i--);} } /* //// 三个LED独立呼吸效果 //void breathing_led(void) //{ // uint16_t i; // uint32_t on_time1, off_time1; // uint32_t on_time2, off_time2; // uint32_t on_time3, off_time3; // const uint32_t cycle_time = BREATH_CYCLE / SAMPLE_POINTS; // // // 三个LED的相位差 // for (i = 0; i < SAMPLE_POINTS; i++) { // // 计算每个LED的相位(错开20个采样点) // float angle1 = (float)i * 3.14159f / SAMPLE_POINTS; // float angle2 = (float)(i + PHASE_SHIFT) * 3.14159f / SAMPLE_POINTS; // float angle3 = (float)(i + PHASE_SHIFT * 2) * 3.14159f / SAMPLE_POINTS; // // // 使用正弦函数计算亮度(0.0~1.0) // float brightness1 = (sin(angle1) + 1.0f) / 2.0f*BRIGHTNESS_FACTOR1; // float brightness2 = (sin(angle2) + 1.0f) / 2.0f; // float brightness3 = (sin(angle3) + 1.0f) / 2.0f; // // // 计算每个LED的亮灭时间 // on_time1 = (uint32_t)(cycle_time * brightness1*1.2f); // off_time1 = cycle_time - on_time1; // // on_time2 = (uint32_t)(cycle_time * brightness2); // off_time2 = cycle_time - on_time2; // // on_time3 = (uint32_t)(cycle_time * brightness3); // off_time3 = cycle_time - on_time3; // // // 控制LED1 (PA1) // GPIO_ResetBits(GPIOA, GPIO_Pin_1); // delay_ms(on_time1); // GPIO_SetBits(GPIOA, GPIO_Pin_1); // delay_ms(off_time1); // // // 控制LED2 (PB8) // GPIO_ResetBits(GPIOB, GPIO_Pin_8); // delay_ms(on_time2); // GPIO_SetBits(GPIOB, GPIO_Pin_8); // delay_ms(off_time2); // // // 控制LED3 (PB9) // GPIO_ResetBits(GPIOB, GPIO_Pin_9); // delay_ms(on_time3); // GPIO_SetBits(GPIOB, GPIO_Pin_9); // delay_ms(off_time3); // } //} */ // 三个LED同时呼吸(相位错开)延时模拟的方式实现呼吸灯效果 void breathing_led(void) { uint16_t i; uint32_t on_time1, off_time1; // LED1 (PA1) uint32_t on_time2, off_time2; // LED2 (PB8) uint32_t on_time3, off_time3; // LED3 (PB9) const uint32_t cycle_time = BREATH_CYCLE / SAMPLE_POINTS; // 呼吸灯效果(三个LED相位错开) for (i = 0; i < SAMPLE_POINTS * 2; i++) { // 计算每个LED的相位(错开120度,约17个采样点) float angle1 = (float)i * 3.14159f / SAMPLE_POINTS; float angle2 = (float)(i + PHASE_SHIFT) * 3.14159f / SAMPLE_POINTS; float angle3 = (float)(i + PHASE_SHIFT * 2) * 3.14159f / SAMPLE_POINTS; // 使用正弦函数计算亮度(0.0~1.0) float brightness1 = (sin(angle1) + 1.0f) / 2.0f; float brightness2 = (sin(angle2) + 1.0f) / 2.0f; float brightness3 = (sin(angle3) + 1.0f) / 2.0f; // 计算每个LED的亮灭时间 on_time1 = (uint32_t)(cycle_time * brightness1); off_time1 = cycle_time - on_time1; on_time2 = (uint32_t)(cycle_time * brightness2); off_time2 = cycle_time - on_time2; on_time3 = (uint32_t)(cycle_time * brightness3); off_time3 = cycle_time - on_time3; // 控制LED1 (PA1) GPIO_ResetBits(GPIOA, GPIO_Pin_1); delay_ms(on_time1); GPIO_SetBits(GPIOA, GPIO_Pin_1); delay_ms(off_time1); // 控制LED2 (PB8) GPIO_ResetBits(GPIOB, GPIO_Pin_8); delay_ms(on_time2); GPIO_SetBits(GPIOB, GPIO_Pin_8); delay_ms(off_time2); // 控制LED3 (PB9) GPIO_ResetBits(GPIOB, GPIO_Pin_9); delay_ms(on_time3); GPIO_SetBits(GPIOB, GPIO_Pin_9); delay_ms(off_time3); } }
06-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值