Collinearity | Programming Praxis
Maybe your like
A collection of etudes, updated weekly, for the education and enjoyment of the savvy programmer
Collinearity
June 17, 2014
Beware: today’s exercise sounds simple but is actually quite complex if you don’t look at it properly.
Your task is to write a function that takes three points in the x,y plane and determines if they are collinear; be sure to handle vertical lines and horizontal lines properly. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
Share this:
- X
Related
Pages: 1 2
Posted by programmingpraxis Filed in Exercises 9 Comments »9 Responses to “Collinearity”
- Paul said June 17, 2014 at 10:29 AM
In Python.
def is_collinear(x1, y1, x2, y2, x3, y3): return x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2) == 0 assert is_collinear(0, 0, 1, 0, 2, 0) assert is_collinear(0, 0, 0, 1, 0, 2) assert is_collinear(0, 0, 1, 1, 2, 2) assert not is_collinear(0, 0, 1, 1, 2, 3) - Michael ⌨ Kamau said June 17, 2014 at 10:32 AM
My simple solution uses the fact that point lying on the same line form a triangle of area zero. This code just does that.
import math class Point: def __init__(self,x,y): self.x = x; self.y = y; def distance(p1,p2): return math.sqrt(math.pow(p1.x - p2.x, 2) + math.pow(p1.y - p2.y, 2)) def getArea(pa,pb,pc): ''' Uses Heron's Formula ''' a = distance(pb,pc) b = distance(pa,pc) c = distance(pa,pb) s = (a +b +c )/2 return math.sqrt(s*(s-a)*(s-b)*(s-c)) def is_Collinear(p1,p2,p3): ''' Collinear points form a traingle of area zero ''' return getArea(p1,p2,p3) == 0 - Rutger said June 17, 2014 at 11:37 AM
Using area of triangle is 0, includes duplicate points and avoids zero division.
def collinear(x1,y1,x2,y2,x3,y3): return x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2)==0 assert collinear(0,0, 0,0, 0,0) assert collinear(1,2, 1,3, 1,4) assert collinear(1,3, 2,2, 3,1) assert collinear(1,1, 2,2, -3,-3) - Jussi Piitulainen said June 17, 2014 at 1:45 PM
(define re real-part) (define im imag-part) (define mag magnitude) (define (dot u w) (+ (* (re u) (re w)) (* (im u) (im w)))) (define (collinearish? u v w) (let ((uv (- u v)) (uw (- u w)) (eps 1e-14)) (or (< (mag uv) eps) (< (mag uw) eps) (< (abs (- (cos 0) (abs (/ (dot uv uw) (mag uv) (mag uw))))) eps))))
A complex solution, pun intended, to within an epsilon: representing points as rectangular numbers in the obvious way, then computing the cosine of the angle between two vectors. (Or if at least one of the two vectors is null, the three points are at most two, and so collinear.)
- pbo said June 17, 2014 at 2:27 PM
If A, B and C are collinear, the vector product of AB and AC is zero.
bool collinear(x1, y1, x2, y2, x3, y3) { return ((x2 – x1)*(y3 – y1) – (x3 – x1)*(y2 – y1) == 0); }
- matthew said June 18, 2014 at 12:33 PM
I like pbo’s solution – it’s equivalent to taking AB and BC, rotating BC 90 degrees and taking the dot product. This gives |AB| *
We can extend to a nice floating point solution, for consistency (and to minimize rounding errors) we will take all three difference vectors and work with the two longest ones (this also deals nicely with have two of the points very close to each other). Having calculated the dot product, we then scale by the length of the longest vector to get a dimensionless indication of collinearity (so scaling up all the points by a constant factor doesn’t affect the result). For simplicity, we use the taxicab metric for computing vector lengths. (Needs C++11 for auto).
#include <iostream> template <typename T>T square(T x) { return x*x; } template <typename T>T abs(T x) { return (x>=0) ? x : -x; } template <typename T> struct Vector { Vector(T x_, T y_) : x(x_), y(y_){} Vector(const Vector &p) : x(p.x), y(p.y) {} friend Vector operator-(Vector p, Vector q) { return Vector(p.x-q.x, p.y-q.y); } T taxi() { return abs(x) + abs(y); } // Max would do too T x; T y; }; template <typename T> T check(Vector<T> p, Vector<T> q, Vector<T> r) { Vector<T> a = p-q; Vector<T> b = q-r; Vector<T> c = r-p; // Sort a,b,c by taxi metric // Numerically more stable and we will should consistent // results regardless of point order. if (a.taxi() < b.taxi()) std::swap(a,b); if (b.taxi() < c.taxi()) std::swap(b,c); if (a.taxi() < b.taxi()) std::swap(a,b); // This is the perpendicular distance of point b from line a, // scaled by the taxi length of a. Since taxi length is no more // that sqrt(2) larger than the true length, this is fine. T res = abs(a.x*b.y - a.y*b.x)/square(a.taxi()); return res; } Vector<float> rvector(float x, float y) { return Vector<float>(x,y); } int main(int argc, char *argv[]) { { float x0 = 0.3333333; float y0 = 0.4444444; float k = 0.12345; auto p = rvector(1*k*x0,1*k*y0); auto q = rvector(2*k*x0,2*k*y0); auto r = rvector(3*k*x0,3*k*y0); auto s = rvector(3*k*x0,2*k*y0); std::cout << check(p,q,r) << "\n"; std::cout << check(q,r,p) << "\n"; std::cout << check(r,p,q) << "\n"; std::cout << check(p,q,s) << "\n"; std::cout << check(q,s,p) << "\n"; std::cout << check(s,p,q) << "\n"; } { float x = 1.23456789e10;; for (float k = 9.87654321e-10; k < 1e10; k *= 100) { auto p = rvector(0,0); auto q = rvector(k*x,k); auto r = rvector(2*k*x,0); std::cout << check(p,q,r) << "\n"; } } { float x = 1e-10;; auto p = rvector(x,x); auto q = rvector(-x,x); auto r = rvector(1,1); std::cout << check(p,q,r) << "\n"; } } - John Lekberg said June 18, 2014 at 2:06 PM
Here is a Racket Solution. I use the fact that three collinear points produce a triangle of area 0. My solution uses an epsilon value to take into account error propagation in floating point, so it is a probabilistic method, although very accurate.
#lang racket (define (point x y) (cons x y)) (define (x point) (car point)) (define (y point) (cdr point)) (define (distance p1 p2) (sqrt (+ (sqr (- (x p1) (x p2))) (sqr (- (y p1) (y p2)))))) (define epsilon 0.001) (define (collinear? p1 p2 p3) (let ((a (distance p1 p2)) (b (distance p2 p3)) (c (distance p3 p1))) (let ((s (/ (+ a b c) 2))) (< (sqrt (foldr * 1 (map (lambda (x) (- s x)) (list 0 a b c)))) epsilon)))) - treeowl said June 22, 2014 at 8:45 AM
I used the same approach as pbo:
Codepad
Attempt to embed:
module Collinear (collinear) where
(.-) :: Num a => (a, a) -> (a, a) -> (a, a)
(x1, x2) .- (y1, y2) = (x1 – y1, x2 – y2)
cross :: Num a => (a, a) -> (a, a) -> a
(x1, x2) `cross` (y1, y2) = x1 * y2 – x2 * y1
collinear :: (Num a, Eq a) => (a, a) -> (a, a) -> (a, a) -> Bool
collinear v1 v2 v3 = (v1 .- v2) `cross` (v1 .- v3) == 0
- Subhranshu said July 7, 2014 at 11:04 AM
Javascript Code…
/*input*/ var points = [ { x : 5, y : 5 }, { x : 15, y : 15 }, { x : 40, y : 40 } ];
/*logic*/ function isCoLinear(points){ return (Math.atan( Math.abs((points[1][“y”] – points[0][“y”])/(points[1][“x”] – points[0][“x”])) ) * (180/Math.PI)) === (Math.atan( Math.abs((points[2][“y”] – points[0][“y”])/(points[2][“x”] – points[0][“x”])) ) * (180/Math.PI)) }
isCoLinear(points);
Leave a comment
Categories
- Administrivia
- Exercises
Archives
- December 2021
- November 2021
- October 2021
- September 2021
- June 2021
- May 2021
- April 2021
- March 2021
- February 2021
- January 2021
- December 2020
- November 2020
- October 2020
- September 2020
- August 2020
- July 2020
- June 2020
- May 2020
- April 2020
- March 2020
- February 2020
- January 2020
- December 2019
- November 2019
- October 2019
- September 2019
- August 2019
- July 2019
- June 2019
- May 2019
- April 2019
- March 2019
- February 2019
- January 2019
- November 2018
- October 2018
- September 2018
- August 2018
- July 2018
- June 2018
- May 2018
- April 2018
- March 2018
- February 2018
- January 2018
- December 2017
- November 2017
- October 2017
- September 2017
- August 2017
- July 2017
- June 2017
- May 2017
- April 2017
- March 2017
- February 2017
- January 2017
- December 2016
- November 2016
- October 2016
- September 2016
- August 2016
- July 2016
- June 2016
- May 2016
- April 2016
- March 2016
- February 2016
- January 2016
- December 2015
- November 2015
- October 2015
- September 2015
- August 2015
- July 2015
- June 2015
- May 2015
- April 2015
- March 2015
- February 2015
- January 2015
- December 2014
- November 2014
- October 2014
- September 2014
- August 2014
- July 2014
- June 2014
- May 2014
- April 2014
- March 2014
- February 2014
- January 2014
- December 2013
- November 2013
- October 2013
- September 2013
- August 2013
- July 2013
- June 2013
- May 2013
- April 2013
- March 2013
- February 2013
- January 2013
- December 2012
- November 2012
- October 2012
- September 2012
- August 2012
- July 2012
- June 2012
- May 2012
- April 2012
- March 2012
- February 2012
- January 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- May 2011
- April 2011
- March 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
| 29 | 30 |
Archives
- December 2021
- November 2021
- October 2021
- September 2021
- June 2021
- May 2021
- April 2021
- March 2021
- February 2021
- January 2021
- December 2020
- November 2020
- October 2020
- September 2020
- August 2020
- July 2020
- June 2020
- May 2020
- April 2020
- March 2020
- February 2020
- January 2020
- December 2019
- November 2019
- October 2019
- September 2019
- August 2019
- July 2019
- June 2019
- May 2019
- April 2019
- March 2019
- February 2019
- January 2019
- November 2018
- October 2018
- September 2018
- August 2018
- July 2018
- June 2018
- May 2018
- April 2018
- March 2018
- February 2018
- January 2018
- December 2017
- November 2017
- October 2017
- September 2017
- August 2017
- July 2017
- June 2017
- May 2017
- April 2017
- March 2017
- February 2017
- January 2017
- December 2016
- November 2016
- October 2016
- September 2016
- August 2016
- July 2016
- June 2016
- May 2016
- April 2016
- March 2016
- February 2016
- January 2016
- December 2015
- November 2015
- October 2015
- September 2015
- August 2015
- July 2015
- June 2015
- May 2015
- April 2015
- March 2015
- February 2015
- January 2015
- December 2014
- November 2014
- October 2014
- September 2014
- August 2014
- July 2014
- June 2014
- May 2014
- April 2014
- March 2014
- February 2014
- January 2014
- December 2013
- November 2013
- October 2013
- September 2013
- August 2013
- July 2013
- June 2013
- May 2013
- April 2013
- March 2013
- February 2013
- January 2013
- December 2012
- November 2012
- October 2012
- September 2012
- August 2012
- July 2012
- June 2012
- May 2012
- April 2012
- March 2012
- February 2012
- January 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- May 2011
- April 2011
- March 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- Comment
- Reblog
- Subscribe Subscribed
-
Programming Praxis Join 820 other subscribers Sign me up - Already have a WordPress.com account? Log in now.
-
-
-
Programming Praxis - Subscribe Subscribed
- Sign up
- Log in
- Copy shortlink
- Report this content
- View post in Reader
- Manage subscriptions
- Collapse this bar
-
Tag » Collinear Vectors Python
-
(Python) Script To Determine If (x, Y) Coordinates Are Colinear
-
Check If Two Vectors Are Collinear Or Not - GeeksforGeeks
-
Program To Check If Three Points Are Collinear - GeeksforGeeks
-
Checking If Two Vectors Are Colinear - Blender Artists Community
-
Find Clusters Of Collinear Points From A Given Set Of Data Points
-
Statistics In Python — Collinearity And Multicollinearity
-
A Python Library To Remove Collinearity | By Gianluca Malato
-
Python Program To Check If Three Points Are Collinear - BTech Geeks
-
Complete Guide To Vectors In Linear Algebra With Implementation ...
-
Vector-shortcuts · PyPI
-
oss — NumPy V1.23 Manual
-
Python Script To Determine If X Y Coordinates Are Colinear Getting ...
-
Types Of Vectors: Collinear And Equal Vectors, Videos ... - Toppr
-
Python: Test If Two Segments Are Roughly Collinear (on The Same Line)