天天看点

sketchup Ruby API学习笔记(二)——Array(数组)

作者:钢之炼筋术士

4 Class:Array(数组)

sketchup ruby API(以下简称“SURA”)的数组在做Geom::Vector3d 和 Geom::Point3d两个模块类的输入时,其前三个元素a[0],a[1]和a[2]分别代表了xyz三个坐标轴的参数。

4.1 entities.add_cpoint(array)和Geom::Transformation.new(array)插入和移动点

打开https://ruby.sketchup.com/Array.html的页面,第一个代码案例的意思是:声明一个三元的数组[0, 0, 1]z轴的值为1,在图上画出一个该数组坐标的cpoint(add_cpoint)。在赋值一个 Geom::Transformation模块类,移动该点到新的位置。这里的代码需要分别回车,才能看到意图。我把代码改成如下:

array = [0, 0, 1]

entities = Sketchup.active_model.entities
construction_point = entities.add_cpoint(array)

array = [1, 0, 1]

transformation = Geom::Transformation.new(array)
entities.transform_entities(transformation, construction_point)           

以3行为块,分别输入控制台,结果如下:

sketchup Ruby API学习笔记(二)——Array(数组)
sketchup Ruby API学习笔记(二)——Array(数组)

4.2 cross(vector) ⇒ Geom::Vector3d向量叉乘(好像是)

Geom::Vector3d可以理解为相对于坐标0点的观察者。Array.cross(vector) 好像是叉乘的意思。

vector1 = Geom::Vector3d.new(0, 1, 0)
array = [1, 0, 0]
# This will return a new Vector3d
vector2 = array.cross(vector1)           

运行的结果是(0,0,1)好像是两个向量的法向量(×乘)的结果。

sketchup Ruby API学习笔记(二)——Array(数组)

4.3 distance(point) ⇒ Length点到点的距离

两点间的距离。这里有一个单位转换的过程。SURA默认的是英寸,如果你使用的是建筑mm制。要做一个换算。

sketchup Ruby API学习笔记(二)——Array(数组)

以下代码运行的结果是:根号(254平方+254平方+254平方)~396mm

point = Geom::Point3d.new(10, 10, 10)
array = [1, 1, 1]
# This will return a Length
distance = array.distance(point)           

4.4 distance_to_line点到直线的距离

line由直线上一个点,和一个方向向量组成。

line = [Geom::Point3d.new(0, 0, 0), Geom::Vector3d.new(0, 0, 1)]
array = [10, 10, 10]
# This will return a Length
distance = array.distance_to_line(line)           

以上代码运行的结果是:根号(254平方+254平方)~359mm

4.5 distance_to_plane(array)点到平面的距离

plane由平面上一个点,和一个法向量组成。

plane = [Geom::Point3d.new(0, 0, 0), Geom::Vector3d.new(0, 0, 1)]
array = [10, 10, 10]
# This will return a Length
distance = array.distance_to_plane(plane)           

以上代码运行的结果是:254mm

4.6 dot(vector) ⇒ Float两向量的点积

vector = Geom::Vector3d.new(12, 12, 0)
array = [12, 0, 0]
# This will return a Float, in this case 144.0
dot_product = array.dot(vector)           

代码返回144.0

4.7 normalize⇒ Array(Float, Float, Float)归一化向量

array = [1, 2, 3]
# This will return a new Vector3d
normal_vector = array.normalize           

返回数组:[0.2672612419124244, 0.5345224838248488, 0.8017837257372732]

4.8 offset(vector) ⇒ Array(Length, Length, Length)偏移向量

array = [10, 10, 10]
vector = Geom::Vector3d.new(0, 0, 1)
# This will modify 'array' in place
length_array = array.offset(vector)           

返回一个数组:【10,10,11】

第二个参数offset(vector,length)指向量长度

4.9offset!(vector) ⇒ Array(Length, Length, Length)

array = [10, 10, 10]
vector = Geom::Vector3d.new(0, 0, 1)
# This will modify 'array' in place
array.offset!(vector)           

