I have a line segment defined by two coordinate values and I need to check if the line segment is parallel to the x-axis or y-axis. I believe that it is not that difficult to find. When it comes to test for parallelism against x-axis, we just need to check if the both the end points of the line segments has the equivalent y-value, whereas parallelism test against y-axis can be found if both the coordinate point ends have the same x-value.

Any other better way do it is most welcome.


  • 3
    $\begingroup$ I don't really think you can be more efficient than a equality check for one coordinate of two points. And as far as I see this check is correct. So... What is your issue with this solution? $\endgroup$
    – Dragonseel
    Mar 2, 2016 at 11:57
  • 2
    $\begingroup$ It may (or may not) be slightly faster to subtract the points and check whether one component of the difference is zero. Dragonseel has a valid question though. $\endgroup$ Mar 2, 2016 at 12:26
  • 1
    $\begingroup$ @MartinBüttner Yes, its also easier to deal with floating point inaccuracy that way. Possibly more readable algo... Sounds like premature optimization to me. $\endgroup$
    – joojaa
    Mar 2, 2016 at 13:01
  • $\begingroup$ You will get the most appropriate answers by tell us why you want to detect such segments. $\endgroup$ Mar 4, 2016 at 9:03

1 Answer 1


It is not generally useful to try to optimise small parts of a program before you have profiled the program to see where the most benefit can be gained by optimisation. Make sure everything is correct and working first, and then profile to decide where to optimise (if optimisation turns out to be necessary).

Mathematically, your approach is already correct for all lines. It will correctly identify all lines parallel to either axis, and it will not incorrectly identify any lines that are not parallel to an axis. However, for use in a practical setting, it's worth asking yourself about near-parallel cases and any odd exceptions. Consider all this before even thinking about optimisation, otherwise you may have to repeat all the optimisation later if something you have to change for correctness breaks your initial optimisation.

One case to consider is when both the coordinates are identical. That is, the end points have the same x coordinate and the same y coordinate. This is a degenerate case that doesn't define a line (it could have any direction). Check your implementation with this case and make sure it gives the output that you want it to.

Depending on your purpose, you may want to detect only exactly parallel lines, or you may want to detect lines that are sufficiently near to parallel. In some cases lines near to parallel may satisfy your purpose, and in other cases it may be essential to accept lines near to parallel, if for example there is the possibility of small inaccuracies in the coordinates.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.