【C#】已知有三个坐标点:P0、P1、P2,当满足P3和P4连成的一条直线 与 P0和P1连成一条直线平行且长度一致,该如何计算P3、P4?

29 篇文章 2 订阅

问题描述

已知有三个坐标点:P0、P1、P2,当满足P3和P4连成的一条直线 与 P0和P1连成一条直线平行长度一致,该如何计算P3、P4?

 

解决办法

思路一:斜率及点斜式方程

6a6ffa76862f47eb9ac46aa74031d56c.png


# 示例坐标
x0, y0 = 1, 1 # P0坐标
x1, y1 = 4, 4 # P1坐标
x2, y2 = 2, 2 # P2坐标

# 计算直线P0P1的斜率
m = (y1 - y0) / (x1 - x0)

# 设定P3和P4在x轴上的不同值
x3 = x2 - 1
x4 = x2 + 1

# 使用点斜式方程求P3和P4的y坐标
y3 = m * (x3 - x2) + y2
y4 = m * (x4 - x2) + y2

# P3和P4的坐标
P3 = (x3, y3)
P4 = (x4, y4)

P3, P4

思路二:斜率及两线相交

直线的斜率

斜率是数学中的一个概念,特别是在解析几何和平面直角坐标系中,用来描述一条直线倾斜程度的量。它定义为直线上任意两点之间的垂直变化量(即纵坐标的变化量,通常称为“上升”或“Δy”)与水平变化量(即横坐标的变化量,通常称为“运行”或“Δx”)之比。斜率通常用字母 m 表示。

 

6af594287d3649baa8746d1254c040a5.png

需要注意的是,对于垂直线,由于水平变化量“Δx” 为零,所以斜率无法定义,因为这会导致分母为零,我们说垂直线的斜率是无穷大或未定义。

斜率的概念在许多数学和物理问题中都有应用,例如在微积分中,导数可以看作是曲线在某一点处的瞬时斜率;在物理学中,斜率可以表示速度、加速度等随时间的变化率。

在实际应用中,斜率也可以帮助我们理解数据的趋势,比如在统计学中,通过计算散点图中数据点的斜率,我们可以了解变量间的关系是正相关还是负相关。

 

注意:两条平行的直线斜率是相等的。

 

如何计算两条直线的垂直交点

15521f07509e4798bcb53b19a91f1433.png

using System;
using System.Numerics; // For handling potential overflow with large numbers

public class LineIntersection
{
    public static Tuple<double, double>? FindPerpendicularIntersection(Tuple<double, double> p1, Tuple<double, double> p2, Tuple<double, double> p3, Tuple<double, double> p4)
    {
        double dx1 = p2.Item1 - p1.Item1;
        double dy1 = p2.Item2 - p1.Item2;
        double dx2 = p4.Item1 - p3.Item1;
        double dy2 = p4.Item2 - p3.Item2;

        // Check for vertical and horizontal lines
        bool isVerticalLine1 = Math.Abs(dx1) < double.Epsilon;
        bool isVerticalLine2 = Math.Abs(dx2) < double.Epsilon;
        bool isHorizontalLine1 = Math.Abs(dy1) < double.Epsilon;
        bool isHorizontalLine2 = Math.Abs(dy2) < double.Epsilon;

        if (isVerticalLine1 && isVerticalLine2)
        {
            // Both lines are vertical, check if they coincide
            if (Math.Abs(p1.Item1 - p3.Item1) > double.Epsilon)
                return null; // Lines do not intersect
        }
        else if (isVerticalLine1)
        {
            // Line 1 is vertical, Line 2 is not
            if (isHorizontalLine2)
            {
                // Line 2 is horizontal, check intersection
                if (p3.Item1 <= p1.Item1 && p1.Item1 <= p4.Item1 || p4.Item1 <= p1.Item1 && p1.Item1 <= p3.Item1)
                    return Tuple.Create(p1.Item1, p3.Item2);
                else
                    return null;
            }
            else
            {
                // Calculate intersection of vertical line 1 and non-vertical line 2
                double m2 = dy2 / dx2;
                double b2 = p3.Item2 - m2 * p3.Item1;
                double y = m2 * p1.Item1 + b2;
                return Tuple.Create(p1.Item1, y);
            }
        }
        else if (isVerticalLine2)
        {
            // Line 2 is vertical, Line 1 is not
            if (isHorizontalLine1)
            {
                // Line 1 is horizontal, check intersection
                if (p1.Item1 <= p3.Item1 && p3.Item1 <= p2.Item1 || p2.Item1 <= p3.Item1 && p3.Item1 <= p1.Item1)
                    return Tuple.Create(p3.Item1, p1.Item2);
                else
                    return null;
            }
            else
            {
                // Calculate intersection of vertical line 2 and non-vertical line 1
                double m1 = dy1 / dx1;
                double b1 = p1.Item2 - m1 * p1.Item1;
                double y = m1 * p3.Item1 + b1;
                return Tuple.Create(p3.Item1, y);
            }
        }
        else if (isHorizontalLine1 && isHorizontalLine2)
        {
            // Both lines are horizontal, check if they coincide
            if (Math.Abs(p1.Item2 - p3.Item2) > double.Epsilon)
                return null; // Lines do not intersect
        }
        else if (isHorizontalLine1)
        {
            // Line 1 is horizontal, Line 2 is not
            double m2 = dy2 / dx2;
            double b2 = p3.Item2 - m2 * p3.Item1;
            double x = (p1.Item2 - b2) / m2;
            return Tuple.Create(x, p1.Item2);
        }
        else if (isHorizontalLine2)
        {
            // Line 2 is horizontal, Line 1 is not
            double m1 = dy1 / dx1;
            double b1 = p1.Item2 - m1 * p1.Item1;
            double x = (p3.Item2 - b1) / m1;
            return Tuple.Create(x, p3.Item2);
        }
        else
        {
            // Neither line is vertical or horizontal
            double det = dx1 * dy2 - dx2 * dy1;
            if (Math.Abs(det) < double.Epsilon)
            {
                // Lines are parallel or coincident
                return null;
            }

            double x = (dx1 * (p3.Item2 - p1.Item2) - dy1 * (p3.Item1 - p1.Item1)) / det;
            double y = (dy2 * (p3.Item1 - p1.Item1) - dx2 * (p3.Item2 - p1.Item2)) / det;
            return Tuple.Create(x, y);
        }

        return null; // Should never reach here
    }
}

