Problem:
Intersection

For given two segments s1 and s2, print "1" if they are intersect, "0" otherwise.

s1 is formed by end points p0 and p1, and
s2 is formed by end points p2 and p3.

Input

The entire input looks like:

q (the number of queries)
1st query
2nd query
...
qth query

Each query consists of integer coordinates of end points of s1 and s2 in the following format:

xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3

Output

For each query, print "1" or "0".

Constraints

1 ≤ q ≤ 1000

-10000 ≤ xpi, ypi ≤ 10000

p0 ≠ p1 and
p2 ≠ p3.

Sample Input 1

3
0 0 3 0 1 1 2 -1
0 0 3 0 3 1 3 -1
0 0 3 0 3 -2 5 0

Sample Output 1

1
1
0

Solution in Ruby (judge verdict: Wrong Answer)

class Point
  attr_reader :x, :y
  def initialize(x, y)
    @x = x
    @y = y
  end
end
class Line
  attr_reader :p1, :p2, :maxx, :minx, :maxy, :miny, :x, :y
  def initialize(x1, y1, x2, y2)
    @p1 = Point.new(x1, y1)
    @p2 = Point.new(x2, y2)
    @maxx = (x1 > x2)? x1 : x2
    @minx = (x1 > x2)? x2 : x1
    @maxy = (y1 > y2)? y1 : y2
    @miny = (y1 > y2)? y2 : y1
    @x = x1 - x2
    @y = y1 - y2
  end
  def parallel?(other)
    return (x * other.y) == (x * other.x)
  end
  def over_x?(other)
    return (x ** 2 + other.x ** 2 >= (maxx - other.maxx) ** 2 + (minx - other.mixx) ** 2)
  end
  def over_y?(other)
    return (y ** 2 + other.y ** 2 >= (maxy - other.maxy) ** 2 + (miny - other.miny) ** 2)
  end
  def get_xy(other)
    return (y * other + (x * p2.y - y * p2.x))
  end
  def get_yx(other)
    return (x * other + (y * p2.x - x * p2.y))
  end
  def cross_x?(other)
    return (other.x * p1.y - other.get_xy(p1.x)) * (other.x * p2.y - other.get_xy(p2.x)) <= 0
  end
  def cross_y?(other)
    return (other.y * p1.x - other.get_yx(p1.y)) * (other.y * p2.x - other.get_yx(p2.y)) <= 0
  end
  def intersection?(other)
    if (parallel?(other))
      if (x * y == 0)
        if (x == 0)
          return ((p1.x == other.p1.x) and (over_y?(other)))
        else
          return ((p1.y == other.p1.y) and (over_x?(other)))
        end
      else
        return (cross_x?(other) and over_x?(other))
      end
    else
      if (y == 0 or other.y == 0)
        return (cross_x?(other) and other.cross_x?(self))
      else
        return (cross_y?(other) and other.cross_y?(self))
      end
    end
  end
end

def main(input = STDIN, output = nil)
  q = input.gets.to_i
  q.times do |i|
    arg = input.gets.split(" ").map(&:to_i)
    l1 = Line.new(*arg[0...4])
    l2 = Line.new(*arg[4..-1])
    if (l1.intersection?(l2))
      puts "1"
    else
      puts "0"
    end
  end
end
main