Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding Cross Product to find points above/below a line in MatplotLib

I am trying to draw a line in MatPlotLib , and find all points above or below the line depending on the slope.

After reading this post, and reading about Cross Product, I believe this is the best approach for me to check the points.

I am unsure what would be the most optimal way to implement this in python.

v1 = {x2-x1, y2-y1}   # Vector 1
v2 = {x2-xA, y2-yA}   # Vector 2
xp = v1.x*v2.y - v1.y*v2.x  # Cross product

Has anyone tried implementing something similar ?

Is there some other way for me to find if points on the plot are above or below a line in MatPlotLib ?

like image 433
P Ved Avatar asked Dec 24 '22 15:12

P Ved


1 Answers

As seen e.g. in this question the cross product can indeed be used to determine whether a point lies above or below a line which is defined by two points.

So let a and b be two points defining a line, and p a (set of) point(s) for which we want to know the relative position to the line. Then if

numpy.cross(p-a, b-a) < 0

is True the point(s) lie above the line.

To be accurate here, "above" means that looking from point a towards point b, the point p lies to the left of the line.

This can be used to colorize points in a scatter plot differently depending on whether they're above or below the line, as seem below:

import numpy as np
import matplotlib.pyplot as plt

isabove = lambda p, a,b: np.cross(p-a, b-a) < 0

a = np.array([1,1])
b = np.array([4,3])

p1 = np.array([2,4])
p2 = np.array([3,1])

p = np.array([p1,p2])


fig, (ax,ax2) = plt.subplots(ncols=2, sharex=True, sharey=True)

ax.plot([a[0],b[0]],[a[1],b[1]], marker="o", color="k")
ax.scatter(p[:,0],p[:,1], c=isabove(p,a,b), cmap="bwr", vmin=0, vmax=1)

p = np.random.rand(10,2)*5

ax2.plot([a[0],b[0]],[a[1],b[1]], marker="o", color="k")
ax2.scatter(p[:,0],p[:,1], c=isabove(p,a,b), cmap="bwr", vmin=0, vmax=1)


ax.set_xlim(0,6)
ax.set_ylim(0,6)
plt.show()

enter image description here

like image 70
ImportanceOfBeingErnest Avatar answered Mar 30 '23 00:03

ImportanceOfBeingErnest