|
SketchUp Ruby API Reference |
The Point3d class allows you to work with a point in 3d space.
The point is basically just a series of values representing x, y and z
coordinates. The values are specified as [x,y,z]. For example [100,200,300].
To create a point:
Geom::Point3d.new - where the creation method (new) can take a variety of arguments.
Geom::Point3d.new - with no arguments, creates a point at the origin [0,0,0]
Geom::Point3d.new(100,200,300) - creates a point in space with an x value of 100, a y value of 200 and a z value of 300
Geom::Point3d.new 100,200,300 - same as above
You can also create a point directly by simply assigning the x, y and z values to a variable as an array:
pt = [100,200,300]
Parent: Object
Methods: linear_combination, new, +, -, ==, [], []=, clone, distance, distance_to_line, distance_to_plane, inspect, offset, offset!, on_line?, on_plane?, project_to_line, project_to_plane, set!, to_a, to_s, transform, transform!, vector_to, x, x=, y, y=, z, z=
Example Code: point3dtests.rb
The linear_combination method is used to create a new point as a linear combination of two points. This method is generally used to get a point at some percentage along a line connecting the two points.
point = Geom::Point3d.linear_combination(weight1, point1, weight2, point2)
weight1 - a weight or percentage
point1 - the start point on the line
weight2 - a weight or percentage
point2 - the end point of the line
point - a Point3d object
A linear combination is a standard term for vector math. It is defined as point = weight1 * point1 + weight2 * point2.
point1 = Geom::Point3d.new 1,1,1point2 = 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::Point3d.linear_combination 0.25, point1, 0.75, point2if (point)UI.messagebox pointelseUI.messagebox "Failure"end
The new method is used to create a new 3d point.
point = Geom::Point3d.new
point = Geom::Point3d.new(x,y)
point = Geom::Point3d.new(x,y,z)
x - the location along the x axis
y - the location along the y axis
z - the location along the z axis
point - the newly created Point3d object
With no arguments, this creates a point at the origin (0,0,0). With two values, it creates a point at (x,y,0).
point = Geom::Point3d.new
The + method is used to add a point and a vector together.
point2 = point + vector
point - a Point3d object
vector - a Vector3d object
The + method is used to subtract two points to get a vector. See examples under Point3d +
vector = point2 - point1
point1 - a Point3d object
point2 - a Point3d object
vector - a Vector3d object
point1 = Geom::Point3d.new 1,1,1
point2 = Geom::Point3d.new 10,10,10
vector = point2-point1
The == method is used to compare two points for equality.
status = point1 == point2
point1 - a Point3d object
point2 - a Point3d object
status - true if both points are equal; false if points are not equal
This uses the standard SketchUp tolerance to determine if two points are the same.
Points can be compared to one another or to an array representing x, y and z coordinates, as in the following examples:
if( pt1 == pt2 ) ...
or
if( pt1 == [100,200,300] ) ...
point1 = Geom::Point3d.new 1,1,1
point2 = Geom::Point3d.new 10,10,10
status = point1 == point2
The [] method is used to retrieve the value of the point at the specified index.
value = point[index]
index - the index for a specific x,y, or z value within the Point3d
value - an x, y, or z value if successful
point = Geom::Point3d.new 1,2,3
# retrieves the y value
yvalue = point[1]
The []= method is used to set the x, y, or z value of the point based on the specific index of the value.
status = point[n] = new_value
new_value - new x, y, or z value
status - the newly set x, y, or z value if successful
pt[0] = xval
pt[1] = yval
pt[2] = zval
point = Geom::Point3d.new 1,2,3
yvalue = point[1] = 4
The clone method is used to create another point identical to the point being cloned.
point2 = point.clone
point2 - the cloned Point3d object
point = Geom::Point3d.new 1,2,3
newpoint = point.clone
The distance method is used to compute the distance from a point to another point.
distance = point1.distance point2
point2 - the Point3d object to compute the distance to
distance - the distance in current units
point1 = Geom::Point3d.new 1,1,1
point2 = Geom::Point3d.new 10,10,10
distance = point1.distance point2
The distance_to_line method is used to compute the distance from a point to a line.
distance = point.distance_to_line line
line - a line (see Geom for information on creating lines)
distance - the distance between a point and line in current units if successful
See module Geom for how to specify a line.
point1 = Geom::Point3d.new 1,1,1
point2 = Geom::Point3d.new 10,10,10
distance = point1.distance point2
The distance_to_plane method is used to compute the distance from the point to a plane.
distance = point.distance_to_plane plane
plane - a plane (see Geom for how to create a plane)
distance - a distance between a point and a plane in current units if successful
See module Geom for how to specify a plane.
point1 = Geom::Point3d.new 1,1,1
point2 = Geom::Point3d.new 10,10,10
distance = point1.distance point2
The inspect method is used to format a 3d point as a string.
string = point.inspect
point - a string point representation
You will not often use these function
directly. Instead, they are called automatically when an object is output using a print command like 'puts', which writes
to the
Ruby console.
point = Geom::Point3d.new 10,10,10
string = point.inspect
The offset method is used to offset a point by a vector.
point2 = point1.offset(vector)
point2 = point1.offset(vector, distance)
vector - a Vector3d object to offset the point by
distance - a distance
point2 - a new Point3d object
In the second form, the point is offset in the direction of the vector by the given distance. The length of the vector must not be zero.
point1 = Geom::Point3d.new 10,10,10
vector = Geom::Vector3d.new(0,0,1)
point2 = point1.offset vector
The offset! method is used to offset a point by a vector. The point itself is modified.
point2 = point.offset! vector
vector - a Vector3d object to offset the point by
point2 - a new Point3d object
Unlike offset, the point itself is modified.
point = Geom::Point3d.new 10,10,10
vector = Geom::Vector3d.new(0,0,1)
point2 = point1.offset! vector
The on_line? method is used to determine if the point is on a line.
status = point.on_line? line
line - a line (see Geom for how to create a line)
status - true if the point is on the line; false if the point is not on the line
See module Geom for how to specify a line.
line = [Geom::Point3d.new(0,0,0), Geom::Vector3d.new(0,0,1)
point = Geom::Point3d.new 10,10,10
status = point.on_line? line
The on_plane? method is used to determine if the point is on a plane.
status = point.on_plane? point, normal
status = point.on_plane? A, B, C, D
status - true if the point is on the plane; false if the point is not on the plane
See module Geom for how to specify a line.
plane = [Geom::Point3d.new(0,0,0), Geom::Vector3d.new(0,0,1)]
point = Geom::Point3d.new 10,10,10
status = point.on_plane? plane
The project_to_line method is used to retrieve the point on a line that is closest to this point.
point = point.project_to_line line
line - a line (see Geom for how to create a line)
point - the Point3d that is on a line closest to the point
The line may be defined by either a point and a vector or by two points.
The project_to_plane method is used to retrieve the point on a plane that is closest to the point.
point = point.project_to_plane plane
plane - a plane (see Geom for how to create a plane)
point - the Point3d that is on a plane closest to the point
The plane may be defined by either a point on the plane and a vector perpendicular to the plane or by the coeficients to the plane equation AX + BY + CZ + D = 0
point = Geom::Point3d.new 10,10,10
status = point.set! 100,200,300
The set! method is used to set the values of the Point3d.
status = point.set! x,y,z
x - the location along the x axis
y - the location along the y axis
z - the location along the z axis
point - the newly created Point3d object
point = Geom::Point3d.new 10,10,10
point = point.set! 100,200,300
The to_a method is used to convert the point to an array of 3 numbers
array = pt.to_a
array - an array of three numbers representing x,y,z of the Point3d
pt = [100,200,300] pt.to_a outputs [100.0,200.0,300.0]
point = Geom::Point3d.new 10,20,30
array = point.to_a
The to_s method is used to retrieve a string representation of a point.
string = pt.to_s
string - the string representation of the Point3d
point = Geom::Point3d.new 10,10,10
str = point.to_s
The transform method is used to create a new point by applying a transformation to a point.
point2 = point.transform transformation
transformation - a Transformation object
point2 - the transformed Point3d object
transform = Geom::Transformation.new(point2)
point2 = Geom::Point3d.new 100,200,300
point1 = Geom::Point3d.new 10,10,10
point3 = point1.transform transform
The transform! method is used to apply a Transformation to a point.
point.transform! transformation
transformation - a Transformation object
Unlike the transform method, the point itself is modified.
transform = Geom::Transformation.new(point2)
point2 = Geom::Point3d.new 100,200,300
point1 = Geom::Point3d.new 10,10,10
point3 = point1.transform! transform
The vector_to team method retrieves the vector between points.
vector = point1.vector_to point2
point2 - a Point3d object
vector - a Vector object
pt1 = [1,1,0] pt2 = [3,1,0] pt1.vector_to( pt2 ) returns the vector (2,0,0) pt1.vector_to(pt2) is equivalent to (pt2 - pt1)
point2 = Geom::Point3d.new 100,200,300
point1 = Geom::Point3d.new 10,10,10
vector = point1.vector_to point2
The x method retrieves the x value of the 3d point.
x = point.x
x - the new x value
point = Geom::Point3d.new 1,2,3
x = point.x
The x= method is used to set the x value of a 3d point.
x= point.x = value
value - the new x value
x - the newly set x value
point = Geom::Point3d.new 1,2,3
x = point.x=2
The y method retrieves the y value of the 3d point.
y = point.y
y - the new y value
point = Geom::Point3d.new 1,2,3
y = point.y
The y= method is used to set the y value of a 3d point.
y = point.y = value
value - the new y value
y - the newly set y value
point = Geom::Point3d.new 1,2,3
y = point.y=2
The z method retrieves the z value of the 3d point.
z = point.z
z - the z value
point = Geom::Point3d.new 1,2,3
z = point.x
The z= method is used to set the z value of a 3d point.
point.z = value
value - the new z value
z - the newly set z value
point = Geom::Point3d.new 1,2,3
z = point.z=2
|
SketchUp Ruby API Reference: Point3d |
© Google Inc. 2007 sketchup.google.com |