In this first chapter, we will see how to create interactions between objects by evaluating each object in a loop and finding some criteria that will trigger the interaction. This will require that our objects are stored in a LIST.

01 - Interactions using loops

Let's create a collection of points that are randomly distributed in space:

import random

#Point Class
class Point:
    def __init__(self, pos):
        self.pos = pos
    
#Create an empty list 
myList = []

def setup():
    size(1000, 1000) 
    background (0)
    
    #Let's create 20 points in random locations
    for i in range(0,20):
    
        x = random.randrange(0,1000)
        y = random.randrange(0,1000)
        new_point = Point ( PVector (x,y) )
        
        myList.append (new_point)
    
    #Let's check that we have 20 random points
    for pt in myList:
        stroke(255)
        point(pt.pos.x, pt.pos.y)
        print (pt.pos)

Notice that we created 20 points in random positions and we are storing them into a list.

Now let's try to create lines between the points. To do this we need each point to determine the position of another point. We can do this through a nested loop. Let's see how to achieve this:

import random

#Point Class
class Point:
    def __init__(self, pos):
        self.pos = pos
    
#Create an empty list 
myList = []

def setup():
    size(1000, 1000) 
    background (0)
    
    #Let's create 20 points in random locations
    for i in range(0,20):
    
        x = random.randrange(0,1000)
        y = random.randrange(0,1000)
        new_point = Point ( PVector (x,y) )
        
        myList.append (new_point)
    
    #Let's check that we have 20 random points
    for pt in myList:
        stroke(255)
        point(pt.pos.x, pt.pos.y)
        print (pt.pos)
        
    #Let's draw lines between the points
    for pt in myList:
        for other_pt in myList:
            if(other_pt != pt):
                
                #We will calculate the distance between points using substraction & magnitude calculations
                diff = PVector.sub(pt.pos,other_pt.pos) #vector between 2 points
                dist = diff.mag() #magnitude (distance) between the 2 points
                
                #now let's only draw lines between points that are within 200 units of distance
                if(dist < 200):
                    stroke(255)
                    line(pt.pos.x, pt.pos.y, other_pt.pos.x, other_pt.pos.y)
                    print("Line!")
                
            
            
    

Here are evaluating the distance between all points and discriminating based on the distance to establish an interaction; in this case a line. While this method works, it's not very efficient as if we have a large population of points, doing a nested loop through all of them becomes exponentially slower.

There are techniques for mitigating the performance issues, such as 'space partitioning' techniques. But that is a more advanced topic. For now, let's evaluate other methods for creating interactions between objects.