需要注意的是,上述代码没有处理垂直线的情况,因为垂直线的斜率为无穷大。在实际应用中,你需要特别检查分母是否为零,以避免除以零的错误。

此外,如果两条直线实际上是平行的而不是垂直的(即 m1=m2​),则上述代码会抛出异常,因为这样的直线没有交点(除非它们是同一直线,在这种情况下,它们有无数个交点)。

如果你需要一个更完整的实现,包括处理垂直线和重合线的情况,请告知我,我可以进一步详细说明。

思路三:方向向量 

using System;

public class CoordinateCalculator
{    public static void Main(string[] args)
    {
        // 坐标1、坐标2、坐标3
        double[] coord1 = { 1.0, 2.0 };
        double[] coord2 = { 4.0, 6.0 };
        double[] coord3 = { 7.0, 8.0 };

        // 计算坐标4和坐标5
        double[][] results = CalculateCoordinates(coord1, coord2, coord3);

        // 输出结果
        Console.WriteLine("坐标4: (" + results[0][0] + ", " + results[0][1] + ")");
        Console.WriteLine("坐标5: (" + results[1][0] + ", " + results[1][1] + ")");
    }

    public static double[][] CalculateCoordinates(double[] coord1, double[] coord2, double[] coord3)
    {
        // 计算坐标1和坐标2连成的直线的方向向量
        double dx = coord2[0] - coord1[0];
        double dy = coord2[1] - coord1[1];

        // 计算坐标1和坐标2之间的距离
        double distance = Math.Sqrt(dx * dx + dy * dy);

        // 归一化方向向量
        double directionX = dx / distance;
        double directionY = dy / distance;

        // 坐标4和坐标5是沿着方向向量和反方向向量的点
        double[] coord4 = { coord3[0] + directionX * distance, coord3[1] + directionY * distance };
        double[] coord5 = { coord3[0] - directionX * distance, coord3[1] - directionY * distance };

        return new double[][] { coord4, coord5 };
    }
}

 

注意:上述只是参考,具体的实现还需自己计算。

 

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在 B 样条曲线中,控制的位置是通过关键和一些规则来确定的。下面是一个示例的 C# 代码,用于计算 p2 的左控制和右控制坐标: ```csharp using System; public class B_SplineCurve { // 计算控制坐标 public static void CalculateControlPoints(Point p1, Point p2, Point p3, out Point leftControlPoint, out Point rightControlPoint) { // 控制位置的计算规则 leftControlPoint = new Point((2 * p2.X) - p1.X, (2 * p2.Y) - p1.Y); rightControlPoint = new Point((2 * p2.X) - p3.X, (2 * p2.Y) - p3.Y); } } public class Point { public double X { get; set; } public double Y { get; set; } public Point(double x, double y) { X = x; Y = y; } } public class Program { public static void Main(string[] args) { // 三个关键坐标 Point p1 = new Point(1, 2); Point p2 = new Point(3, 4); Point p3 = new Point(5, 6); // 计算控制坐标 B_SplineCurve.CalculateControlPoints(p1, p2, p3, out Point leftControlPoint, out Point rightControlPoint); // 输出结果 Console.WriteLine("左控制坐标:({0}, {1})", leftControlPoint.X, leftControlPoint.Y); Console.WriteLine("右控制坐标:({0}, {1})", rightControlPoint.X, rightControlPoint.Y); } } ``` 在这个示例中,我们定义了一个 `B_SplineCurve` 类,其中的 `CalculateControlPoints` 方法用于计算控制坐标。使用 `Point` 类来表示坐标。 在 `Main` 方法中,我们定义了三个关键坐标 `p1`、`p2`、`p3`,然后调用 `B_SplineCurve.CalculateControlPoints` 方法计算 `p2` 的左控制和右控制坐标,并将结果输出到控制台。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

wangnaisheng

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

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

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

打赏作者

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

抵扣说明:

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

余额充值