In Ruby, would you have a clue that could help me to retrieve the x and y coordinate of a point located on a particular z coordinate (for instance 30) along a line starting on (0,0,0) to (10,10,100) ?
For your specific case,
x = y = 10.0*(z/100.0)
Thanks, it"s so obvious!
What about a line starting from (0,0,0) to (20,45,100) ?
I should look back 30 years ago to my descriptive geometry “knowledge”
Yeah, it does rot with age
The general equation of a line from P1 to P2 is
P = P1 + k(P2 - P1), 0.0 <= k <= 1.0
which broken down by component means
px = p1x + k(p2x - p1x)
py = p1y + k(p2y - p1y)
pz = p1z + k(p2z - p1z)
So, solve for k in any one of these equations and plug that into the other two.
In your second example, p1z = 0 and p2z - p1z = 100, so
k = pz/100.0 = 30.0/100.0 = 3.0/10.0
px = (3.0/10.0) * 20.0 = 6.0
py = (3.0/10.0) * 45.0 = 13.5
Note: Ruby does integer arithmetic if you don’t make it clear that you want real numbers. That’s why I put “.0” onto each value.
# Given an edge object line = edge.line # or from arbitrary points ... # line = [Geom::Point3d.new(0,0,0), Geom::Point3d.new(20,45,100)] plane = [Geom::Point3d.new(0, 0, 30.0), Z_AXIS] point = Geom::intersect_line_plane(line,plane) # Lastly test that point is non-nil for success.
Just to be 100% clear, what @DanRathbun showed uses methods from the SketchUp Ruby API, not simply Ruby. Depending on what else you are doing, his way might be more compatible with the rest of your code. Also, since the API methods are often wrappers around a compiled C routine, if this calculation is done in a loop for a lot of points, using the API might be faster.
That’s clear ! Very helpfull
Great ! That’s the function I was looking for
Be mindful that using Geom::intersect_line_plane uses the infinite line defined by edge, and not just the line-segment bound by the vertices of edge. Thus the intersection point may lie outside the two points you specified in the original post if they are used to define the line, as in:
line = [Geom::Point3d.new(0,0,0), Geom::Point3d.new(10,10,100)]
… we discussed testing whether a point was on an edge in this thread …