Drawing primitives (basic shapes) in Processing using Python mode is a great way to start learning about computer graphics.

1. Setting Up the Canvas

First, you'll need to set up a canvas where you'll draw the shapes. This is done using the size(width, height) function inside the setup() function.

2. Drawing Shapes

Now, let's draw some basic primitives. You'll typically do this in the draw() function, which is called repeatedly by Processing.

Rectangle

Use rect(x, y, width, height) to draw a rectangle. (x, y) specifies the top-left corner of the rectangle.

def setup():
    size(100, 180)  # Creates a 100x180 pixel canvas

def draw():
    rect(40, 20, 60, 70)  # Rectangle at (40, 20) with width 60 and height 70

Other common primitives available in Processing include:

  1. Rectangle (rect) : Draws a rectangle. Specified by the x and y coordinates of the top left corner, followed by its width and height.

  2. Ellipse (ellipse) : Draws an ellipse (circles are a special case of an ellipse). Specified by the x and y coordinates of the center, followed by its width and height.

  3. Line (line) : Draws a line from one point to another. Specified by the x and y coordinates of the start and end points.

  4. Triangle (triangle) : Draws a triangle. Specified by the x and y coordinates of its three vertices.

  5. Point (point) : Draws a point. Specified by the x and y coordinates of its location.

3. Advanced Primitives:

Using the beginShape() and endShape() functions in Processing with Python mode is a step towards more advanced graphical programming.

beginShape() and endShape()

These two functions are used together to create more complex and custom shapes.

Between these two, you define the shape using a series of vertices.

vertex()

The vertex() function is used to specify the vertices (corners) of the shape. Each call to vertex() sets a point at (x, y), and consecutive points are connected by straight lines.

Simple Example: Creating a Triangle

def setup():
    size(400, 400)

def draw():
    background(255)  # Set background to white

    # Start defining a custom shape
    beginShape()
    
    vertex(100, 100)  # First vertex at (100, 100)
    vertex(300, 100)  # Second vertex at (300, 100)
    vertex(200, 300)  # Third vertex at (200, 300)

    endShape(CLOSE)   # End the shape and close it

Result

Running this code in Processing will display a simple triangle on your canvas. This example is a basic application of beginShape() , endShape() , and vertex() for drawing custom shapes. You can create more complex shapes by adding more vertex() calls between beginShape() and endShape().