|
SketchUp Ruby API Reference |
The Geom module defines
a number of Module methods that let you perform different geometric operations.
The methods in this module take
lines and planes as arguments. There is no special class for representing
lines or planes.
A line can be represented as either
an Array of a point and a vector, or as an Array of two points.
line = [Geom::Point3d.new(0,0,0), Geom::Vector3d.new(0,0,1)]
line = [Geom::Point3d.new(0,0,0), Geom::Point3d.new(0,0,100)]
A plane can be represented as
either an Array of a point and a vector, or as an Array of 4 numbers that
given the coeficients of a plane equation.
plane = [Geom::Point3d.new(0,0,0),
Geom::Vector3d.new(0,0,1)]
plane = [0,0,1,0]
NOTE: Lines
and Planes are infinite.
There are several good books on
3D math if you are new to the concepts of a line, plane, and vector.
Parent: N/A
Methods: closest_points, fit_plane_to_points, intersect_line_line, intersect_line_plane, intersect_plane_plane, linear_combination, point_in_polygon_2d
Example Code: geomtests.rb
The closest_points method is used to compute the closest points on two lines.
pts = Geom.closest_points line1, line2
pts - an array of two points. The first point is on the first line and the second point is on the second line.
line1 = [Geom::Point3d.new(0,0,0), Geom::Vector3d.new(0,0,1)]
line2 = [Geom::Point3d.new(0,0,0), Geom::Vector3d.new(0,0,100)]
# 0,0,0 on each line should be closest because both lines start from
# that point
pts = Geom.closest_points line1, line2
The fit_plane_to_points method is used to compute a plane that is a best fit to an array of points
plane = Geom.fit_plane_to_points points
plane = Geom.fit_plane_to_points point1, point2, point3, ...
points - an array of points
point1, point2, point3 - Point3D objects
plane - a plane
If more than three points are given, some of the points may not be on the plane.
The plane is returned as an Array of 4 numbers which are the coefficients of the plane equation Ax + By + Cz + D = 0
point1 = Geom::Point3d.new 0,0,0
point2 = Geom::Point3d.new 10,10,10
point3 = Geom::Point3d.new 25,25,25
plane = Geom.fit_plane_to_points point1, point2, point3
The intersect_line_line computes the intersection of two lines.
point = Geom.intersect_line_line line1, line2
line1, line2 - A line is given as either an array of a point and a vector, or an Array or two points.
Returns nil if they do not intersect.
line1=[Geom::Point3d.new(10,0,0),Geom::Vector3d.new(1,0,0)]line2=[Geom::Point3d.new(0,10,0),Geom::Point3d.new(20,10,0)]pt = Geom.intersect_line_line(line1, line2)This will return the point (10,10,0).
The intersect_line_plane method is used to compute the intersection of a line and a plane.
point = Geom.intersect_line_plane line, plane
line - a line
plane - a plane
point - a Point3d object
# This is really the normal which follows the x axis. The plane is
# perpendicular
plane1 = [Geom::Point3d.new(-10, 0 ,0), Geom::Vector3d.new(1,0,0)]
# This line follows the x axis
line1 = [Geom::Point3d.new(-10,0,0), Geom::Vector3d.new(1,0,0)]
# returns -10, 0, 0
pt = Geom.intersect_line_plane(line1, plane1)
The intersect_plane_plane method is used to compute the intersection of two planes.
line = Geom.intersect_plane_plane plane1, plane2
plane1, plane2 - planes
line - a line where the planes intersect if successful. Returns nil if the planes do not intersect.
# This is really the normal which follows the x axis. The plane is
# perpendicular (or parallel to the Y axis)
plane1 = [Geom::Point3d.new(-10, 0 ,0), Geom::Vector3d.new(1,0,0)]
# This is really the normal which follows the y axis. The plane is
# perpendicular (or parallel to the X axis)
plane2 = [Geom::Point3d.new(0,-10,0), Geom::Vector3d.new(0,1,0)]
# returns (-10,-10,0")(0.0,0.0,1.0)
line = Geom.intersect_plane_plane(plane1, plane2)
The linear_combination method is used to compute the linear combination of points or vectors.
point = Geom.linear_combination(weight1, point1, weight2, point2)
vector = Geom.linear_combination(weight1, vector1, weight2, vector2)
weight1 - a weight or percentage
point1 - the start point on the line
weight2 - a weight or percentage
point2 - the end point of the line
vector1 - the first vector
vector2 - the end point of the line
point - point - a Point3d object
vector - vector - a Vector3d object
point1 = Geom::Point3d.new 1,1,1
point2 = Geom::Point3d.new 10,10,10
# Gets the point on the line segment connecting point1 and point2 that is
# 3/4 the way from point1 to point2.
point = Geom.linear_combination 0.25, point1, 0.75, point2
The point_to_polygon_2D method is used to determine whether a point is inside a polygon when that point is projected as though it and the polygon were in 2D space. The Z component is ignored.
status = Geom.point_to_polygon_2D point, points,
point - a Point3d object
points - an array of points that represent the points of the polygon you are checking
border - true if a point on the border is counted as inside the polygon.
status - true if the point is inside the polygon. False if the point is not inside the polygon.
|
SketchUp Ruby API Reference: Geom |
© Google Inc. 2007 sketchup.google.com |