C# 找到平行四边形的缺失点(Find the Missing Point of Parallelogram)

给定三个坐标点 A、B 和 C,求缺失点 D,使得 ABCD 可以构成平行四边形。

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。 

示例: 

输入: A = (1, 0) 
        B = (1, 1) 
        C = (0, 1)输出: 0, 0

解释:

三个输入点与点 (0, 0) 形成一个单位正方形输入: A = (5, 0) 
        B = (1, 1) 
        C = (2, 5)输出: 6, 4

如下图所示,可能有多种输出,我们需要打印其中的任意一种。 

如果四边形的对边平行且长度相等,则该四边形称为平行四边形。 

给定平行四边形的三个点,我们就能求出缺失边的斜率以及它们的长度。 
该算法解释如下:
设 R 为缺失点。根据定义,我们有 
 

• PR 的长度 = QS 的长度 = L1 (对边相等)
• PR 的斜率 = QS 的斜率 = M1 (对边平行)
• PQ 的长度 = RS 的长度 = L2(对边相等)
• PQ 的斜率 = RS 的斜率 = M2 (对边平行)

因此,我们可以找到距离 P 点 L1 且斜率为 M1 的点,如下文所述: 

在给定斜率的直线上,找到给定距离的点:

Javascript 在给定斜率的线上找到给定距离处的点:https://blog.youkuaiyun.com/hefeng_aspnet/article/details/149936134
C# 在给定斜率的线上找到给定距离处的点:https://blog.youkuaiyun.com/hefeng_aspnet/article/details/149936112
Python 在给定斜率的线上找到给定距离处的点:https://blog.youkuaiyun.com/hefeng_aspnet/article/details/149936088
Java 在给定斜率的线上找到给定距离处的点:https://blog.youkuaiyun.com/hefeng_aspnet/article/details/149936046
C++ 在给定斜率的线上找到给定距离处的点:https://blog.youkuaiyun.com/hefeng_aspnet/article/details/149935654

现在,其中一个点将满足上述条件,这很容易检查(使用条件 3 或 4)。

以下是上述方法的实现: 

// C# program to find missing point of a
// parallelogram
using System;
using System.Collections.Generic;

// struct to represent a co-ordinate point
class Point {
  public double x, y;
  public Point()
  {
    x = 0;
    y = 0;
  }

  public Point(double a, double b)
  {
    x = a;
    y = b;
  }
};

class GFG
{

  // given a source point, slope(m) of line
  // passing through it this function calculates
  // and return two points at a distance l away
  // from the source
  static Point[] findPoints(Point source, double m,
                            double l)
  {
    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;
    }

    // slope if infinity
    else if (m == Double.MaxValue) {
      a.x = source.x;
      a.y = source.y + l;

      b.x = source.x;
      b.y = source.y - l;
    }

    // normal case
    else {
      double dx = (l / Math.Sqrt(1 + (m * m)));
      double dy = m * dx;
      a.x = source.x + dx;
      a.y = source.y + dy;
      b.x = source.x - dx;
      b.y = source.y - dy;
    }

    Point[] res = { a, b };
    return res;
  }

  // given two points, this function calculates
  // the slope of the line/ passing through the
  // points
  static double findSlope(Point p, Point q)
  {
    if (p.y == q.y)
      return 0;
    if (p.x == q.x)
      return Double.MaxValue;
    return (q.y - p.y) / (q.x - p.x);
  }

  // calculates the distance between two points
  static double findDistance(Point p, Point q)
  {
    return Math.Sqrt(Math.Pow((q.x - p.x), 2)
                     + Math.Pow((q.y - p.y), 2));
  }

  // given three points, it prints a point such
  // that a parallelogram is formed
  static void findMissingPoint(Point a, Point b, Point c)
  {
    // calculate points originating from a
    Point[] d = findPoints(a, findSlope(b, c),
                           findDistance(b, c));

    // now check which of the two points satisfy
    // the conditions
    if (findDistance(d[0], c) == findDistance(a, b))
      Console.WriteLine(d[0].x + ", " + d[0].y);
    else
      Console.WriteLine(d[1].x + ", " + d[1].y);
  }

  // Driver code
  public static void Main(string[] args)
  {
    findMissingPoint(new Point(1, 0), new Point(1, 1),
                     new Point(0, 1));
    findMissingPoint(new Point(5, 0), new Point(1, 1),
                     new Point(2, 5));
  }
}

// This code is contributed by phasing17

输出 : 

0, 0 
6, 4

时间复杂度: O(log(log n)),因为使用内置的 sqrt 和 log 函数 

辅助空间: O(1)

替代方法: 

由于对边相等,AD = BC 且 AB = CD,我们可以计算出缺失点 (D) 的坐标:

AD = BC 
(Dx - Ax, Dy - Ay) = (Cx - Bx, Cy - By) 
Dx = Ax + Cx - Bx 
Dy = Ay + Cy - By

参考文献: https://math.stackexchange.com/questions/887095/find-the-4th-vertex-of-the-parallelogram 以下是上述方法的实现:

// C# program to 
// find missing point
// of a parallelogram
using System;

class GFG
{
    static public void Main ()
    {
        int ax = 5, ay = 0; //coordinates of A
        int bx = 1, by = 1; //coordinates of B
        int cx = 2, cy = 5; //coordinates of C
        Console.WriteLine(ax + (cx - bx) + ", " +
                             ay + (cy - by));
    }
}

// This code is contributed by ajit

输出: 

6、4

时间复杂度: O(1) 

辅助空间: O(1)

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

hefeng_aspnet

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

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

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

打赏作者

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

抵扣说明:

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

余额充值