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.youkuaiyun.com/hefeng_aspnet/article/details/141320964

Java https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141321133

Python https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141321178

C# https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141321206

Javascript https://blog.youkuaiyun.com/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
using System;
 
class GFG {
 
    // Class to represent a co-ordinate
    // point
    public class Point {
        public float x, y;
 
        public Point() { x = y = 0; }
 
        public 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
    static 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 = new Point();
        Point b = new Point();
 
        // 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 (Double.IsInfinity(m)) {
            a.x = source.x;
            a.y = source.y + l;
 
            b.x = source.x;
            b.y = source.y - l;
        }
        else {
            float dx = (float)(l / Math.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
        Console.WriteLine(a.x + ", " + a.y);
 
        // Print the second Point
        Console.WriteLine(b.x + ", " + b.y);
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        Point p = new Point(2, 1), q = new Point(1, 0);
 
        printPoints(p, (float)Math.Sqrt(2), 1);
 
        Console.WriteLine();
 
        printPoints(q, 5, 0);
    }
}
 
// This code is contributed by Amit Katiyar 

输出:
3, 2 
1, 0 

6, 0 
-4, 0

时间复杂度: O(1)

辅助空间: O(1)

直线类(综合型题目) (1)创建C#控制台应用程序L4_1。 (2)在程序中新建一个类CzPoint,为其定义两个double类型的私有字段成员x和y,分别表示的横坐标和纵坐标。 (3)为CzPoint定义两个公有属性X、Y,分别用于封装对字段x和y的读写访问。 (4)定义CzPoint的带参数构造函数,在其中对字段x和y进行初始化。 (5)为CzPoint定义公有方法Move,用于按指定的水平距离和垂直距离移动坐标。 (6)对CzPoint类进行相等和不相等操作符重载。两个坐标相等,是指它们的横坐标和纵坐标都相等。 (7)在程序主方法中创建两个坐标对象,判断它们是否相等;而后将第一个坐标移动到第二个坐标上,再判断它们是否相等。 (8)在程序中再新建一个直线类CzLine,为其定义两个double类型的字段成员a 和b,分别表示直线斜率和截距;再定义字段封装属性A和B,但它们都是只读的。 (9)为CzLine定义两个构造函数,一个根据斜率和截距来创建直线对象,另一个则根据两个CzPoint对象来构造直线对象(直线穿过这两个)。后一个的参考源代码如下(因涉及数学公式,故给出代码): public CzLine(CzPoint p1,CzPoint p2) { this.a=(p2.Y-p1.Y)/(p2.X-p1.X); this.b=p1.Y-this.a*p1.X; } (10)为CzLine定义公有方法Move,但它只用于平移直线,而不改变直线斜率(即平移后的直线与原来的直线平行)。再为其定义公有方法Contains,用于判断某是否在该直线上。 (11)类似的,为CzLine重载相等和不相等操作符。 (12)最后在程序主方法中采用不同的方式创建直线对象,并编译运行程序,测试它们的使用效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hefeng_aspnet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值