Loading [MathJax]/jax/output/HTML-CSS/jax.js

使用 C++ 查找已知一个端点和中点的线的另一个端点


在这个问题中,我们给定了一条线的两个点的坐标,起点 A(xA, yA) 和中点 M(xM, yM)。我们的任务是找到已知一个端点和中点的线的另一个端点

让我们举一个例子来理解这个问题,

输入

A = [1, 2], M = [3, 0]

输出

[5, -2]

解释

这条线是 -

解决方案方法

为了解决这个问题,我们将使用我们在数学中学习过的几何概念。如果你还记得,每条线都有一个中点公式,即:

mid(x) = (x1 + x2) / 2
mid(y) = (y1 + y2) / 2

但是我们给出了问题中的中点值,需要 x2 和 y2 的值。因此,我们将相应地更改公式。

x2 = 2*mid(x) - x1
y2 = 2*mid(y) - y1

使用上述公式,我们可以使用线的中点和一个点找到另一个端点的值。

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

示例

程序说明我们解决方案的工作原理

Open Compiler
#include <iostream> using namespace std; void findMissingPointLine(float x1, float y1, float xm, float ym){ float x2 = (2 * xm) - x1; float y2 = (2 * ym) - y1; cout<<"B(x, y) = "<<"( "<<x2<<", "<<y2<<" )"; } int main() { float x1 = -4, y1 = -1, xm = 3, ym = 5; cout<<"The other end point of the line is \n"; findMissingPointLine(x1, y1, xm, ym); return 0; }

输出

The other end point of the line is
B(x, y) = ( 10, 11 )

更新于: 2022年2月11日

145 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告