Generating coordinates with Ruby
-
Hi, I'm looking for how to generates coordinate with an equation and connect the coordinates adding line or curve with ruby.
My idea was
ents = Sketchup.active_model.entities pt0 = [a,b,c] pt1 = [d,e,f] pt2 = [g,h,i] for k in 0...20 x = eq*pt0.x + eq*pt1.x + eq*pt2.x y = eq*pt0.y + eq*pt1.y + eq*pt2.y z = eq*pt0.z + eq*pt1.z + eq*pt2.z pt3 = Geom;;Point3d.new(x, y, z) end ents.add_line pt3
pt0, pt1, pt2 are basic coordinates. 'eq' means random equation. We can add any equation with a variable 'k'
I know there are some errors, but don't know how to fix it. I wish 21 different coordinates are generated from the 'for loop' and they connect each other to draw a line. Can you help me?
-
@kmk111890 said:
Hi, I'm looking for how to generates coordinate with an equation and connect the coordinates adding line or curve with ruby.
My idea was
> ents = Sketchup.active_model.entities > pt0 = [a,b,c] > pt1 = [d,e,f] > pt2 = [g,h,i] > > for k in 0...20 > > x = eq*pt0.x + eq*pt1.x + eq*pt2.x > y = eq*pt0.y + eq*pt1.y + eq*pt2.y > z = eq*pt0.z + eq*pt1.z + eq*pt2.z > > pt3 = Geom;;Point3d.new(x, y, z) > > end > > ents.add_line pt3 >
pt0, pt1, pt2 are basic coordinates. 'eq' means random equation. We can add any equation with a variable 'k'
I know there are some errors, but don't know how to fix it. I wish 21 different coordinates are generated from the 'for loop' and they connect each other to draw a line. Can you help me?
The .add_line functions requires a start and end. Your code only has an end. The ents.add_line statement should be inside the For loop. If not, only the last "pair" will be used in the .add_line.
-
In addition, if you gather the points into an Array you can use Entities#add_edges to create them all in one shot.
-
Something like this:
pt0 = [a, b, c] pt1 = [d, e, f] pt2 = [g, h, i] pt3 = Geom;;Point3d.new(0, 0, 0) pts = [] for k in 0...20 x = eq*pt0.x + eq*pt1.x + eq*pt2.x y = eq*pt0.y + eq*pt1.y + eq*pt2.y z = eq*pt0.z + eq*pt1.z + eq*pt2.z pt3 = Geom;;Point3d.new(x, y, z) pts << pt3 end Sketchup.active_model.active_entities.add_edges(pts)
Naturally you'll need to define the xyz value references
a
up toi
, and theeq
earlier on in the code...
BUT is this example it currently makes a straight line,k
is never used ?
Is it actuallyeq
?
If so then currently the points would just 'step' up by 1" each time !
I suspect you somehow want to usept3
iterated in the calculation [that's why I made it outside of the block before it starts changing] ?
e.g.x = eq*pt0.x + eq*pt1.x + eq*pt2.x**+ eq*pt3.x**
-
Thank you, sdmitch and TIG. I could partly understand how i have to fix the codes, but not perfect because I showed you code roughly. Maybe, it would be better i show you what i'm doing now. I'm trying to add bezier curve to draw one simple shape. The equation for the quadratic bezier curve represents as shown below
B(t) = (1-t)(1-t)P0+2(1-t)P1+(t)(t)P2
With the equation, I tried to generate coordinates which are from the equation and wanted to use 'For loop'. hmmm It looked like i can make the codes by myself, but didn't work well
ents = Sketchup.active_model.entities Length = 1000 angle = (60)/2 angle2 = (45)/2 pt0 = [0,0,0] pt1 = [Length.mm, 0, 0] aa = 80*0.01 pt2 = pt1.transform(Geom;;Transformation.rotation(pt0,[0,-1,0],angle.degrees)) pt3 = pt1.transform(Geom;;Transformation.rotation(pt0,[0,-1,0],angle2.degrees)) pt4 = [Length.mm*aa, 0, 0] pt5 = pt4.transform(Geom;;Transformation.rotation(pt0,[0,-1,0],angle.degrees)) arcarray = ents.add_arc [0,0,0], [1,0,0],[0,-1,0], Length.mm, 0, angle2.degrees arc = arcarray[0] arccurve = arc.curve verts = arccurve.vertices t1=0 x6 = (1-(t1))*(1-(t1))*pt5.x+2*(1-(t1))*(t1)*pt2.x+(t1)*(t1)*pt3.x y6 = (1-(t1))*(1-(t1))*pt5.y+2*(1-(t1))*(t1)*pt2.y+(t1)*(t1)*pt3.y z6 = (1-(t1))*(1-(t1))*pt5.z+2*(1-(t1))*(t1)*pt2.z+(t1)*(t1)*pt3.z pt6 = Geom;;Point3d.new(x6, y6, z6) t2=0.2 x7 = (1-(t2))*(1-(t2))*pt5.x+2*(1-(t2))*(t2)*pt2.x+(t2)*(t2)*pt3.x y7 = (1-(t2))*(1-(t2))*pt5.y+2*(1-(t2))*(t2)*pt2.y+(t2)*(t2)*pt3.y z7 = (1-(t2))*(1-(t2))*pt5.z+2*(1-(t2))*(t2)*pt2.z+(t2)*(t2)*pt3.z pt7 = Geom;;Point3d.new(x7, y7, z7) t3=0.4 x8 = (1-(t3))*(1-(t3))*pt5.x+2*(1-(t3))*(t3)*pt2.x+(t3)*(t3)*pt3.x y8 = (1-(t3))*(1-(t3))*pt5.y+2*(1-(t3))*(t3)*pt2.y+(t3)*(t3)*pt3.y z8 = (1-(t3))*(1-(t3))*pt5.z+2*(1-(t3))*(t3)*pt2.z+(t3)*(t3)*pt3.z pt8 = Geom;;Point3d.new(x8, y8, z8) t4=0.6 x9 = (1-(t4))*(1-(t4))*pt5.x+2*(1-(t4))*(t4)*pt2.x+(t4)*(t4)*pt3.x y9 = (1-(t4))*(1-(t4))*pt5.y+2*(1-(t4))*(t4)*pt2.y+(t4)*(t4)*pt3.y z9 = (1-(t4))*(1-(t4))*pt5.z+2*(1-(t4))*(t4)*pt2.z+(t4)*(t4)*pt3.z pt9 = Geom;;Point3d.new(x9, y9, z9) t5=0.8 x10 = (1-(t5))*(1-(t5))*pt5.x+2*(1-(t5))*(t5)*pt2.x+(t5)*(t5)*pt3.x y10 = (1-(t5))*(1-(t5))*pt5.y+2*(1-(t5))*(t5)*pt2.y+(t5)*(t5)*pt3.y z10 = (1-(t5))*(1-(t5))*pt5.z+2*(1-(t5))*(t5)*pt2.z+(t5)*(t5)*pt3.z pt10 = Geom;;Point3d.new(x10, y10, z10) t6=1 x11 = (1-(t6))*(1-(t6))*pt5.x+2*(1-(t6))*(t6)*pt2.x+(t6)*(t6)*pt3.x y11 = (1-(t6))*(1-(t6))*pt5.y+2*(1-(t6))*(t6)*pt2.y+(t6)*(t6)*pt3.y z11 = (1-(t6))*(1-(t6))*pt5.z+2*(1-(t6))*(t6)*pt2.z+(t6)*(t6)*pt3.z pt11 = Geom;;Point3d.new(x11, y11, z11) a = ents.add_line pt0, pt1 b = ents.add_line pt0, pt5 c = ents.add_curve pt0, pt6, pt7, pt8, pt9, pt10, pt11, pt1 face = ents.add_face pt6, pt7, pt8, pt9, pt10, pt11, pt1, pt0 path = ents.add_circle [Length,0,0], [-1,0,0], -Length face.followme path ents.erase_entities path face2 = ents.add_face verts path2 = ents.add_circle [Length,0,0], [-1,0,0], -Length face2.followme path2 ents.erase_entities path2 group=ents.add_group ents.to_a
-
@kmk111890 said:
Thank you, sdmitch and TIG. I could partly understand how i have to fix the codes, but not perfect because I showed you code roughly. Maybe, it would be better i show you what i'm doing now. I'm trying to add bezier curve to draw one simple shape. The equation for the quadratic bezier curve represents as shown below
B(t) = (1-t)(1-t)P0+2(1-t)P1+(t)(t)P2
With the equation, I tried to generate coordinates which are from the equation and wanted to use 'For loop'. hmmm It looked like i can make the codes by myself, but didn't work well
This seems to work
ents = Sketchup.active_model.entities Length = 1000 angle = (60)/2 angle2 = (45)/2 pt0 = [0,0,0] pt1 = [Length.mm, 0, 0] aa = 80*0.01 pt2 = pt1.transform(Geom;;Transformation.rotation(pt0,[0,-1,0],angle.degrees)) pt3 = pt1.transform(Geom;;Transformation.rotation(pt0,[0,-1,0],angle2.degrees)) pt4 = [Length.mm*aa, 0, 0] pt5 = pt4.transform(Geom;;Transformation.rotation(pt0,[0,-1,0],angle.degrees)) arcarray = ents.add_arc [0,0,0], [1,0,0],[0,-1,0], Length.mm, 0, angle2.degrees arc = arcarray[0] arccurve = arc.curve verts = arccurve.vertices pts=[];pts<<pt0 0.step(1,0.2) do |t1| x = (1-(t1))*(1-(t1))*pt5.x+2*(1-(t1))*(t1)*pt2.x+(t1)*(t1)*pt3.x y = (1-(t1))*(1-(t1))*pt5.y+2*(1-(t1))*(t1)*pt2.y+(t1)*(t1)*pt3.y z = (1-(t1))*(1-(t1))*pt5.z+2*(1-(t1))*(t1)*pt2.z+(t1)*(t1)*pt3.z pts << Geom;;Point3d.new(x, y, z) end pts<<pt1 a = ents.add_line pt0, pt1 b = ents.add_line pt0, pt5 c = ents.add_curve pts face = ents.add_face pts path = ents.add_circle [Length,0,0], [-1,0,0], -Length face.followme path ents.erase_entities path face2 = ents.add_face verts path2 = ents.add_circle [Length,0,0], [-1,0,0], -Length face2.followme path2 ents.erase_entities path2 group=ents.add_group ents.to_a
Advertisement