<p>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).</p>

<p>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.</p>

<p>One case to consider is when both the coordinates are identical. That is, the end points have the same x coordinate <em>and</em> 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.</p>

<p>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.</p>

CC BY-SA 3.0
2016-03-03T11:41:25.397Z
2016-03-03T11:41:25.397Z
trichoplax is on Codidact now
<p>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. </p>

<p>Any other better way do it is most welcome.</p>

<p>Thanks</p>

CC BY-SA 3.0
2016-03-02T11:57:06.680Z
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?
Dragonseel
CC BY-SA 3.0
2016-03-02T12:26:55.480Z
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.
Martin Ender
CC BY-SA 3.0
2016-03-02T13:01:06.953Z
@MartinBüttner Yes, its also easier to deal with floating point inaccuracy that way. Possibly more readable algo... Sounds like premature optimization to me.
joojaa
<pixel-graphics>
vertical or horizontal line test
sajis997
# vertical or horizontal line test

(Asked by: username_0 on 2016-03-02)

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.

Thanks

**Tags:** pixel-graphics

> **Comments:**
> **username_1**: 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?  
> **username_2**: It may (or may not) be *slightly* faster to subtract the points and check whether one component of the difference is zero. username_1 has a valid question though.  
> **username_3**: @MartinBüttner Yes, its also easier to deal with floating point inaccuracy that way. Possibly more readable algo... Sounds like premature optimization to me.  

---

## 1 answers

### Answer by username_4 on 2016-03-03. Score: 4

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.


---

