Given a point in a Euclidean plane (x
and y
), return the quadrant
the point exists in: 1
, 2
, 3
or 4
(integer). x
and y
are non-zero integers, therefore the given point never
lies on the axes.
(1, 2) => 1
(3, 5) => 1
(-10, 100) => 2
(-1, -9) => 3
(19, -56) => 4
Quadrants
There are four quadrants:
- First quadrant, the quadrant in the top-right, contains all points with positive X and Y
- Second quadrant, the quadrant in the top-left, contains all points with negative X, but positive Y
- Third quadrant, the quadrant in the bottom-left, contains all points with negative X and Y
- Fourth quadrant, the quadrant in the bottom-right, contains all points with positive X, but negative Y
More on quadrants: Quadrant (plane geometry) - Wikipedia
def quadrant(x, y)
if x.positive?
if y.positive?
return 1
end
4
elsif y.positive?
2
else
3
end
end
def quadrant(x, y)
return 1 if x.positive? && y.positive?
return 2 if x.negative? && y.positive?
return 3 if x.negative? && y.negative?
return 4 if x.positive? && y.negative?
end