Intersections for 3D lines - python

I have written a function that should calculate all intersectionpoints between a line and all lines that are given to it, and this in 3D. I have these lines parametrized because that seemed to be the easiest way to work with things. The problem is that when I input the variables "t1" and "t2" back into the functions of the lines, there seems to be an inaccuracy that is too big to be acceptable for the thing that I need.
t1 is the parameter for the line of which you would like to know all intersections, so it's written in this form:
x = xo + t1 * dx
y = yo + t1 * dy
z = zo + t1 * dz
Where [xo, yo, zo] represent a point on the line that I call the "origin" and [dx, dy, dz] represents the direction of that line. The other lines are given in the same form and the function I wrote basically solves the following equation:
xo1 + t1 * dx1 = xo2 + t2 * dx2
yo1 + t1 * dy1 = yo2 + t2 * dy2
zo1 + t1 * dz1 = zo2 + t2 * dz2
Where everything is given except for t1 and t2, that's what I'm looking for here. However, I don't think actually finding t1 and t2 is the problem, I do have a solution that gives me some kind of result. As mentioned earlier, the problem is really that when I feed t1 and t2 back into these formulas to get the actual intersectionpoints, that they differ slightly from eachother. I'm talking about differences that are mostly 0.005-0.05 away from eachother in euclidean distance. But in extreme cases it could be up to 0.5 inaccuracy. I am aware that most lines in 3D do not intersect and therefore do not have a solution to these equations, but for the tests that I'm doing right now, I am 100% sure that all of the lines are within the same plane, but some might be parallel to each other. However, these inaccuracies occur for all lines, and I'm really just looking for a solution that gets it accuratly when they do intersect.
Here's the code I have for this:
def lineIntersection(self, lines):
origins = np.zeros((len(lines), 3), dtype=np.float32)
directions = np.zeros((len(lines), 3), dtype=np.float32)
for i in range(0, len(lines)):
origins[i] = lines[i].origin
directions[i] = lines[i].direction
ox = origins[:, 0]
oy = origins[:, 1]
dx = self.origin[0]
dy = self.origin[1]
x1 = directions[:, 0]
y1 = directions[:, 1]
x2 = self.direction[0]
y2 = self.direction[1]
t2 = np.divide((np.subtract(np.add(oy, np.multiply(np.divide(np.subtract(dx, ox), x1), y1)), dy)), np.subtract(y2, np.multiply(np.divide(x2, x1), y1)))
t1 = np.divide((np.add(dx, np.subtract(np.multiply(t2, x2), ox))), x1)
testx1 = np.add(ox, np.multiply(t1, x1))
testx2 = np.add(dx, np.multiply(t2, x2))
testy1 = np.add(oy, np.multiply(t1, y1))
testy2 = np.add(dy, np.multiply(t2, y2))
testz1 = np.add(origins[:, 2], np.multiply(t1, directions[:, 2]))
testz2 = np.add(self.origin[2], np.multiply(t2, self.direction[2]))
arr1 = np.array([testx1, testy1, testz1]).T
arr2 = np.array([testx2, testy2, testz2]).T
diff = np.linalg.norm(np.subtract(arr1, arr2), axis=1)
narr = arr1[diff < 0.05] #Filtering out points that aren't actually intersecting
nt2 = t2[diff < 0.05]
return narr, nt2
This function is located in the "Line" class and has an origin and direction as explained earlier. The input it takes, is an array of objects from the "Line" class.
So to be clear, I'm asking why this doesn't seem to work as precise as I want it to be and how I can fix it. Or, if there are alternatives to calculating intersectionpoints that are really accurate, I would love to hear about it.

