[Grasshopper]数式や計算式、関数を使うことができるExpressionの使い方

[Grasshopper]数式や計算式、関数を使うことができるExpressionの使い方 Grasshopperコンポーネント

数式や計算式、関数を使うことができる、Expressionの使い方について解説します。

見え方

グラスホッパー上では上のように表されます。

本記事では、コンポーネントはアイコン表示になっています。

アイコン表示に変えたい場合、以下のリンク先の記事を参考にしてください。

数式や計算式を使う

Expressionを使うことで、数式や計算式を使うことができます。

Expressionの設定画面

ダブルクリック

Expressionをダブルクリックすると、Expressionの設定画面が表示されます。

Expressionの設定画面

こちらが、Expressionの設定画面になります。

Expression

Expressionの部分を変えることで、任意の数式や計算式を作ることができます。

記号

また、上のアイコンをクリックすることで、数式や計算式に記号や関数を追加することもできます。

数式や計算式を作る

計算してみる

まずは簡単な計算をしてみます。

最初は、Expressionにx+yと入力しました。

その後、右下のOKを押します。

エラー表示

すると、上の画像のように赤くエラー表示されます。

しかし、このままでも問題ありません。

左側の端子

左側の端子に情報を入力します。

デフォルトでは、x端子とy端子があります。

今回は、x端子に3、y端子に5を入力しました。

すると、3+5の足し算が行われ、R端子から8が出力されました。

このように、左側の端子からは変数を入力します。

端子・変数の数を増やす

左側の端子の数を変える

左側の端子の数を変えることもできます。

Expressionにズームすると、+と-のアイコンが表示されるようになります。

+のアイコンを押すと、その場所に端子が追加されます。

-のアイコンを押すと、その部分にある端子が消えます。

今回は、y端子の下の+を押しました。

端子が追加された

すると、新しくz端子が追加されました。

計算式を変える

z端子ができたので、計算式を変えてx+y+zに変更しました。

複数の端子で計算できた

すると、このように3つの端子で計算できました。

このように、複数の端子で計算することもできます。

端子・変数名を変える

変数名を変える

端子の名前を変えることで、変数名を変えることもできます。

その場合、変更したい端子を右クリックします。

その後、端子名を変更します。

今回は、端子名をtにしました。

計算式の変数名も変える

その場合、計算式の変数名も変えます。

今回は端子名をtにしたので、x+y+tにしました。

変数名を変えて計算できた

すると、このように変数名を変えて計算できました。

長い変数名

長い変数名は、上の画像のように長くすることもできます。

文字列として結合する

&

&を使うことで、入力した情報を文字列として結合させることもできます。

今回は、x&yをExpressionに入力しました。

文字列として結合

その後、左側の端子に情報を入力します。

最初は、x端子に3、y端子に5を入力しました。

すると、上の画像のように35と表示されました。

これは数値の35ではなく、3と5が文字として隣り合っている状態です。

文字列として結合2

文字として扱うため上の画像のように、線の数:6のような使い方もできます。

座標を作成する

座標を作成することもできます。

座標を作成する

その場合、Expressionに{x,y,z}のように入力します。

その後、左側の端子にx、y、z座標のそれぞれの数値を入力します。

すると、座標データが作成されます。

点が作成される

ライノセラス上を見てみると、指定した座標に点が作成されていることが分かります。

関数を使う

次は、関数を使う方法を解説します。

使用できる関数を確認

Expressionの設定画面で、一番右上の部分をクリックすると、使用できる関数を確認することができます。

関数を確認

このように、関数一覧が表示されます。

以下に、使用できる関数を一覧でまとめました。

一番左のNameは関数名です。

真ん中のSignatureは、Expressionでの関数の入力方法です。

Descriptionは、関数の説明(英語)です。

