home processing download documents tutorial python tutorial gallery source about
 Python Tutorials (back to the list of tutorials)

Physics Simulation and Swarm Examples

     Tension Example 1 (requires iGeo version 7.6.0 or higher)

This page shows an example of use of tension converting lines or curves into strings of tension and applying forces to them.

The first code below shows an algorithm to import curves from an external file and to create particles at divided points on each curve. Then each pair of particles in an array is connected again with tension lines.

The input file used in the code is tension_lines1.3dm.

add_library('igeo')

size(480, 360, IG.GL)
IG.bg(0)
IG.open("tension_lines1.3dm")
curves = IG.curves()

division=30
inc = 1.0/division

for crv in curves : 
    pts = [] 
    for j in range(division+1) : # creating particles
        pts.append(IParticle( crv.pt( j*inc ) ).fric(0.01))
    pts[0].fix() # fixing the start point
    pts[division].fix() # fixing the end point
    for j in range(division) : # connecting particles
        ITensionLine(pts[j], pts[j+1]).tension(150).clr(1.0, 0.5).weight(2)
    crv.del()

The next code applies forces to the tension lines with two attractors, one with attractive force and another with repulsive force.

add_library('igeo')

size(480, 360, IG.GL)
IG.bg(0)
IG.open("tension_lines1.3dm")
curves = IG.curves()

IAttractor(15, 6, 0).intensity(10).linear(30).clr(1.0,0,0).size(8)
IAttractor(5, 4, 0).intensity(-30).linear(10).clr(1.0,0,0).size(8)

division=30
inc = 1.0/division

for crv in curves : 
    pts = [] 
    for j in range(division+1) : # creating particles
        pts.append(IParticle( crv.pt( j*inc ) ).fric(0.01))
    pts[0].fix() # fixing the start point
    pts[division].fix() # fixing the end point
    for j in range(division) : # connecting particles
        ITensionLine(pts[j], pts[j+1]).tension(150).clr(1.0, 0.5).weight(2)
    crv.del()

This third code creates instances of swarm agent IBoid class, instead of particle instances. Because of forces between swarm, each node of tension strings attracts each other trying to form clusters but the tension still pulls them apart in certain directions.

add_library('igeo')

size(480, 360, IG.GL)
IG.bg(0)
IG.open("tension_lines1.3dm")
curves = IG.curves()

division=30
inc = 1.0/division

for crv in curves : 
    pts = [] 
    for j in range(division+1) : # creating particles
        pts.append(IBoid( crv.pt( j*inc ) ).fric(0.01))
        pts[j].cohesionDist(1)
        pts[j].cohesionRatio(10)
        pts[j].separationRatio(0)
        pts[j].alignmentRatio(0)
    pts[0].fix() # fixing the start point
    pts[division].fix() # fixing the end point
    for j in range(division) : # connecting particles
        ITensionLine(pts[j], pts[j+1]).tension(150).clr(1.0, 0.5).weight(2)
    crv.del()

The next example only changes the input file. When you have random intersecting lines, swarm forces to cluster together are clear. The final lines are result of the balance between gathering swarm forces and smoothing tension forces. The code below also hides points by hide() method.

The input file used in the next code is tension_lines2.3dm.

add_library('igeo')

size(480, 360, IG.GL)
IG.bg(0)
IG.open("tension_lines2.3dm")
curves = IG.curves()

division=50
inc = 1.0/division

for crv in curves : 
    pts = [] 
    for j in range(division+1) : # creating particles
        pts.append(IBoid( crv.pt( j*inc ) ).fric(0.01))
        pts[j].cohesionDist(0.5)
        pts[j].cohesionRatio(50)
        pts[j].separationRatio(0)
        pts[j].alignmentRatio(0)
        pts[j].hide()
    pts[0].fix() # fixing the start point
    pts[division].fix() # fixing the end point
    for j in range(division) : # connecting particles
        ITensionLine(pts[j], pts[j+1]).tension(150).clr(1.0, 0.5).weight(2)
    crv.del()

This shows the random intersecting input lines.

This is the result when they are in equilibrium.


(back to the list of tutorials)

HOME
FOR PROCESSING
DOWNLOAD
DOCUMENTS
TUTORIALS (Java / Python)
GALLERY
SOURCE CODE(GitHub)
ABOUT