Inaccuracy is common case for intersection of lines forming small angle.
I did not checked your algo correctness, but seems you just solve system of three equation with linalg solver.
In case of almost parallel lines intermediate values (determinant) might be small causing significant errors.
Have you tried more robust numeric algorithms like SVD?
But perhaps you really don't need them:
Note that when you are sure that all lines lie in the same plane, you can exploit 2D algorithm - just check what component of dx,dy,dz have the smallest magnitude (check for some distinct lines) and ignore corresponding component - it is similar to projecting of lines onto OXY or OXZ or OYZ plane. 2D code should be much simpler.
For true 3D case there is well-tested vector approach intended to find distance (the shortest line segment) between two skew lines - it is just zero length for intersecting ones. Example here.
Note that det (determinant) magnitude is evaluated to check for parallel (and almost parallel) lines too.

Related

numpy strange results in solving an inequality

I have written an inequality by two formats the first shape is converting to a polynomial
the polynomial and the matrix multiplication are exactly the same
V0j=yj.T # P # yj=12137.5 * (y1**2) + 11438.7 * (y2**2) +(26.381 * 2)*y1*y2
and also I have the next the constant value
lambdaprimezero*nrmedoe0+minelement=8.920678
this means that polynomial inequality and the next inequality are the same and must have the same answer
yj.T # P # yj-lambdaprimezero*nrmedoe0<minelement
and with the help of a friend I could extract the points in which polynomial inequality is valid (first program)
The problem occurs when I use the answer of the firs program (polynomial inequality) in the matrix one
As the are the same the send program must return small enough at these points but it does not
#first part
import numpy as np
# 12137.5x^2 + 11438.7y^2 + (26.381*2)xy = 0.000731
Y1 = np.linspace(-0.003, 0.003, num=100)
Y2 = np.linspace(-0.003, 0.003, num=100)
pointsInsideEllipse = []
for y1 in Y1:
for y2 in Y2:
if 12137.5 * (y1**2) + 11438.7 * (y2**2) +(26.381 * 2)*y1*y2 < 8.920678:
pointsInsideEllipse.append([y1, y2])
#print(pointsInsideEllipse)
y=[y1, y2]
P=np.array([[12137.5, 26.381], [26.381,11438.7]])
yj=np.array(y)
pointset=np.array(pointsInsideEllipse)
def msquarefunc(yj):
VALUE=yj.T # P # yj
return VALUE
point=pointsInsideEllipse
for point in pointset:
if msquarefunc(point)<8.920678:
############################################
###########################
# second program(matrix form)
V0j=yj.T # P # yj
testfeaturej=V0j-lambdaprimezero*nrmedoe0
#print(V0j)
#print(lambdaprimezero*nrmedoe0)
cj=abs(testfeaturej)
print(cj)
if cj<=minelement:
print('small enough')
What is the problem?
You used y=[y1, y2] while it should be y=[Y1, Y2]. Moreover, note that point=pointsInsideEllipse is useless since point is set by the following loop. The biggest issue comes from the fact that the first hypothesis is actually wrong. Indeed, yj.T # P # yj is not equal to 12137.5 * (y1**2) + 11438.7 * (y2**2) +(26.381 * 2)*y1*y2. I do not see why it would be actually true. The y1 values multiplied seems not the same in one case while they are with the second.
Note that the rest of the code is not clear and incomplete and so hard to test/run.
You can use vectorized calls to make your code faster and more clear with Numpy. More over you can easily test that the equality hypothesis is wrong using the following code:
Y1 = np.linspace(-0.003, 0.003, num=100)
Y2 = np.linspace(-0.003, 0.003, num=100)
yj = np.vstack([Y1, Y2])
V0j_1 = yj.T # P # yj
V0j_2 = 12137.5 * (Y1.reshape(-1,1)**2) + 11438.7 * (Y2**2) + (26.381*2)*np.outer(Y1,Y2)
# Another (less efficient) way of computing V0j_2:
#V0j_2_bis = np.array([[12137.5 * (y1**2) + 11438.7 * (y2**2) +(26.381 * 2)*y1*y2 for y2 in Y2] for y1 in Y1])
#print(np.allclose(V0j_2, V0j_2_bis)) # True
distance = (V0j_1 - V0j_2)**2
print(np.allclose(V0j_1, V0j_2)) # False
print(distance)
# This shows the two matrices have different symmetries (and so the assumption is wrong)
print(np.allclose(V0j_1, V0j_1.T)) # True
print(np.allclose(V0j_2, V0j_2.T)) # False
The distance matrix show huge difference (that are unrelated with floating-point errors). Actually some values of V0j_1 and V0j_2 are even note of the same sign.

