Extract coordinates of a point array

To extract coordinates of a point to assign them to another point
where the z is negative…thanks for your help.
example
pt1 = [50, 65, 0]
pt1a = [pt1.x + 5, pt1.y + 5, pt1.z - 5]

…“pt1.z - 5”…incorrect ???

result I would like to achieve = pt1a = [55, 70, -5]…

Joseph.

it works in Ruby Console for me…

``````pt1 = [50, 65, 0]
pt1a = [pt1.x + 5, pt1.y + 5, pt1.z - 5]
# => [55, 70, -5]

# I would more likely use
pt1  = [50, 65, 0]
a    = [5, 5, - 5]
pt1a = pt1.zip(a).map(&:sum)
# => [55, 70, -5]
# same result but easier to apply to any array...
``````

what, where and how are you getting an error?

john

In fact, I asked for help because I can not understand ??

You are experiencing a limitation of the code-editor…
Try using something like this ?

``````pt1a = [(pt1.x + 5), (pt1.y + 5), (pt1.z - 5)]
``````

I took some space…and now it works…

Thank you so much to all of you for being interested…

Joseph.

It also so works, much better for reading code…

Thank you

Joseph

Obviously it also works with spaces…

Thank you

Joseph

Good evening, everyone…

1 Like

Just to be clear, the original problem was that ’ -5’ (with a space before ‘-’ but no space between ‘-’ and ‘5’) is taken as a negative literal number not a subtraction operator combining two terms, so the expression ‘pt1.z -5’ is a syntax error. The various ways of adding or removing spaces around ‘-’ alter the interpretation of the expression, making clear that you intended a subtraction.

Edit: I hesitate to contradict @TIG, but this has nothing to do with the code editor.

2 Likes

OK…

One more cosa…se I add… pt1 = [50.cm, 65.cm, 0.cm]
How do I fix it ??? Wanting result of x, y and z in cm units of measurement…

I need to get = pt1a = [55, 70, -5] … unit of measurement in cm.

Thank you…

also if pt1 = [50.0.cm, 65.0.cm, 0.0.cm] decimal numbers…
the result I always need pt1a = [55.0, 70.0, -5.0]…

Thank you

You’re probably correct - since I’ve never used the code-editor I can’t be sure.
It’s always best to use consistent spaces or none, and of course parentheses can help too…

``````pt1a.map {|n| Sketchup.format_length(n).split(' ')[0].to_f}
``````

but “why”, can you give an overview of what you want to achieve…

SU Ruby has lots of shortcuts for dealing with user input/outputs…

john

What I want to get is this…

if pt1 = [50, 65, 0] integer in inches.
pt1a = [(pt1.x + 5), (pt1.y + 5), (pt1.z - 5)] if the syntax is right…
result is [55, 70, -5]

or if instead of inches I use the cm, being in Italy…

if pt1 = [50.cm, 65.cm, 0.cm] if the syntax is right. integer…
pt1a = [(pt1.x + 5.cm), (pt1.y + 5.cm), (pt1.z - 5.cm)] if the syntax is right…
result is [55, 70, -5] units of measurement in cm since I used .cm in pt1…

or even…
if pt1 = [50,0.cm, 65,0.cm, 0,0.cm] decimal number in cm… is it right to use the comma? 50,0.cm or you have to use the 50.0.cm
pt1a = [(pt1.x + 5,0.cm), (pt1.y + 5,0.cm), (pt1.z - 5,0.cm)] if the syntax is right…
result is [55,0, 70,0, -5,0]

Also in Ruby the decimal separator is always .
Unlike your locale where it will probably be a ,
Ruby’s list separator is always a ,
Again unlike your locale where it will probably be a ;

So something like this will probably work…

``````if pt1 == [50.cm, 65.cm, 0.cm] ### note the double ==, to test equality
pt1a = [(pt1.x + 5.cm), (pt1.y + 5.cm), (pt1.z - 5.cm)] ### note the single =, to set the reference
else
pt1a = pt1.clone ### ? fallback result ?
end``````

All ok…se multiply “Done running code” by 2.54 the result
what you get is in cm…since Ruby code returns in inches…

Thanks, everyone

Joseph.

Such as …

``````pt1 = [50, 65, 0]
pt1a = pt1.offset( [5,5,-5] )
``````

… or …

``````pt1 = [50, 65, 0]
pt1a = pt1.transform( [5,5,-5] )
``````

Also (nitpick) these are array instances not points.

health

Joseph.

I apologize for forgetting to thank you.,
for his advice, any syntax errors, are due to the translator I use…

Joseph.