-1

I'm trying to do this assignment:

Write a Python function crosses(), to determine whether the line formed between 2 points (x1,y1) and (x2,y2) crosses the x- and/or y-axis.

The function prompts for the 4 values – x1, y1, x2 and y2. It outputs one of the following messages:

Line crosses both x and y axes
Line crosses x axis only
Line crosses y axis only
Line does not cross both x and y axes

I read my lecture notes but it does not cover this topic. Is there any formula I can use to calculate?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Fufu Alex
  • 37
  • 1
  • 1
  • 9
  • 4
    What have you tried so far? – DirtyBit Feb 07 '19 at 09:38
  • 3
    Possible duplicate of [How do you detect where two line segments intersect?](https://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect) – mkrieger1 Feb 07 '19 at 09:39
  • this question is one line segment? – Fufu Alex Feb 07 '19 at 09:40
  • One line segment... and a line (one of the axes) ... which is a very long line segment. – mkrieger1 Feb 07 '19 at 09:42
  • 2
    Get a piece of paper. Draw a coordinate system with x and y axis. Ponder what it means that a line crosses the x axis. Ponder what it means that a line crosses the y axis. All you need are a few boolean expressions, not a formula. There might be interesting special cases if a line is on an axis without crossing it. – Roland Weber Feb 07 '19 at 09:43
  • 1
    Is no one going to mention that SO is not a homework site? – Jarvis Feb 07 '19 at 09:45
  • @Jarvis this is not a homework. is a revision question which i trying to understand – Fufu Alex Feb 07 '19 at 09:48
  • "*I'm trying to do this assignment:*" .. seems like homework to me. Quoting from Wikipedia, *Homework, or a homework assignment, is a set of tasks assigned to students by their teachers to be completed outside the class.* – Jarvis Feb 07 '19 at 09:49
  • even i don't write that you will still say its a homework to you. Ok u win :) – Fufu Alex Feb 07 '19 at 10:00
  • @KangChul why don't you try below answer instead of counting win and lose :D – Vikas Periyadath Feb 07 '19 at 10:05

2 Answers2

1

This should be fairly simple since you only need to check if it crosses the x-axis or the y-axis. You can just check if any one of your xs or ys shifts from positive to negative or negative to positive.

def intersects_axis(v1, v2):
  return (v1 <= 0 <= v2 or v2 <= 0 <= v1)


def determine_intersections(x1, y1, x2, y2):

  print("Checking if {}, {} and {}, {} any axes".format(x1, y1, x2, y2))

  intersects_y = intersects_axis(y1, y2)
  intersects_x = intersects_axis(x1, x2)

  if intersects_y and intersects_x:
    print("Line crosses both x and y axes")
  elif intersects_y:
    print("Line crosses y axis only")
  elif intersects_x:
    print("Line crosses x axis only")
  else:
    print("Line does not cross both x and y axes")


if __name__ == "__main__":

  x1, y1 = 1, 1

  x2, y2 = 2, 2

  determine_intersections(x1, y1, x2, y2)

  x2, y2 = 1, -1

  determine_intersections(x1, y1, x2, y2)

  x2, y2 = -1, -1

  determine_intersections(x1, y1, x2, y2)

  x2, y2 = -1, 1

  determine_intersections(x1, y1, x2, y2)

Which will give you:

Checking if 1, 1 and 2, 2 any axes
Line does not cross both x and y axes
Checking if 1, 1 and 1, -1 any axes
Line crosses y axis only
Checking if 1, 1 and -1, -1 any axes
Line crosses both x and y axes
Checking if 1, 1 and -1, 1 any axes
Line crosses x axis only
fixatd
  • 1,394
  • 1
  • 11
  • 19
0

Is problem is really about segments, not lines: some simple bit magic

def crossesaxes(x1,y1,x2,y2):
    q1 = (1 if x1 < 0 else 0) | (2 if y1 < 0 else 0)
    q2 = (1 if x2 < 0 else 0) | (2 if y2 < 0 else 0)
    q = q1 ^ q2
    answers = ['no crossing', 'x', 'y', 'crosses both']
    return answers[q]

(It is not specified whether segment with end point on axis intersects it)

MBo
  • 77,366
  • 5
  • 53
  • 86