- XLogo library of procedures
These procedures can be copied and pasted into the Editor window as usual. Most do not produce any graphics. To test type Test. The results will show in the History window.
Download library of math procedures.
Decimal to Binary
Return an 8 bit binary number.
Used in CALine and Hexagram.
To Dec2Bin :Num
# convert decimal number Num to an 8 item binary list
If :Num >255 [Print [Rule is too large!] Stop]
LocalMake "Bin []
For [C 7 0 -1] [
LocalMake "Bin LPut (Quotient :Num Power 2 :C) :Bin
LocalMake "Num :Num -((Power 2 :C) *Last :Bin)]
Output :Bin
End
To Test :Num
# test procedure
Print (Sentence [Binary value of] :Num [is] Dec2Bin :Num)
End
DistanceBetween
Return the distance between two points.
To DistanceBetween :Point1 :Point2
# return distance between two points
LocalMake "Hor (First :Point1)
-(First :Point2)
LocalMake "Ver (Last :Point1)
-(Last :Point2)
Output SqRt ((Power :Hor 2) +(Power :Ver 2))
End
To Test
# test procedure
Make "Point1 [1 1] Make "Point2 [4 5]
Print (Sentence [The distance between point (] :Point1 [)
and point (] :Point2 [) is] DistanceBetween :Point1 :Point2)
End
Even (also see Odd)
Return True if :Num is even, else return False.
Used in Bulge, CloverArt, Grid and Marbles.
To Even? :Num
# return 'true' if Num even, else return 'false'
If (Mod :Num 2)=0 [Output "True] [Output "False]
End
To Test :A
# test procedure
If Even :A [Print (Sentence :A [is
even]) ] [Print (Sentence :A [is odd]) ]
End
Least (also see Most)
Return the lowest of two values.
To Least :I :J
# return lowest value
If Before :I :J [Output :I] [Output :J]
End
To Test
# test procedure
Make "A 12 Make "B 22
Print (Sentence [The lowest value of] :A [and] :B [is] Least :A :B)
End
Most (also see Least)
Return the highest of two values.
To Most :I :J
# return greatest value
If Before :I :J [Output :J] [Output :I]
End
To Test
# test procedure
Make "A 12 Make "B 22
Print (Sentence [The highest value of] :A [and] :B [is] Most :A :B)
End
Odd (also see Even)
Return True if :Num is odd, else returns False.
Used in LSystem.
To Odd? :Num
# return 'true' if Num odd, else return 'false'
If Not (Mod :Num 2) = 0 [Output "True] [Output "False]
End
To Test :A
# test procedure
Make "A 42
If Odd :A [Print (Sentence :A [is
even]) ] [Print (Sentence :A [is odd]) ]
End
Polar to Rectangular
Return rectangular x y coordinates from polar r theta coordinates.
Used in Butterfly, ElasticGrid and PolarTwist.
To PtoR :RadDist :Theta
# convert polar to rectangular co-ordinates
LocalMake "X :RadDist *Sin :Theta
LocalMake "Y :RadDist *Cos :Theta
Output List :X :Y
End
To Test
# test procedure
Make "R 42 Make "Theta 30
Print (Sentence [X Y co-ordinates are:] PtoR :R :Theta)
End
Rectangular to Polar
Return polar 'r theta' coordinates from rectangular x y coordinates.
Used in ElasticGrid.
To RtoP :X :Y
# convert rectangular to polar co-ordinates
LocalMake "Dist Sqrt ((Power :X 2) +(Power :Y 2))
If :X<0 [Output List :Dist 180 +ATan :Y/:X] [
# else :X >0
If :Y<0 [Output List :Dist 360 +ATan :Y/:X]
[Output List :Dist ATan :Y/:X]]
# else :X =0
If :Y<=0 [Output List :Dist 270] [Output List :Dist 90]
End
To Test
# test procedure
Make "X 30 Make "Y 40
Print (Sentence [r theta co-ordinates are:] RtoP :X :Y)
End
Rectangular to Polar 2
Return polar 'r theta' coordinates from rectangular x y coordinates.
Uses a second turtle and no ArcTangent function.
Used in ElasticGrid.
To RtoP :X :Y
# use a second turtle to convert rectangular to polar co-ordinates
SetTurtle 1 PenUp SetXY :X :Y
LocalMake "RadDist Sqrt ((Power :X 2)+(Power :Y 2))
SetHeading Towards [0 0]
LocalMake "myHeading Mod Int (Heading+180) 360 SetTurtle 0
Output List :RadDist :myHeading
End
To Test
# test procedure
Make "X 30 Make "Y 40
Print (Sentence [r theta co-ordinates are:] RtoP :X :Y)
End
To Root2
Output SqRt 2 # return sqrt of 2 (approx 1.414)
End
To Root3
Output SqRt 3 # return sqrt of 3 (approx 1.732)
End
Sign
Return sign of number (+1 if positive or -1 if negative or zero). Not
to be confused with Sine. By convention, Sign of zero is 1.
Used in Hop.
To Sign :Num
# return sign (-1 or 1) of number
If :Num < 0 [Output Minus 1] [Output 1]
End
![]()