math.vectors: new word vclamp for clamping vectors

char-rename
Björn Lindqvist 2016-08-22 04:08:37 +02:00
parent 7a4e345f6c
commit cb39da4b31
5 changed files with 22 additions and 2 deletions

View File

@ -85,6 +85,7 @@ $nl
vunordered?
vmax
vmin
vclamp
vsupremum
vinfimum
}
@ -302,6 +303,17 @@ HELP: vmin
{ $description "Creates a sequence where each element is the minimum of the corresponding elements from " { $snippet "u" } " and " { $snippet "v" } "." }
{ $examples { $example "USING: math.vectors prettyprint ;" "{ 1 2 5 } { -7 6 3 } vmin ." "{ -7 2 3 }" } } ;
HELP: vclamp
{ $values { "v" "a sequence of real numbers" } { "min" "a sequence of real numbers" } { "max" "a sequence of real numbers" } }
{ $description "Creates a sequence where each element is clamped to the minimum and maximum elements of the " { $snippet "min" } " and " { $snippet "max" } " sequences." }
{ $examples
{ $example
"USING: math.vectors prettyprint ;"
"{ -10 30 120 } { 0 0 0 } { 100 100 100 } vclamp ."
"{ 0 30 100 }"
}
} ;
HELP: v.
{ $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "x" "a real number" } }
{ $description "Computes the dot product of two vectors." } ;

View File

@ -47,3 +47,8 @@ SPECIALIZED-ARRAY: int
{ { 1 5 3 } } [
{ t f t } [ { 1 2 3 } ] [ { 4 5 6 } ] vif
] unit-test
! vclamp
{ { 0 30 100 } } [
{ -10 30 120 } { 0 0 0 } { 100 100 100 } vclamp
] unit-test

View File

@ -276,3 +276,6 @@ PRIVATE>
: v~ ( a b epsilon -- ? )
[ ~ ] curry 2all? ; inline
: vclamp ( v min max -- w )
rot vmin vmax ; inline

View File

@ -152,7 +152,7 @@ terrain-world H{
GRAVITY v+ ;
: clamp-coords ( coords dim -- coords' )
[ { 0 0 } vmax ] dip { 2 2 } v- vmin ;
{ 0 0 } swap { 2 2 } v- vclamp ;
:: pixel-indices ( coords dim -- indices )
coords vfloor v>integer dim clamp-coords :> floor-coords

View File

@ -101,7 +101,7 @@ M: list focusable-child* drop t ;
[ list select-index ] when* ;
: clamp-loc ( point max -- point )
vmin { 0 0 } vmax ;
{ 0 0 } swap vclamp ;
: select-at ( point list -- )
[ dim>> clamp-loc ] keep