Addition of vectors and points
-
I have posted this also on google group, but the group seems inactive.
Dear Group,
I have recently discovered this great world of ruby programming in SU. I have used sketch up and the various plugins for a long time, but never wrote one myself.
I am writing a few plugins to help in my work. I am an engineer and work in sustainable buildings/masterplanning.I am having issues in adding two points - precisely a point and a vector. From what I have read this should be possible using the addition operator +, so that
new point = point + vector
In my code I have two vectors m and v. When I show them with UI.messagebox I can see that they are three elements arrays, but when I write m+v the output is actually a string concatenation.
Am I missing some thing?
I know could use [m.x+v.x,m.y+v.y,m.z+v.z] but the other solution is much more elegant and clear.
Thanks a lot for any help.
Ruggiero
-
What is the question?
How points and vectors are represented as strings?
Or the point + vector operation?
What is your result and what is your expected result?
-
From the Ruby Console:
<span class="syntaxdefault"><br />pt </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Point3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">2</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">3</span><span class="syntaxkeyword">)<br /></span><span class="syntaxcomment"># => Point3d(1, 2, 3)<br /><br /></span><span class="syntaxdefault">v </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Vector3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">10</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">20</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">30</span><span class="syntaxkeyword">)<br /></span><span class="syntaxcomment"># => Vector3d(10, 20, 30)<br /><br /></span><span class="syntaxdefault">result </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> pt </span><span class="syntaxkeyword">+</span><span class="syntaxdefault"> v<br /></span><span class="syntaxcomment"># => Point3d(11, 22, 33)<br /> </span><span class="syntaxdefault"></span>
Is this not what you would expect to see?
-
After a bit of playing around, the addition seems to work in all the situations I tried - I can only think that inappropriate classes of object are being used.
Even UI.messagebox(pt+v,MB_OK) with no explicit string substitutions works fine.Are you trying to add two arrays directly? For the Array class the '+' method is indeed defined as a concatenation - not of strings, but of the arrays themselves. This returns a single, bigger array, containing the contents of the two input arrays.
The essential step in ThomThom's example is the explicit creating of Vector3d and Point3d objects. Although they can be converted to arrays, and have a similar appearance, they are NOT in themselves arrays or even sub-classes of Array - they are Sketchup specific classes of object for which the '+' method is defined differently in order to make geometry easier.
The x,y,z parameters can be turned into Array form using the '.to_a' method - but this should only be done if you are certain that a Ruby Array object is really what you need, as the '+' method will then revert to the standard Array class version.
Defining '+' any other way for arrays would make little sense, as they can contain many different kind of objects (within the same array even) - and for the vast majority of 'non-number' objects, an item by item addition would simply return an error.
-
Thanks guys for the answers. I got it now. It makes actually perfect sense.
I was defining generic arrays and expecting them to behave like geometric vectors!
Thanks again
-
@ruggiero said:
I was defining generic arrays and expecting them to behave like geometric vectors!
Why should they? The SketchUp API does not override
Array#+()
, so you need to consult the Standard Ruby dictionary. (Either online, or the downloadable CHM. Follow the link in my signature, to more links in the Ruby Resources sticky thread.)
Advertisement