2
$\begingroup$

What's the most compact WL translation of this GLSL expression:

length(max(vec2(x,y),0))

?

My best attempt (using Wolfram Development Platform, not Mathematica) is

EuclideanDistance [{0,0},{Max[x,0],Max[y,0]}]

which works

enter image description here

but repeats Max.

This

EuclideanDistance [{0,0},Max[{x,y},0]]

fails thus:

enter image description here

$\endgroup$
3
$\begingroup$

Ramp

Norm @ Ramp @ {x,y}

Sqrt[Abs[Ramp[x]]^2 + Abs[Ramp[y]]^2]

Simplify[Ramp[x] - Max[x, 0]]

0

 Plot3D[Norm[Ramp@{x,y}],{x,-1,1},{y,-1,1}]

enter image description here

UnitStep

Norm[{x, y} UnitStep[{x, y}]]

Sqrt[Abs[x UnitStep[x]]^2 + Abs[y UnitStep[y]]^2]

Simplify[x UnitStep[x] - Max[x, 0]]

0

$\endgroup$
1
$\begingroup$

I would just write it out

Sqrt[Max[0, x]^2 + Max[0, y]^2]

To me, this is clearer than the alternatives which include your version maybe these two

Sqrt[#.#] &[Max[0, #] & /@ {x, y}]
EuclideanDistance[{0, 0}, Max[0, #] & /@ {x, y}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.