How to compare 2 distances in Python, Pygame with lines to create a box?

So what I want in theory is pretty simple. I want create something where you:
have 2 lines that the user draws
have code that take the first line and take the 2 points from it
take the other lines 2 points
takes the first point from the first line and compares it to the 2 points from (let's call it) line 2
does the same for the 2nd point of line 1
and draws line connecting the points the first point of line 1 to the closest point of line 2, then drawing the 2nd point of line one to the last one (of line 2).
Sounds a bit complicated, but probably because I'm bad a explaining things.
The code I have for this is here
I also just want to add that the function "draw_line()" just takes the inputs of "start_pos", and "end_pos" so it doesn't matter what order the inputs are put in. Also "y2, y1" is the starting point or, 1st, and z is the 2nd.
def find_closer_point(self, origin_point, point1, point2):
# find the distance between both points and then choose the closest one to it
# the variables
x2, x1 = origin_point
y2, y1 = point1
z2, z1 = point2
# the distance formula
calc1 = ((x2 - x1) ^ 2) + ((y2 - y1) ^ 2)
calc2 = ((x2 - x1) ^ 2) + ((z2 - z1) ^ 2)
# to compare the distances (which seems to be the hard part)
try:
if math.sqrt(calc1) > math.sqrt(calc2):
self.draw_line((x2, x1), (z2, z1))
except ValueError:
new_calc1 = math.sqrt(abs(calc1))
new_calc2 = math.sqrt(abs(calc2))
if new_calc1 > new_calc2:
self.draw_line((x2, x1), (z2, z1))
if new_calc1 < new_calc2:
self.draw_line((x2, x1), (y2, y1))
I can't seem to find what's wrong. Also, if I need to provide more code as context or if something else is wrong then just ask.
Use the Dot product to find the point with the pointe which is "nearer" in a certain direction
def dot(p1, p2):
return p1[0]*p2[0] + p1[1]*p2[1]
if dot(origin_point, point1) < dot(origin_point, point2):
self.draw_line(origin_point, point1)
else:
self.draw_line(origin_point, point2)
The Dot product of 2 vectors is equal the cosine of the angle between the 2 vectors multiplied by the magnitude (length) of both vectors.
dot(A, B) == | A | * | B | * cos(angle_A_B)
This follows, that the dot product of 2 unit vectors is equal the cosine of the angle between the 2 vectors, because the length of a unit vector is 1.
uA = normalize(A)
uB = normalize(B)
cos(angle_A_B) == dot(uA, uB)

Python: Intersection of spheres

I am extremely new to programming but I decided to take on an interesting project as I recently learnt how to represent a sphere in parametric form. When intersecting three spheres, there are two points of intersections that are distinct unless they only overlap at a singular point.
Parametric representation of a sphere:
The code I have is modified from the answer from Python/matplotlib : plotting a 3d cube, a sphere and a vector?, adding the ability to dictate the x, y and z origin and the radius of the sphere. Many similar questions were written in C++, Java, and C#, which I cannot understand at all (I barely know what I am doing so go easy on me).
My Code:
import numpy as np
def make_sphere_x(x, radius):
u, v = np.mgrid[0:2 * np.pi:5000j, 0:np.pi:2500j]
x += radius * np.cos(u) * np.sin(v)
return x
def make_sphere_y(y, radius):
u, v = np.mgrid[0:2 * np.pi:5000j, 0:np.pi:2500j]
y += radius * np.sin(u) * np.sin(v)
return y
def make_sphere_z(z, radius):
u, v = np.mgrid[0:2 * np.pi:5000j, 0:np.pi:2500j]
z += radius * np.cos(v)
return z
#x values
sphere_1_x = make_sphere_x(0, 2)
sphere_2_x = make_sphere_x(1, 3)
sphere_3_x = make_sphere_x(-1, 4)
#y values
sphere_1_y = make_sphere_y(0, 2)
sphere_2_y = make_sphere_y(1, 3)
sphere_3_y = make_sphere_y(0, 4)
#z values
sphere_1_z = make_sphere_z(0, 2)
sphere_2_z = make_sphere_z(1, 3)
sphere_3_z = make_sphere_z(-2, 4)
#intercept of x-values
intercept_x = list(filter(lambda x: x in sphere_1_x, sphere_2_x))
intercept_x = list(filter(lambda x: x in intercept_x, sphere_3_x))
print(intercept_x)
Problems:
Clearly there must be a better way of finding the intercepts. Right now, the code generates points at equal intervals, with the number of intervals I specify under the imaginary number in np.mgrid. If this is increased, the chances of an intersection should increase (I think) but when I try to increase it to 10000j or above, it just spits a memory error.
There are obvious gaps in the array and this method would most likely be erroneous even if I have access to a super computer and can crank up the value to an obscene value. Right now the code results in a null set.
The code is extremely inefficient, not that this is a priority but people like things in threes right?
Feel free to flame me for rookie mistakes in coding or asking questions on Stack Overflow. Your help is greatly valued.
Using scipy.optimize.fsolve you can find the root of a given function, given an initial guess that is somewhere in the range of your solution. I used this approach to solve your problem and it seems to work for me. The only downside is that it only provides you one intersection. To find the second one you would have to tinker with the initial conditions until fsolve finds the second root.
First we define our spheres by defining (arbitrary) radii and centers for each sphere:
a1 = np.array([0,0,0])
r1 = .4
a2 = np.array([.3,0,0])
r2 = .5
a3 = np.array([0,.3,0])
r3 = .5
We then define how to transform back into cartesian coordinates, given angles u,v
def position(a,r,u,v):
return a + r*np.array([np.cos(u)*np.sin(v),np.sin(u)*np.sin(v),np.cos(v)])
Now we think about what equation we need to find the root of. For any intersection point, it holds that for perfect u1,v1,u2,v2,u3,v3 the positions position(a1,r1,u1,v1) = position(a2,r2,u2,v2) = position(a3,r3,u3,v3) are equal. We thus find three equations which must be zeros, namely the differences of two position vectors. In fact, as every vector has 3 components, we have 9 equations which is more than enough to determine our 6 variables.
We find the function to minimize as:
def f(args):
u1,v1,u2,v2,u3,v3,_,_,_ = args
pos1 = position(a1,r1,u1,v1)
pos2 = position(a2,r2,u2,v2)
pos3 = position(a3,r3,u3,v3)
return np.array([pos1 - pos2, pos1 - pos3, pos2 - pos3]).flatten()
fsolve needs the same amount of input and output arguments. As we have 9 equations but only 6 variables I simply used 3 dummy variables so the dimensions match. Flattening the array in the last line is necessary as fsolve only accepts 1D-Arrays.
Now the intersection can be found using fsolve and a (pretty random) guess:
guess = np.array([np.pi/4,np.pi/4,np.pi/4,np.pi/4,np.pi/4,np.pi/4,0,0,0])
x0 = fsolve(f,guess)
u1,v1,u2,v2,u3,v3,_,_,_ = x0
You can check that the result is correct by plugging the angles you received into the position function.
The problem would be better tackled using trigonometry.
Reducing the problem into 2D circles, we could do:
import math
import numpy
class Circle():
def __init__(self, cx, cy, r):
"""initialise Circle and set main properties"""
self.centre = numpy.array([cx, cy])
self.radius = r
def find_intercept(self, c2):
"""find the intercepts between the current Circle and a second c2"""
#Find the distance between the circles
s = c2.centre - self.centre
self.dx, self.dy = s
self.d = math.sqrt(numpy.sum(s**2))
#Test if there is an overlap. Note: this won't detect if one circle completly surrounds the other.
if self.d > (self.radius + c2.radius):
print("no interaction")
else:
#trigonometry
self.theta = math.atan2(self.dy,self.dx)
#cosine rule
self.cosA = (c2.radius**2 - self.radius**2 + self.d**2)/(2*c2.radius*self.d)
self.A = math.acos(self.cosA)
self.Ia = c2.centre - [math.cos(self.A+self.theta)*c2.radius, math.sin(self.A+self.theta)*c2.radius]
self.Ib = c2.centre - [math.cos(self.A-self.theta)*c2.radius,-math.sin(self.A-self.theta)*c2.radius]
print("Interaction points are : ", self.Ia, " and: ", self.Ib)
#define two arbitrary circles
c1 = Circle(2,5,5)
c2 = Circle(1,6,4)
#find the intercepts
c1.find_intercept(c2)
#test results by reversing the operation
c2.find_intercept(c1)

Optimizing by translation to map one x,y set of points onto another

I have a list of x,y ideal points, and a second list of x,y measured points. The latter has some offset and some noise.
I am trying to "fit" the latter to the former. So, extract the x,y offset of the latter relative to the former.
I'm following some examples of scipy.optimize.leastsq, but having trouble getting it working. Here is my code:
import random
import numpy as np
from scipy import optimize
# Generate fake data. Goal: Get back dx=0.1, dy=0.2 at the end of this exercise
dx = 0.1
dy = 0.2
# "Actual" (ideal) data.
xa = np.array([0,0,0,1,1,1])
ya = np.array([0,1,2,0,1,2])
# "Measured" (non-ideal) data. Add the offset and some randomness.
xm = map(lambda x: x + dx + random.uniform(0,0.01), xa)
ym = map(lambda y: y + dy + random.uniform(0,0.01), ya)
# Plot each
plt.figure()
plt.plot(xa, ya, 'b.', xm, ym, 'r.')
# The error function.
#
# Args:
# translations: A list of xy tuples, each xy tuple holding the xy offset
# between 'coords' and the ideal positions.
# coords: A list of xy tuples, each xy tuple holding the measured (non-ideal)
# coordinates.
def errfunc(translations, coords):
sum = 0
for t, xy in zip(translations, coords):
dx = t[0] + xy[0]
dy = t[1] + xy[1]
sum += np.sqrt(dx**2 + dy**2)
return sum
translations, coords = [], []
for xxa, yya, xxm, yym in zip(xa, ya, xm, ym):
t = (xxm-xxa, yym-yya)
c = (xxm, yym)
translations.append(t)
coords.append(c)
translation_guess = [0.05, 0.1]
out = optimize.leastsq(errfunc, translation_guess, args=(translations, coords), full_output=1)
print out
I get the error:
errfunc() takes exactly 2 arguments (3 given)"
I'm not sure why it says 3 arguments as I only gave it two. Can anyone help?
====
ANSWER:
I was thinking about this wrong. All I have to do is to take the average of the dx and dy's -- that gives the correct result.
n = xa.shape[0]
dx = -np.sum(xa - xm) / n
dy = -np.sum(ya - ym) / n
print dx, dy
The scipy.optimize.leastsq assumes that the function you are using already has one input, x0, the initial guess. Any other additional inputs are then listed in args.
So you are sending three arguments: translation_guess, transactions, and coords.
Note that here it specifies that args are "extra arguments."
Okay, I think I understand now. You have the actual locations and the measured locations and you want to figure out the constant offset, but there is noise on each pair. Correct me if I'm wrong:
xy = tuple with coordinates of measured point
t = tuple with measured offset (constant + noise)
The actual coordinates of a point are (xy - t) then?
If so, then we think it should be measured at (xy - t + guess).
If so, then our error is (xy - t + guess - xy) = (guess - t)
Where it is measured doesn't even matter! We just want to find the guess that is closest to all of the measured translations:
def errfunc(guess, translations):
errx = 0
erry = 0
for t in translations:
errx += guess[0] - t[0]
erry += guess[1] - t[1]
return errx,erry
What do you think? Does that make sense or did I miss something?

What is most efficient way to find the intersection of a line and a circle in python?

I have a polygon consists of lots of points. I want to find the intersection of the polygon and a circle. Providing the circle center of [x0,y0] and radius of r0, I have wrote a rough function to simply solve the quadratic equation of the circle and a line. But what about the efficiency of find the intersection of every line segment of the polygon one by one? Is there more efficient way?
I know sympy already provide the feature to get the intersections of different geometry. But also what about the efficiency of external library like sympy compared to calculate it by my own function, if I want to deal with lots of polygons?
def LineIntersectCircle(p,lsp,lep):
# p is the circle parameter, lsp and lep is the two end of the line
x0,y0,r0 = p
x1,y1 = lsp
x2,y2 = esp
if x1 == x2:
if abs(r0) >= abs(x1 - x0):
p1 = x1, y0 - sqrt(r0**2 - (x1-x0)**2)
p2 = x1, y0 + sqrt(r0**2 - (x1-x0)**2)
inp = [p1,p2]
# select the points lie on the line segment
inp = [p for p in inp if p[1]>=min(y1,y2) and p[1]<=max(y1,y2)]
else:
inp = []
else:
k = (y1 - y2)/(x1 - x2)
b0 = y1 - k*x1
a = k**2 + 1
b = 2*k*(b0 - y0) - 2*x0
c = (b0 - y0)**2 + x0**2 - r0**2
delta = b**2 - 4*a*c
if delta >= 0:
p1x = (-b - sqrt(delta))/(2*a)
p2x = (-b + sqrt(delta))/(2*a)
p1y = k*x1 + b0
p2y = k*x2 + b0
inp = [[p1x,p1y],[p2x,p2y]]
# select the points lie on the line segment
inp = [p for p in inp if p[0]>=min(x1,x2) and p[0]<=max(x1,x2)]
else:
inp = []
return inp
I guess maybe your question is about how to in theory do this in the fastest manner. But if you want to do this quickly, you should really use something which is written in C/C++.
I am quite used to Shapely, so I will provide an example of how to do this with this library. There are many geometry libraries for python. I will list them at the end of this answer.
from shapely.geometry import LineString
from shapely.geometry import Point
p = Point(5,5)
c = p.buffer(3).boundary
l = LineString([(0,0), (10, 10)])
i = c.intersection(l)
print i.geoms[0].coords[0]
(2.8786796564403576, 2.8786796564403576)
print i.geoms[1].coords[0]
(7.121320343559642, 7.121320343559642)
What is a little bit counter intuitive in Shapely is that circles are the boundries of points with buffer areas. This is why I do p.buffer(3).boundry
Also the intersection i is a list of geometric shapes, both of them points in this case, this is why I have to get both of them from i.geoms[]
There is another Stackoverflow question which goes into details about these libraries for those interested.
SymPy
CGAL Python bindings
PyEuclid
PythonOCC
Geometry-simple
EDIT because comments:
Shapely is based on GEOS (trac.osgeo.org/geos) which is built in C++ and considerably faster than anything you write natively in python. SymPy seems to be based on mpmath (mpmath.org) which also seems to be python, but seems to have lots of quite complex math integrated into it. Implementing that yourself may require a lot of work, and will probably not be as fast as GEOS C++ implementations.
Here's a solution that computes the intersection of a circle with either a line or a line segment defined by two (x, y) points:
def circle_line_segment_intersection(circle_center, circle_radius, pt1, pt2, full_line=True, tangent_tol=1e-9):
""" Find the points at which a circle intersects a line-segment. This can happen at 0, 1, or 2 points.
:param circle_center: The (x, y) location of the circle center
:param circle_radius: The radius of the circle
:param pt1: The (x, y) location of the first point of the segment
:param pt2: The (x, y) location of the second point of the segment
:param full_line: True to find intersections along full line - not just in the segment. False will just return intersections within the segment.
:param tangent_tol: Numerical tolerance at which we decide the intersections are close enough to consider it a tangent
:return Sequence[Tuple[float, float]]: A list of length 0, 1, or 2, where each element is a point at which the circle intercepts a line segment.
Note: We follow: http://mathworld.wolfram.com/Circle-LineIntersection.html
"""
(p1x, p1y), (p2x, p2y), (cx, cy) = pt1, pt2, circle_center
(x1, y1), (x2, y2) = (p1x - cx, p1y - cy), (p2x - cx, p2y - cy)
dx, dy = (x2 - x1), (y2 - y1)
dr = (dx ** 2 + dy ** 2)**.5
big_d = x1 * y2 - x2 * y1
discriminant = circle_radius ** 2 * dr ** 2 - big_d ** 2
if discriminant < 0: # No intersection between circle and line
return []
else: # There may be 0, 1, or 2 intersections with the segment
intersections = [
(cx + (big_d * dy + sign * (-1 if dy < 0 else 1) * dx * discriminant**.5) / dr ** 2,
cy + (-big_d * dx + sign * abs(dy) * discriminant**.5) / dr ** 2)
for sign in ((1, -1) if dy < 0 else (-1, 1))] # This makes sure the order along the segment is correct
if not full_line: # If only considering the segment, filter out intersections that do not fall within the segment
fraction_along_segment = [(xi - p1x) / dx if abs(dx) > abs(dy) else (yi - p1y) / dy for xi, yi in intersections]
intersections = [pt for pt, frac in zip(intersections, fraction_along_segment) if 0 <= frac <= 1]
if len(intersections) == 2 and abs(discriminant) <= tangent_tol: # If line is tangent to circle, return just one point (as both intersections have same location)
return [intersections[0]]
else:
return intersections
A low cost spacial partition might be to divide the plane into 9 pieces
Here is a crappy diagram. Imagine, if you will, that the lines are just touching the circle.
| |
__|_|__
__|O|__
| |
| |
8 of the areas we are interested in are surrounding the circle. The square in the centre isn't much use for a cheap test, but you can place a square of r/sqrt(2) inside the circle, so it's corners just touch the circle.
Lets label the areas
A |B| C
__|_|__
D_|O|_E
| |
F |G| H
And the square of r/sqrt(2) in the centre we'll call J
We will call the set of points in the centre square shown in the diagram that aren't in J, Z
Label each vertex of the polygon with it's letter code.
Now we can quickly see
AA => Outside
AB => Outside
AC => Outside
...
AJ => Intersects
BJ => Intersects
...
JJ => Inside
This can turned into a lookup table
So depending on your dataset, you may have saved yourself a load of work. Anything with an endpoint in Z will need to be tested however.
I think that the formula you use to find the coordinates of the two intersections cannot be optimized further. The only improvement (which is numerically important) is to distinguish the two cases: |x_2-x_1| >= |y_2-y_1| and |x_2-x1| < |y_2-y1| so that the quantity k is always between -1 and 1 (in your computation you can get very high numerical errors if |x_2-x_1| is very small). You can swap x-s and y-s to reduce one case to the other.
You could also implement a preliminary check: if both endpoints are internal to the circle there are no intersection. By computing the squared distance from the points to the center of the circle this becomes a simple formula which does not use the square root function. The other check: "whether the line is outside the circle" is already implemented in your code and corresponds to delta < 0. If you have a lot of small segments these two check should give a shortcut answer (no intersection) in most cases.

Categories

Resources