当前位置: 首页 > news >正文

C++ 在给定斜率的线上找到给定距离处的点(Find points at a given distance on a line of given slope)

给定二维点 p(x 0 , y 0 )的坐标。找到距离该点 L 的点,使得连接这些点所形成的线的斜率为M。

例子: 
输入: p = (2, 1)
        L = sqrt(2)
        M = 1
输出:3, 2
        1, 0
解释:
与源的距离为 sqrt(2) ,并具有所需的斜率m = 1。

输入: p = (1, 0)
        L = 5
        M = 0
输出: 6, 0
        -4, 0
        
我们需要找到与给定点距离为 L 的两个点,它们位于斜率为 M 的直线上。
这个想法已在下面的帖子中介绍:
C++ https://blog.csdn.net/hefeng_aspnet/article/details/141320964

Java https://blog.csdn.net/hefeng_aspnet/article/details/141321133

Python https://blog.csdn.net/hefeng_aspnet/article/details/141321178

C# https://blog.csdn.net/hefeng_aspnet/article/details/141321206

Javascript https://blog.csdn.net/hefeng_aspnet/article/details/141321238

根据输入的斜率,该问题可以分为 3 类。  

1、如果斜率为零,我们只需要调整源点的 x 坐标

2、如果斜率无限大,则需要调整 y 坐标

3、对于其他斜率值,我们可以使用以下方程来找到点

现在利用上述公式我们可以找到所需的点。

// C++ program to find the points on a line of
// slope M at distance L
#include <bits/stdc++.h>
using namespace std;
 
// structure to represent a co-ordinate
// point
struct Point {
 
    float x, y;
    Point() { x = y = 0; }
    Point(float a, float b) { x = a, y = b; }
};
 
// Function to print pair of points at
// distance 'l' and having a slope 'm'
// from the source
void printPoints(Point source, float l, int m)
{
    // m is the slope of line, and the
    // required Point lies distance l
    // away from the source Point
    Point a, b;
 
    // slope is 0
    if (m == 0) {
        a.x = source.x + l;
        a.y = source.y;
 
        b.x = source.x - l;
        b.y = source.y;
    }
 
    // if slope is infinite
    else if (m == std::numeric_limits<float>::max()) {
        a.x = source.x;
        a.y = source.y + l;
 
        b.x = source.x;
        b.y = source.y - l;
    }
    else {
        float dx = (l / sqrt(1 + (m * m)));
        float dy = m * dx;
        a.x = source.x + dx;
        a.y = source.y + dy;
        b.x = source.x - dx;
        b.y = source.y - dy;
    }
 
    // print the first Point
    cout << a.x << ", " << a.y << endl;
 
    // print the second Point
    cout << b.x << ", " << b.y << endl;
}
 
// driver function
int main()
{
    Point p(2, 1), q(1, 0);
    printPoints(p, sqrt(2), 1);
    cout << endl;
    printPoints(q, 5, 0);
    return 0;
}

输出:
3, 2 
1, 0 

6, 0 
-4, 0

时间复杂度: O(1)

辅助空间: O(1)


http://www.mrgr.cn/news/20752.html

相关文章:

  • iOS——APP启动流程
  • 【C语言从不挂科到高绩点】13-二维数组以及数组元素增加和删除
  • 盘古信息IMS MOM,高效灵活的企业数字化解决方案
  • 开放式运动耳机好不好用?超靠谱好评榜单实物测评
  • 美团面试:mysql 索引失效?怎么解决? (重点知识,建议收藏,读10遍+)
  • 录屏没声音怎么办?3招解决,教您秒变声音大师
  • 【Boost】Asio库学习(一)
  • 号称第一本程序员的Agent入门书籍?《大模型应用开发 动手做AI Agent》来了!
  • 基于SringBoot框架的智慧博物馆预约平台
  • Spark常见面试题整理
  • Java队列详细解释
  • VLDB 2024论文解读丨GaussDB:计算-内存-存储三层池化解耦的多主云原生数据库
  • 基于51单片机的倒计时装置proteus仿真
  • Java高级编程—多线程(完整详解线程的三种实现方式、以及守护线程、出让线程、插入线程、线程声明周期等,附有代码+案例)
  • 零基础入门~汇编语言(第四版王爽)~第4章 第一个程序
  • 前端面试:margin和padding分别适合什么场景使用?
  • 不敢相信,华为智能眼镜2开放式聆听也能做到通话隐私保护了?
  • 如果文件从存储卡中被误删除,存储卡数据恢复如何恢复?
  • 如何进行亚马逊自养号测评?注意事项有哪些?
  • 苹果qq文件过期了怎么恢复?简单4招,拯救你的过期文件