🎨
Xylo Docs
  • Introduction
  • Getting Started
    • Quickstart
  • Guides
    • Random Rotations
  • Syntax
    • Primitives
    • Functions
  • Control Flow
  • Builtin Functions
    • System
    • Math
    • Comparison
    • List
    • Randomness
    • Shape
    • Color
Powered by GitBook
On this page
  • Parameters
  • Weighted Randomness
Export as PDF
  1. Syntax

Functions

PreviousPrimitivesNextControl Flow

Last updated 4 days ago

Parameters

Functions can be defined with a custom name and must return a value.

All Xylo programs require a root function in order to render images. The root function must return a shape type.

root = l 0 FILL : square

square = ss 100 SQUARE

The square function in this example simply returns a square that has been scaled by 100 pixels along the x and y axes. Since the default square is 2x2, the returned shape's dimensions will be 200x200.

Thus, this code will render a 200x200 white square in the center of the image.

Functions can also be defined with a list of custom parameters. Let's say, for instance, we wanted a rect function with custom width and height parameters.

root = l 0 FILL : rect 200 100

rect width height = s (width / 2) (height / 2) SQUARE

The rect function can now take in width and height values and pass half their values into the s function, which scales the square on the x and y axes.

We need to divide both values by two because the default square is 2x2. Since we are passing in 200 for width and 100 for height, it scales the 2x2 square by 100 on the x axis and 50 on the y axis, resulting in a 200x100 rectangle.

Weighted Randomness

The language allows for two or more functions to be defined with the same name. For instance, say we had two shape functions, one which renders a square and the other a circle. Which shape would be rendered?

The answer is that, each time the function is called, there is a 50% chance it will render a square and a 50% chance it will render a circle. This allows generated images to have random outputs!

root = l 0 FILL : shape

shape = ss 100 SQUARE

shape = ss 100 CIRCLE

Try running this code multiple times. You will see that it randomly chooses between two different image outputs each time it is ran.

Let's say we want to change things up a bit. We want the code to also render a triangle a third of the time. We can simply add another function definition.

root = l 0 FILL : shape

shape = ss 100 SQUARE

shape = ss 100 CIRCLE

shape = ss 100 TRIANGLE

Now, the square will be rendered â…“ of the time, the circle â…“ of the time, and the triangle the other â…“ of the time.

There's one additional feature to function randomness: weights. Weights allow us to define exactly how often we want certain outputs to be selected.

Say, for instance, we want the square to be rendered ½ the time, the circle ¼ the time, and the triangle ¼ the time.

root = l 0 FILL : shape

shape@2 = ss 100 SQUARE

shape@1 = ss 100 CIRCLE

shape@1 = ss 100 TRIANGLE

We can use the @ symbol to define custom weights like so. Since 2 + 1 + 1 = 4, the square will be rendered 2 out of 4 times, the circle 1 out of 4, and the triangle 1 out of 4.

Since the default weight for a function is already one, we don't need to define the weights for the second and third shape definitions.

We can also make this code more flexible by passing in a size argument to scale each shape by.

Thus, our refactored code will be the following:

root = l 0 FILL : shape 100

shape@2 size = ss size SQUARE

shape size = ss size CIRCLE

shape size = ss size TRIANGLE

Now every 100 times the function is called, about 50 will be squares, about 25 will be circles, and about 25 will be triangles, on average.

root = l 0 FILL : collect rows

rows =
    for i in 0..10
        collect (cols i)

cols i =
    for j in 0..10
        t (i * 40 - 180) (j * 40 - 180) (shape 10)

shape@2 size = hex 0xd600ff (ss size SQUARE)

shape size = hex 0x00b8ff (ss size CIRCLE)

shape size = hex 0x00ff9f (ss size TRIANGLE)

This will generate 100 shapes, calling a random version of the shape function each time.

As you can see, the output is different each time the program is ran. This allows for an incredible number of possible outputs (~5.15×1047)!

We can demonstrate this with a more advanced use case by arranging the shapes on a 10x10 grid across the image. For more information, check out the and sections.

Color
For Loop