offset与offset!的区别在于带!的时候array变量已经修改,而不带!时,只返回值,而不修改变量。

4.10 #on_line?(point, vector) ⇒ Boolean

4.10 #on_line?(point1, point2) ⇒ Boolean

line = [Geom::Point3d.new(0, 0, 0), Geom::Vector3d.new(0, 0, 1)]
array = [10, 10, 10]
# This will return a true or false value
on_plane = array.on_line?(line)           

判断array点是否在line上。

4.11 #on_plane?(point, vector) ⇒ Boolean

4.11 #on_plane?(point1, point2, point3) ⇒ Boolean

4.11 #on_plane?(float1, float2, float3, float4) ⇒ Boolean

4.11 #on_plane?(array) ⇒ Boolean

4.11 #on_plane?(array) ⇒ Boolean

4.11 #on_plane?(array) ⇒ Boolean

plane = [Geom::Point3d.new(0, 0, 0), Geom::Vector3d.new(0, 0, 1)]
array = [10, 10, 10] #false [10,10,0] true
# This will return a true or false value
on_plane = array.on_plane?(plane)           

判断array点是否在plane上。

4.12 #project_to_line(point, vector) ⇒ Array(Length, Length, Length)

4.12 #project_to_line(point1, point2) ⇒ Array(Length, Length, Length)

line = [Geom::Point3d.new(0, 0, 0), Geom::Vector3d.new(0, 0, 1)]
array = [10, 10, 10]#返回【0,0,10】
# This will return a new Array
point_on_line = array.project_to_line(line)           

返回array在line上的投影。

4.13 #project_to_plane(point, vector) ⇒ Array(Length, Length, Length)

4.13 #project_to_plane(point1, point2, point3) ⇒ Array(Length, Length, Length)

4.13 #project_to_plane(float1, float2, float3, float4) ⇒ Array(Length, Length, Length)

4.13 #project_to_plane(array) ⇒ Array(Length, Length, Length)

4.13 #project_to_plane(array) ⇒ Array(Length, Length, Length)

4.13 #project_to_plane(array) ⇒ Array(Length, Length, Length)

plane = [Geom::Point3d.new(0, 0, 0), Geom::Vector3d.new(0, 0, 1)]
array = [10, 10, 10]
point_on_plane = array.project_to_plane(plane)           

返回array在plane上的投影。

4.14 #transform(transform) ⇒ Array<Length, Length>

4.14 #transform(transform) ⇒ Array<Length, Length, Length>

4.14 #transform!(transform) ⇒ Array

4.14 #transform!(transform) ⇒ Array

point1 = Geom::Point3d.new(10, 20, 30)
transform = Geom::Transformation.new(point1)
array = [1, 2, 3]
# This will return a new Array
point2 = array.transform(transform)           
point = Geom::Point3d.new(10, 20, 30)
transform = Geom::Transformation.new(point)
array = [1, 2, 3]
# This will modify 'array' in place
array.transform!(transform)           

是否带!表示是否给array变量赋值。

4.15 #vector_to(point) ⇒ Geom::Vector3d

4.15 #vector_to(point) ⇒ Geom::Vector2d

point = Geom::Point3d.new(10, 20, 30)
array = [1, 2, 3]
# This will return a new Vector3d
vector = array.vector_to(point) #返回(9, 18, 27)           

4.16 #x⇒ Object?

4.17 #y⇒ Object?

4.18 #z⇒ Object?

array = [1, 2, 3]
# This will return a Fixnum, in this case 1
x = array.x

array = [1.0, 2.0, 3.0]
# This will return a            
Float, in this case 1.0
x = array.x           

这个自己猜吧:)

4.19 Array数组总结

数组类下的方法基本就是上述这些了。重点掌握Geom::Point3d、Geom::Vector3d与Array的关系,即:point,vector,Array之间的关系。还有就是向量几何的一些基础。line[点,方向向量];plane[点,法向向量],等等。

继续阅读