zoukankan      html  css  js  c++  java
  • 判断点是否在线段上的问题

    又是一个新的问题。
    一个判断点是否在线段上的题。这个题的上一个题是通过判断点在线段的左、中、右三个方向。按原书中的内容编写程序。发现和原书的完全不一样。我最后在今天找到了一个比较合理的答案,非常感谢写这个公式的作者,谢谢

    以下,是我截取他文章的内容。

    怎么判断坐标为(xp,yp)的点P是在直线的哪一侧呢?
    设直线是由其上两点(x1,y1),(x2,y2)确定的,直线方向是由(x1,y1)到(x2,y2)的方向。
    假设直线方程为:Ax+By+C=0,则有:
        A=y2-y1;   B=x1-x2;  C=x2y1-x1y2;
    这时可以通过计算D,来判断点P是在直线的哪一侧:
        D=Axp+Byp+C
    若D<0,则点P在直线的左侧;若D>0,则点P在直线的右侧;若D=0,则点P在直线上。

    我利用这个算法修正了,确定点在左、中、右三个方向时,原书中的错误。
    但是我继续有这个公式做下一个题,也就是确定点是否在线段上的时候。这个公式又不好使了。
    接下来我截取一下两个题的内容
    见下图

    现在4.31题我的PYTHON程序如下

    p0X, p0Y = eval(input("Enter coordinates for the p0 is x,y "))

    p1X, p1Y = eval(input("Enter coordinates for the p1 is x,y "))

    p2X, p2Y = eval(input("Enter coordinates for the p2 is x,y "))

    a = p1Y - p0Y

    b = p0X - p1X

    c = p1X * p0Y - p0X * p1Y

    d = (a * p2X) + (b * p2Y) +c

    if d < 0:

    print(“p2 is on the left side of the line from p0 to p1”)

    elif d > 0:

    print(“p2 is on the right side of the line from p0 to p1”)

    elif d == 0:

    print(“p2 is on the same line from p0 to p1”)

    4.32题的PYTHON程序如下:

    x0, y0 = eval(input("Enter coordinates for the p0 is x,y "))

    x1, y1 = eval(input("Enter coordinates for the p1 is x,y "))

    x2, y2 = eval(input("Enter coordinates for the p2 is x,y "))

    a = y1-y0

    b = x0-x1

    c = x1 * y0 - x0 * y1

    d = (a * x2) + (b * y2) + c

    if d == 0:

    print(f"{x2, y2} is on the line segment from {x0, y0} to {x1, y1}")

    else:

    print(f"{x2, y2} is not on the line segment from {x0, y0} to {x1, y1}")

    现在的问题是输入4.32题的测试结果,2个结果全都是点在线段上。

    我开始怀疑是不是我的条件有问题。但是从4.31来看d <0 , >0, =0的结果都是对的。所以这个疑问让我有些琢磨不透。

  • 相关阅读:
    hdu2151
    hdu1028
    hdu1398
    hdu1465
    hdu2853
    poj2195
    poj2255
    JS正则校验数字,特殊字符,邮箱基本格式
    JS正则校验数字,特殊字符,邮箱基本格式
    io读取文件内容乱码处理
  • 原文地址:https://www.cnblogs.com/yogaMan/p/13093823.html
Copyright © 2011-2022 走看看