NameSignatureDescription
AbsAbs(x)Returns the absolute value of a specified number or vector
AcosAcos(x)Returns the angle whose cosine is the specified number
AsinAsin(x)Returns the angle whose sine is the specified number
AtanAtan(x)Returns the angle whose tangent is the specified number
Atan2Atan2(x, y)Returns the angle whose tangent is the quotient of two specified numbers
CDblCDbl(x)Creates a floating point number
CeilingCeiling(x)Returns the smallest integer greater than or equal to the specified number
ClntClnt(x)Converts a number or string to the nearest integer
ContainsContains(s, p)Tests whether [p] occurs within [s]
CosCos(x)Returns the cosine of an angle
CoshCosh(x)Returns the hyperbolic cosine of an angle
Define{a,b[,c]}Create a new vector, plane or complex construct
DegDeg(x)Converts an angle in radians to degrees
DistanceDistance(x, y)Returns the distance (Pythagorean) between two numbers or vectors
EndsWithEndsWith(s, a)Test whether [s] ends with [a]
ExpExp(x)Returns e raised to the specified power
FixFix(x)Returns the integer portion of a number
FloorFloor(x)Returns the largest integer less than or equal to the specified number
FormatFormat(s[, a, b, …])Replaces each format item in a specified String with the text equivalent of a corresponding value
GMeanĠ(x[ y, z , …])Returns the geometric mean of a set of numbers
HMeanŪ(x[ y, z , …])Returns the harmonic mean of a set of numbers
HypotHypot(x, y)Returns the length of the hypotenuse of a right triangle
IfIf(test, A, B)Returns A if test is True, B if test is false
IndexOfIndexOf(s, a[, i])Find the first character position of [a] within [s], starting the search at index [i]
IntInt(x)Returns the integer portion of a number
LCaseLCase(s)Converts all characters in a string to their lower case equivalent
LeftLeft(s, i)Returns the [i] characters on the left hand side of the string
LengthLength(x)Returns the magnitude of a vector or the number of characters in a string
LnLn(x)Returns the natural (base e) logarithm of a specified number
LogLog(x[,b])Returns the base [b] logarithm of a specified number
Log10Log10(x)Returns the base 10 logarithm of a specified number
MaxMax(x[ Z, …])Returns the maximum value in a set of numbers
MeanA(x[, Z, …])Returns the mean (average) of a set of numbers, vectors or planes
MinMin(x[, y, Z, …])Returns the minimum value in a set of numbers
MinkowskiDistanceMinkowskiDistance(x, y, p)Returns the p-order Minkowski distance between two numbers or vectors
PowPow(x, y)Returns a specified number or vector raised to the specified power
ProdΠ(x[, y, z, …])Returns the product of a set of numbers
RadRad(x)Converts an angle in degrees to radians
ReplaceReplace(s, a, b)Replaces all occurrences of [a] in [s], with [b]
RightRight(s) i)Returns the [i] characters on the right hand side of the string
RoundRound(x[, d])Rounds a floating point number to the specific decimal places
SinSin(x)Returns the sine of an angle
SinhSinh(x)Returns the hyperbolic sine of an angle
SqrtSqrt(x)Returns the square root of a specified number
StartsWithStartsWith(s, a)Test whether [s] starts with [a]
SubStringSubString(s, i[ I])Returns a substring based on start char index and length
Sum∑(x[, y, z, …])Returns the sum of a set of numbers or vectors
TanTan(x)Returns the tangent of an angle
TanhTanh(x)Returns the hyperbolic cosine of an angle
UCaseUCase(s)Converts all characters in a string to their upper case equivalent
Unitize[v]Returns a unit length vector

関数の使用例

いくつかの関数の使用例を見てみましょう。

使用例1

If関数

1つ目の例は、If関数です。

If関数は、Expressionの左上のアイコンをクリックすることでもできます。

If関数を使うには、If(条件, 真の場合, 偽の場合)のように入力します。

If関数の使用例

今回は、If(x<100, y, -y)と入力しました。

If関数の使用例2

この場合、xが100未満の時、yは負の値になります。

今回の場合、xに入力した数値は75で100より小さいので、出力される数値は正の値です。

If関数の使用例3

逆にxが100以上の時は、yは正の値になります。

今回の場合、xに入力した数値は115で100より大きいので、出力される数値は負の値です。

If関数の使用例4

使用コンポーネント:①Series ②Expression ③Construct Point

上の例は、点を複数作成する例です。

先程の方法を応用して、ある一定の数値をxが超えた場合、負の数値になるので、点の位置が変わります。

SeriesのN端子に、増加数の1を入力します。

C端子に、作成する数値の個数の200を入力します。

すると、0~199の数値が作成されます。

その数値を、先程のExpressionのx端子につなげます。

その後、Construct PointのX端子に、Seriesをつなげます。

Y端子に、ExpressionのR端子をつなげます。

Z端子に、0をつなげます。

すると、X座標100で、生成される点のY座標が負の座標に来るようになりました。

使用例2

次の使用例は、Sin関数です。

Sin関数

Sin関数を使うには、ExpressionでSin(x)と入力します。

Sin関数使用例

使用コンポーネント:①Pi ②SeriesMultiplication ④Expression ⑤Construct Point

Piを使うことで、円周率のπを使うことができるようになります。

0.1を、SeriesのN端子につなげます。

C端子に、作成する数値の個数を入力します。

その後、PiとSeriesMultiplicationにつなげます。

さらに、MultiplicationをExpressionにつなげます。

Construct PointのX端子に、Multiplicationをつなげます。

Construct PointのY端子に、Expressionをつなげます。

Sin波

すると、このようにSin波を描くように点が作成されました。

Cos関数

Cos関数に変えてみます。

cosine

すると、Cosine波に変わりました。

使用例3

次は、Min関数です。

Min関数

Min関数を使うには、ExpressionでMin(x,y,z)と入力します。

Min関数を使うと、左側の端子に入力した数値から、一番最小の数値のみを出力します。

使用例4

最後は、シグマ関数です。

シグマ関数

シグマ関数を使うには、Expressionで∑(x,y,z,u,v…)のように入力します。

シグマ関数を使うと、左側に入力した数値を合計した数値を出力します。

Expressionコンポーネントを使用しているグラスホッパー記事はこちら↓

コメント

タイトルとURLをコピーしました