Is Point between Points?
-
Given three points A, B and C which are co-linear, what is the best practice way to determine if C is between A and B?
I searched the net and there appear to be various solutions, but there is the question of dealing with precision errors...
-
I hacked together this:
def self.point_between(a, b, c) v1 = c.vector_to(a) v2 = c.vector_to(b) return true if !v1.valid? || !v2.valid? !v1.samedirection?(v2) end
How bad is that?
-
RickW did something...
point1.between?(point2,point3)
???
Not sure where it is ... -
Do you know why just comparing the individual X,Y and Zs won't work?
-
@cjthompson said:
Do you know why just comparing the individual X,Y and Zs won't work?
Like how?
I'm curious in terms of performance and accuracy.
-
Here's one of several examples that are around
class Geom;;Point3d def between?(pt1, pt2, cond) # original idea by RickW # self is the point3d to test # cond if true returns true if self is at and end point, pt1 or pt2 # cond if false does not include for self on an end point, pt1 or pt2 # returns true if self is on the line between pt1 and pt2 # returns false if not. d1 = self.distance(pt1) + self.distance(pt2) d2 = pt1.distance(pt2) + 0.0 if (d1 <= d2) || (d1-d2 < 1e-10) if cond return true else return true if self != pt1 && self != pt2 end#if end#if return false end#def end#class
-
1e-10
- is that the tolerance?I refined my previous hack. I have tried this method in hopes that the tolerance in the methods used makes for a result that matches how SU works. But I'm not all too sure. And I have not benchmarked it.
# Checks if point C is between line segment AB. # Uses Sketchup methods to match it's accuracy. (Reliable?) # Compares the vectors from C to A and B. Assumes Zero vector means that C # is on the same point as A or B, which counts as 'between'. def self.point_between(a, b, c) v1 = c.vector_to(a) v2 = c.vector_to(b) return true unless v1.valid? && v2.valid? v1.samedirection?(v2.reverse!) end
-
Btw, here is one of the links I looked at while reading up on this: http://stackoverflow.com/questions/328107/how-can-you-determine-a-point-is-between-two-other-points-on-a-line-segment
-
@TIG
You should probably convert d1 and d2 to float before comparing them:
0.0001.inch == 0.001.inch #=> true
BTW, do you have to determine whether the point is on the line, or is it just assumed?
-
something like this:
def pointBetween(a,b,c) array = [[a.x.to_f,b.x.to_f],[a.y.to_f,b.y.to_f],[a.z.to_f,b.z.to_f]] x = c.x.to_f.between?(*array[0].sort) y = c.y.to_f.between?(*array[1].sort) z = c.z.to_f.between?(*array[2].sort) return x && y && z end
I'm not saying it will work, I'm just wondering whether it will or not.
Advertisement