Getting Started with Python Exercises (76) - OpenJudge one hundred exercises: to determine whether multiple points on the same straight line

OpenJudge one hundred training exercises No. 4072: determining whether a plurality of points on the same line

Title Description

Source
OpenJudge website - one hundred exercises set - No. 4072 exercises

Requires
the total time limit: 1000ms Memory Limit: 65536kB

description

There are N (1 <= n <= 100) <span = ""> two do not overlap each point, given their coordinates (xi, yi), and asked whether these points are collinear.

Input
The first line is the number of test group T (1 <= T <= 100), followed by T sets of data, each data line is the first number N of the set of data points, followed by N rows, each row represents point coordinates, constituted by two numbers separated by a space between the two figures.
Output
T-rows, each row corresponds to a set of data input, if the set of data points in the same line, the line output True, otherwise output False.
Sample input
. 1
. 3
0 0
2 2
. 1. 1
Sample Output
True

Problem-solving ideas

  1. Trigonometric functions studied people answer this question when more natural to expect a consistent approach to determine whether the slope. However, this approach has had two shortcomings: First, the slope may be infinite, the computer can not exist. Second, when two coordinate points fall on the vertical line, dx is equal to 0.
  2. The above workaround at such a large slope when, instead reciprocal judgment whether the slope of a consistent approach. The slope of this added work, code logic becomes more complex, and in fact, since the coordinate points may be any combination of two, there are two points into a straight line even possible infinite slope, there are two points of a straight line connecting the reciprocal of infinity, the problem still exists.
  3. Our approach is adopted and whether the product of the sine of the angle cosine is determined abscissa axis coincides with the straight line. Analyzing only determines whether the sine or cosine values ​​coincide only is not enough.
  4. This question is a pit, that is, the coordinates of a point only when considered to be in the same line. On this point, not a hundred percent sure.

Answers

import math
#points内的点处在同一条直线上吗?
#points内至少有3个点。
def on_one_line(points):
    delta_x = points[1][0] - points[0][0]
    delta_y = points[1][1] - points[0][1]
    distance_square = delta_x * delta_x + delta_y * delta_y
    sin_times_cos = delta_x * delta_y/ distance_square

    for j in range(2, point_num):
        dx = points[j][0] - points[0][0]
        dy = points[j][1] - points[0][1]
        if math.fabs(dx * dy / (dx * dx + dy * dy) - sin_times_cos) > 10 ** -9:
            return False

    return True

T = int(input())
for i in range(T):
    point_num = int(input())
    points = [[int(s) for s in input().split()] for j in range(point_num)]
    if point_num == 2:
        print("True")
        continue
    elif point_num == 1:
        print("True")
        continue

    if on_one_line(points):
        print("True")
    else:
        print("False")

Test Case

  1. Title description given test coordinate points are in the first quadrant of the coordinate system.

  2. Coordinates of the points fall on a horizontal straight line.
    Sample input
    . 1
    . 5
    0 0
    2 0
    . 1 0
    -10 0
    22 is 0
    Sample Output
    True

  3. Coordinate points fall on the vertical line.
    Sample input
    . 1
    . 5
    -1 0
    -1 10
    -1 20 is
    -1 33 is
    -1 -8
    sample output
    True

  4. Coordinate point not aligned.
    Sample input
    . 1
    . 5
    -1 0
    -2 10
    20 is -1
    0 33 is
    -1 -8
    Sample Output
    False

  5. Two sets of test data.
    Sample input
    2
    . 3
    -1 0
    -2 10
    -1 20 is
    . 3
    0 0
    2 2
    . 1. 1
    Sample Output
    False
    True

  6. A straight line dots.
    Sample input
    . 1
    . 1
    -1 0
    Sample Output
    True

  7. Two points must be configured linearly.
    Sample input
    . 1
    2
    -1 0
    44 is 99
    sample output
    True

  8. Some points fall inclined 45 degrees, some point falls linearly inclined -45 degrees.
    Sample input
    . 1
    . 5
    0 0
    . 1. 1
    -1. 1
    2 2
    2 -2
    Sample Output
    Fasle

  9. Coordinate points fall line inclined 45 degrees.
    Sample input
    . 1
    . 5
    0 0
    . 1. 1
    -1 -1
    2 2
    -2 -2
    Sample Output
    True

summary

  1. Determining whether the product of the sine and cosine of the same slope coincides better than judgment.
  2. Determining whether the set of points in the course of a same straight line as a function of extraction, is a good practice.
Published 132 original articles · won praise 89 · views 330 000 +

Guess you like

Origin blog.csdn.net/yedouble/article/details/104691368