Read Coordinate Points and Determine Its Quadrant in Haskell



This tutorial will help us in reading the x and y coordinates and determine its quadrant. If both the coordinates are positive, the point lies in first quadrant; if x coordinate is positive and y coordinate is negative, the point lies in fourth quadrant ; if x coordinate is negative and y coordinate is positive, the point lies in second quadrant and if both the coordinates are negative, then point lies in third quadrant.

Algorithm

  • Step 1 ? The quadrant function is defined using certain conditions on x and y coordinates.

  • Step 2 ? Program execution will be started from main function. The main() function has whole control of the program.

  • Step 3 ? The variables named, "x" and "y" are initialized. It will contain the coordinates value whose quadrant is to be determined.

  • Step 4 ? The final resultant quadrant is displayed by using ?putStrLn' statement.

Using User Defined Function

In this example, we create a function called quadrant that takes a tuple of the x and y coordinates as input and returns a string indicating which quadrant the point lies in.

Example 1

quadrant :: (Float, Float) -> String
quadrant (x, y)
   | x > 0 && y > 0 = "First Quadrant"
   | x < 0 && y > 0 = "Second Quadrant"
   | x < 0 && y < 0 = "Third Quadrant"
   | x > 0 && y < 0 = "Fourth Quadrant"
   | x == 0 || y == 0 = "On the Axis"

main = do
   let x = -2
   let y = 4
   putStrLn $ "Point lies in " ++ quadrant (x, y)

Output

Point lies in Second Quadrant

Using atan2 Function

In this example, we use the atan2 function to compute the angle in radians between the positive x-axis and the line connecting the point to the origin. Using this angle, we can determine in which quadrant the point lies.

Example 2

quadrant :: (Float, Float) -> String
quadrant (x, y)
   | angle > 0 && angle < pi/2 = "First Quadrant"
   | angle > pi/2 && angle < pi = "Second Quadrant"
   | angle > pi && angle < 3*pi/2 = "Third Quadrant"
   | angle > 3*pi/2 && angle < 2*pi = "Fourth Quadrant"
   | x == 0 && y == 0 = "Origin"
   | x == 0 || y == 0 = "On the Axis"
   where angle = atan2 y x
main = do
   let x = 3
   let y = 2
   putStrLn $ "Point lies in " ++ quadrant (x, y)

Output

Point lies in First Quadrant

Using the Sign of x and y Coordinates

In this example, it checks the sign of the x and y coordinates, and using this, it determines the quadrant in which the point lies. It also handle the cases where the point is on x or y axis or the origin.

Example 3

quadrant :: (Float, Float) -> String
quadrant (x, y)
   | x > 0 && y > 0 = "First Quadrant"
   | x < 0 && y > 0 = "Second Quadrant"
   | x < 0 && y < 0 = "Third Quadrant"
   | x > 0 && y < 0 = "Fourth Quadrant"
   | x == 0 && y == 0 = "Origin"
   | x == 0 = "On the x-axis"
   | y == 0 = "On the y-axis"

main = do
   let x = -7
   let y = -5
   putStrLn $ "Point lies in " ++ quadrant (x, y)

Output

Point lies in Third Quadrant

Conclusion

In Haskell, there are various ways to read the x and y coordinates and determine their quadrant. These ways can be implemented by using the quadrant function, atan2 function, or by checking the sign of x and y coordinates.

Updated on: 2023-01-23T11:14:11+05:30

248 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements