Mooc 课程:程序设计基础——发现计算之美
李骏扬 、魏海坤 、仰燕兰 、朱蔚萍 、杨万扣
网址:https://www.icourse163.org/course/SEU-1003771004
判断三角形是否存在
详见我的博客 【C++ 程序】 判断是否构成三角形。
基站问题
有两个基站,A坐标(0,0),B坐标(100,0)
假设飞行器根据接收到两个基站A、B信号的强度,可以得到距离A、B的距离,Da 和 Db,那么请根据Da 和 Db 计算飞行器的坐标。
- Da、Db依靠手工输入
- 对Da、Db的有效性进行判定
- 自行推理公式(解析法)计算
- 假设 y 坐标一定 > 0
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
begin:
double Da = 0, Db = 0;
double x, y;
constexpr double c = 50;
double a;
cin >> Da >> Db;
if (Da * Db <= 0 || Da + Db <= 100)
{
cerr << "Illegal input!\nTry again? Enter Y or N." << endl;
char decision;
cin >> decision;
if (decision != 'N' && decision != 'n') goto begin;
else return -1;
}
else
{
a = (Da + Db) / 2;
x = a * a / c - Db * a / c;
y = sqrt((a * a - c * c) * (1 - x * x / (a * a)));
}
cout << "(" << x + 50 << "," << y << ")" << endl;
return 0;
}
这就是一道简单的解析几何问题,利用焦半径公式(由椭圆第二定义/圆锥曲线统一定义),先求出x,再代回椭圆方程求出y。
ALL RIGHTS RESERVED © 2020 Teddy van Jerry
欢迎转载,转载请注明出处。