Skip to main content

Check whether two lines A (x1,x2) and B (x3,x4) on the x-axis whether they overlap or not

Project description

check whether two lines (x1,x2) and (x3,x4) on the x-axis do overlap or not

It returns True if they DO overlap, otherwise False

As an example, (1,5) and (2,6) overlaps but not (1,5) and (6,8).

The solution was based on the following schema:

             A0------------------A1
  B0------B1                          B0------B1

      if B1<A0 OR B0>A1 then
         Line B does NOT overlap Line A
      else
         Line B DOES ovelap Line A

Therefore, we need to make sure the A and B are ordered:

def swap(X):
    '''
    this function does make sure the pair is 
    ordered (a,b) where a<b
    (10, -5) would become (-5, 10)
    '''
    if X[0]>X[1]:
        return (X[1],X[0])
    return X

A, B = swap(A), swap(B)

Proposed solution:

def isbovera(A,B):

# Return True if A Line overlaps B Line; False otherwise

A, B = swap(A), swap(B)

return not (B[1]<A[0] or B[0]>A[1])

In order to call this function, please take a look at the testOverlapV0.py

(venv) C:\work\vanhack\venv\Quiz\test_A>python testOverlapV0.py

This program that accepts two lines (x1,x2) and (x3,x4) on the x-axis and returns whether they overlap (True) or not overlap (False)

Enter total number of lines to check, default=1 pair to check

please add 1 lines to be checked - use comma

(1,2),(3,4) should be typed as 1,2,3,4

1,5,2,6

(1.0, 5.0) (2.0, 6.0) True

True means (1,2) and (5,6) do overlap

Let's check the following pair (1,5) and (6,8)

(1,5) and (6,8) should be entered as below:

1,5,6,8

(1.0, 5.0) (6.0, 8.0) False

False means (1,2) and (6,8) do NOT overlap

Note: We are using float although has no mention about it - set/intersection could also handle it but only for int figures

in order to run it as a batch module, a text file should be created with n pairs (comma separeted) as follow:

first line, number of pair to check second line next pair to check and so on

ex:

4

13 , -6 , -8 , 3

-15 , -6 , 4 , -11

-4 , -2 , 0 , -20

-18 , 9 , 10 , 12

** please check testsample.txt **

python testOverlapV0.py <testsample.txt

This program that accepts two lines (x1,x2) and (x3,x4) on the x-axis and returns whether they overlap Enter total number of lines to check, default=1 pair to check please add 100 lines to be checked - use comma (1,2),(3,4) should be typed as 1,2,3,4

(13.0, -6.0) (-8.0, 3.0) True

(-15.0, -6.0) (4.0, -11.0) True

(-4.0, -2.0) (0.0, -20.0) True

(-18.0, 9.0) (10.0, 12.0) False

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

isover10-0.1.5-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file isover10-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: isover10-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.0

File hashes

Hashes for isover10-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 27f6983d0ee8b5b24b692d727d7eae3bebbdbabc97fbb870cd5a82ebcfee0b9f
MD5 83b6fb488105d5d65d2fa2714718e42f
BLAKE2b-256 f49de9b44a5df195349aedfe485623485a5bbb06945b2ea36cb78c903ba145ab

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page